利用LinkedHashMap查找结果
解题思路
将便利字符串,将其存入LinkedHashMap,在遍历map找到value等于1的key并返回。
class Solution {
public char firstUniqChar(String s) {
if(s.length()==0)
return ' ';
char[] chasrs = s.toCharArray();
Map<Character,Integer> charsMap = new LinkedHashMap<>();
for (int i = 0; i < chasrs.length;i++){
Integer total = charsMap.get(chasrs[i]);
if (total!=null) {
charsMap.put(chasrs[i], total + 1);
}else {
charsMap.put(chasrs[i],1);
}
}
for(Map.Entry<Character,Integer> entry:charsMap.entrySet()) {
if(entry.getValue()==1){
return entry.getKey();
}
}
return ' ';
}
}