请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g"。当从该字符流中读出前六个字符“google"时,第一个只出现一次的字符是"l"。


public class Solution {
    //Insert one char from stringstream
    int[] arr = new int[256];
    int temp = 1;
    public void Insert(char ch)
    {
        if(arr[ch] == 0){
            arr[ch] = temp++;
        }
        else{
            arr[ch] = -1;
        }
    }
  //return the first appearence once char in current stringstream
    public char FirstAppearingOnce()
    {
        int num = Integer.MAX_VALUE;
        char ch = '#';
        for(int i = 0; i < 256; i++){
            if(arr[i] != 0 && arr[i] != -1 && arr[i] < num){
                num = arr[i];
                ch = (char)i;
            }
        }
        return ch;
    }
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM