题解 | #字符流中第一个不重复的字符# Java 直接用 LinkedHashSet 解决

字符流中第一个不重复的字符

http://www.nowcoder.com/practice/00de97733b8e4f97a3fb5c680ee10720

public class Solution {
  LinkedHashSet<Character> set = new LinkedHashSet<>();

  //Insert one char from stringstream
  public void Insert(char ch) {
    if (set.contains(ch))
      set.remove(ch);
    else
      set.add(ch);
  }

  //return the first appearence once char in current stringstream
  public char FirstAppearingOnce() {
    if (set.isEmpty()) return '#';
    return set.iterator().next();
  }
}
全部评论
你的代码好像有点问题,LinkedHashSet弹空了之后如果再来之前出现过的字符是无法判断出来的,还是会继续输出
点赞 回复
分享
发布于 2021-10-19 22:12

相关推荐

头像
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务