字符流中第一个不反复的字符
题目形容
请实现一个函数用来找出字符流中第一个只呈现一次的字符。例如,当从字符流中只读出前两个字符”go”时,第一个只呈现一次的字符是”g”。当从该字符流中读出前六个字符“google”时,第一个只呈现一次的字符是”l”。
题目链接: 字符流中第一个不反复的字符
代码
<code class="java">import java.util.LinkedList; import java.util.Queue; /** * 题目:字符流中第一个不反复的字符 * 题目形容 * 请实现一个函数用来找出字符流中第一个只呈现一次的字符。例如,当从字符流中只读出前两个字符"go"时, * 第一个只呈现一次的字符是"g"。当从该字符流中读出前六个字符“google"时,第一个只呈现一次的字符是"l"。 * 题目链接: * https://www.nowcoder.com/practice/00de97733b8e4f97a3fb5c680ee10720?tpId=13&&tqId=11207&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking */ public class Jz54 { private int[] cnts = new int[256]; private Queue<Character> queue = new LinkedList<>(); //Insert one char from stringstream public void insert(char ch) { cnts[ch]++; queue.add(ch); while (!queue.isEmpty() && cnts[queue.peek()] > 1) { queue.poll(); } } //return the first appearence once char in current stringstream public char firstAppearingOnce() { return queue.isEmpty() ? '#' : queue.peek(); } public static void main(String[] args) { } }
【每日寄语】 愿你昨晚的坏情绪,在今日掀开被子,拉开窗帘的那一刻,杳无踪影。