今日算法之_113_第一个只出现一次的字符
前言
Github:https://github.com/HealerJean
1、第一个只出现一次的字符
在字符串 s 中找出第一个只出现一次的字符。如果没有,返回一个单空格。 s 只包含小写字母。
示例:
s = "abaccdeff"
返回 "b"
s = ""
返回 " "
1.1、解题思路
遍历两次,map接收
1.2、算法
public char firstUniqChar(String s) {
List<Map<Character, Integer>> list = new ArrayList<>();
Map<Character, Integer> map = new HashMap<>();
for (Character ch : s.toCharArray()){
map.put(ch, map.getOrDefault( ch,0) +1);
}
for (Character ch : s.toCharArray()){
if (map.get(ch) == 1){
return ch;
}
}
return ' ' ;
}
1.3、测试
@Test
public void test(){
System.out.println(firstUniqChar("asfasdf"));
}