剑指 Offer 第50题:第一个只出现一次的字符
2022-08-06 本文已影响0人
放开那个BUG
1、前言

2、思路
使用 LinkedHashMap,因为 LinkedHashMap 使用链表的方式记录了 key 插入的先后顺序
3、代码
class Solution {
public char firstUniqChar(String s) {
if(s == null || s.length() == 0){
return ' ';
}
Map<Character, Integer> map = new LinkedHashMap<>();
for(char ch : s.toCharArray()){
map.put(ch, map.getOrDefault(ch, 0) + 1);
}
for(char ch : map.keySet()){
if(map.get(ch) == 1){
return ch;
}
}
return ' ';
}
}