【题目描述】
AcWing 59. 把数字翻译成字符串
【思路】
HashMap只是用于统计每个字母的出现次数 后面的顺序(保证是第一个)是靠遍历原来字符串来保证的
class Solution {
public char firstNotRepeatingChar(String s) {
if( s == null || s.length() == 0) return '#';
char c[] = s.toCharArray();
int n = c.length;
Map<Character,Integer> map = new LinkedHashMap<>();
for(int i = 0; i < n; i ++)
{
if( !map.containsKey(c[i])) map.put(c[i], 1);
else{
int v = map.get(c[i]);
map.put(c[i],v + 1);
}
}
for(int i = 0; i < n; i ++)
if( map.get(c[i]) == 1 )
return c[i];
return '#';
}
}