第一次只出现一个的字符

import java.util.*;
public class Solution {
    public int FirstNotRepeatingChar(String str) {
        Map<Character,Integer> res = new LinkedHashMap<Character,Integer>();
        Map<Character,Integer> index=  new HashMap<Character,Integer>();
        for(int i =0;i<str.length();i++){
            if(res.containsKey(str.charAt(i))){
                res.put(str.charAt(i),res.get(str.charAt(i))+1);
            }else{
                res.put(str.charAt(i),1);
                index.put(str.charAt(i),i);
            }
        }
        //可以使用一个Map,第二次遍历可以遍历String
        for(Map.Entry<Character, Integer> y:res.entrySet()) {                               //常用遍历方法
            if(y.getValue()==1)
                return index.get(y.getKey());
           }
        return -1;
    }
}
原文地址:https://www.cnblogs.com/a1225234/p/11012908.html