找到流中不重复的字符

huangapple go评论84阅读模式
英文:

Find non repeating character in a stream

问题

import java.util.*;
import java.lang.*;
import java.io.*;

class GFG {

    public static void firstnonrepeatingcharacterinstream(char[] stream) {
        HashMap<Character, Integer> h = new HashMap<>();
        Queue<Character> q = new LinkedList<>();

        for (int i = 0; i < stream.length; i++) {
            h.put(stream[i], h.getOrDefault(stream[i], 0) + 1);

            if (h.get(stream[i]) == 1) {
                q.add(stream[i]);
            }
        }

        while (!q.isEmpty()) {
            char c = q.peek();

            if (h.get(c) == 1) {
                break;
            } else {
                q.remove();
            }
        }

        if (q.isEmpty()) {
            System.out.print(-1 + "");
        } else {
            System.out.print(q.peek() + "");
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int T = sc.nextInt();

        while (T-- > 0) {
            int N = sc.nextInt();

            char stream[] = new char[N];

            for (int i = 0; i < N; i++) {
                stream[i] = sc.next().charAt(0);
            }
            firstnonrepeatingcharacterinstream(stream);
        }
    }
}
英文:

First non-repeating character in a stream. I tried to solve this question in geeks for geeks and it is throwing an error.Can someone help me out?
https://practice.geeksforgeeks.org/problems/first-non-repeating-character-in-a-stream/0#ExpectOP

import java.util.*;
import java.lang.*;
import java.io.*;

class GFG {
    
    public static void firstnonrepeatingcharacterinstream(char[] stream) {
        HashMap&lt;Character,Integer&gt; h = new HashMap&lt;&gt;();
        Queue&lt;Character&gt; q = new LinkedList&lt;&gt;();

        for(int i = 0; i &lt; stream.length; i++) {
            h.put(stream[i], h.getorDefault(stream[i], 0) + 1);

            if(h.get(stream[i])==1) {
                q.add(stream[i]);
            }
        }
       
        while(!q.isEmpty()) {
            char c = q.peek();

            if(h.get(c) == 1) {
                break;
            } else {
                q.remove();
            }
        }

        if(q.isEmpty()) {
            System.out.print(-1 + &quot;&quot;);
        } else {
            System.out.print(q.peek() + &quot;&quot;);
        }
    }


	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		
        int T = sc.nextInt();
		
        while (T-- &gt; 0) {
		    int N=sc.nextInt();

		    char stream[]=new char[N];

            for(int i = 0; i &lt; N; i++) {
                stream[i]=sc.next();
            }
			firstnonrepeatingcharacterinstream(stream);
        }
    }
}

I get this error:

Compilation Error
prog.java:12: error: cannot find symbol
h.put(stream[i],h.getorDefault(stream[i],0)+1);
^
symbol:   method getorDefault(char,int)
location: variable h of type HashMap&lt;Character,Integer&gt;
1 error

答案1

得分: 1

第一个错误

prog.java:12: 错误: 找不到符号
h.put(stream[i],h.getorDefault(stream[i],0)+1); 

是因为 "or" 没有大写,应该是大写的 "OrDefault"。应该改为如下:

h.put(stream[i],h.getOrDefault(stream[i],0)+1);

至于其他错误,是因为你试图不正确地将一个对象转换为字符:

char c=q.peek();

以及不正确地尝试将字符串转换为字符:

stream[i]=sc.next();

希望这能帮到你,祝你面试准备顺利!

英文:

The first error

prog.java:12: error: cannot find symbol
h.put(stream[i],h.getorDefault(stream[i],0)+1); 

is because the Or is not capitalized like it should be. It should read the following:

h.put(stream[i],h.getOrDefault(stream[i],0)+1);

As for the other errors, they are due to you trying to convert an Object into a char improperly:

char c=q.peek();

As well as improperly try to convert a String to a char:

stream[i]=sc.next();

I hope this helps and good luck with the Interview Prep!

huangapple
  • 本文由 发表于 2020年9月11日 00:59:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63834389.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定