找到流中不重复的字符

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

Find non repeating character in a stream

问题

  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4. class GFG {
  5. public static void firstnonrepeatingcharacterinstream(char[] stream) {
  6. HashMap<Character, Integer> h = new HashMap<>();
  7. Queue<Character> q = new LinkedList<>();
  8. for (int i = 0; i < stream.length; i++) {
  9. h.put(stream[i], h.getOrDefault(stream[i], 0) + 1);
  10. if (h.get(stream[i]) == 1) {
  11. q.add(stream[i]);
  12. }
  13. }
  14. while (!q.isEmpty()) {
  15. char c = q.peek();
  16. if (h.get(c) == 1) {
  17. break;
  18. } else {
  19. q.remove();
  20. }
  21. }
  22. if (q.isEmpty()) {
  23. System.out.print(-1 + "");
  24. } else {
  25. System.out.print(q.peek() + "");
  26. }
  27. }
  28. public static void main(String[] args) {
  29. Scanner sc = new Scanner(System.in);
  30. int T = sc.nextInt();
  31. while (T-- > 0) {
  32. int N = sc.nextInt();
  33. char stream[] = new char[N];
  34. for (int i = 0; i < N; i++) {
  35. stream[i] = sc.next().charAt(0);
  36. }
  37. firstnonrepeatingcharacterinstream(stream);
  38. }
  39. }
  40. }
英文:

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

  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4. class GFG {
  5. public static void firstnonrepeatingcharacterinstream(char[] stream) {
  6. HashMap&lt;Character,Integer&gt; h = new HashMap&lt;&gt;();
  7. Queue&lt;Character&gt; q = new LinkedList&lt;&gt;();
  8. for(int i = 0; i &lt; stream.length; i++) {
  9. h.put(stream[i], h.getorDefault(stream[i], 0) + 1);
  10. if(h.get(stream[i])==1) {
  11. q.add(stream[i]);
  12. }
  13. }
  14. while(!q.isEmpty()) {
  15. char c = q.peek();
  16. if(h.get(c) == 1) {
  17. break;
  18. } else {
  19. q.remove();
  20. }
  21. }
  22. if(q.isEmpty()) {
  23. System.out.print(-1 + &quot;&quot;);
  24. } else {
  25. System.out.print(q.peek() + &quot;&quot;);
  26. }
  27. }
  28. public static void main (String[] args) {
  29. Scanner sc = new Scanner(System.in);
  30. int T = sc.nextInt();
  31. while (T-- &gt; 0) {
  32. int N=sc.nextInt();
  33. char stream[]=new char[N];
  34. for(int i = 0; i &lt; N; i++) {
  35. stream[i]=sc.next();
  36. }
  37. firstnonrepeatingcharacterinstream(stream);
  38. }
  39. }
  40. }

I get this error:

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

答案1

得分: 1

第一个错误

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

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

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

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

  1. char c=q.peek();

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

  1. stream[i]=sc.next();

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

英文:

The first error

  1. prog.java:12: error: cannot find symbol
  2. 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:

  1. 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:

  1. char c=q.peek();

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

  1. 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:

确定