字符串中第一个只出现一次的字符-50-1
在字符串中找出第一个只出现一次的字符。
如输入"abaccdeff"
,则输出b
。
如果字符串中不存在只出现一次的字符,返回#字符。
样例:
输入:"abaccdeff"
输出:'b'
思路:
class Solution{
// 用数组实现 hash表 '0' 为 ASCII 的第一个字符
public char firstNotRepeatingChar(String s) {
int[] count = new int[256];
char[] chars = s.toCharArray();
for(char c : chars)
count[c-'0']++;
for(char c : chars){
if(count[c-'0'] == 1) return c;
}
return '#';
}
}