英文:
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<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();
}
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<Character,Integer>
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!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论