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

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

https://www.nowcoder.com/questionTerminal/00de97733b8e4f97a3fb5c680ee10720?answerType=1&f=discussion

也算是一种思路,使用HashSet存储不重复字符,每个字符只会存一次,不重复存,使用ArrayList存第一次出现的字符,只会存出现一次的,重复出现就删除。

import java.util.ArrayList;
import java.util.HashSet;
public class Solution {
    HashSet<Character> set = new HashSet<>();
    ArrayList<Character> list = new ArrayList<>();
    //Insert one char from stringstream
    public void Insert(char ch)
    {
        if (set.contains(ch)){
            for (int i = 0;i<list.size(); i++){
                if (ch == list.get(i)){
                    list.remove(i);
                }
            }
        }else {
            set.add(ch);
            list.add(ch);
        }
    }
  //return the first appearence once char in current stringstream
    public char FirstAppearingOnce()
    {
        if (!list.isEmpty()) return list.get(0);
        return '#';
    }
}
全部评论

相关推荐

04-27 08:59
常州大学 Java
牛客139242382号:《两门以上汇编语言》
点赞 评论 收藏
分享
每晚夜里独自颤抖:要求太多的没必要理
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务