387. First Unique Character in a String

通刷的时候,破处在这个题上了。。记录一下吧。。

装逼1-PASS,失败。

老老实实MAP 2pass..

话说JAVA的这个代码规则看着真乱。。我还是喜欢

func(int a)
{
    //
    eatShit(a);
    if(a==0)
    {
        int c = 0;
    }
}

这种括号格式

public class Solution {
    public int firstUniqChar(String s) {
        if(s.length() == 0) return -1;
        
        Map<Character,Integer> map = new HashMap<Character,Integer>();
        for(int i = 0; i < s.length();i++){
            char c = s.charAt(i);
            if(map.containsKey(c)){
                map.put(c,map.get(c)+1);
            }else{
                map.put(c,1);
            }
            
        }
        
        for(int i = 0; i < s.length();i++){
            if(map.get(s.charAt(i)) == 1) return i;
        }
        
        return -1;
        
        
    }
}
posted @ 2016-10-20 03:28  哇呀呀..生气啦~  阅读(156)  评论(0编辑  收藏  举报