题解 | #字符流中第一个不重复的字符#

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

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

利用map进行查重

import java.util.*;
public class Solution {
    //Insert one char from stringstream
    String str = "";
    public void Insert(char ch)
    {
        str = str +  ch;
    }
  //return the first appearence once char in current stringstream
    public char FirstAppearingOnce()
    {
        Map<String,Integer> we = new HashMap<>();
        String[] list = str.split("");
        int op = 0;
        int k = 0;
        char w = '#';
        for(String i : list){
            if(we.containsKey(i)){
                we.put(i,we.get(i)+1);
            }else{
                we.put(i,1);
            }
        }
        for(int i=0;i<list.length;i++){
            if(we.get(list[i]) == 1){
                op = 1;
                k =i;
                break;
            }
        }
        if(op == 0){
            return '#';
        }else{
            return str.charAt(k);
        }
    }
}
全部评论

相关推荐

一表renzha:手写数字识别就是一个作业而已
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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