问题描述
在字符串 s 中找出第一个只出现一次的字符。如果没有,返回一个单空格。 s 只包含小写字母。
示例:
s = “abaccdeff”
返回 “b”
s = “”
返回 " "
问题解决
解决存在性的问题,可使用map。set和数组
class Solution {
public char firstUniqChar(String s) {
if(s == null || s.length() == 0) return ' ';
char[] table = new char[26];
for(int i = 0; i < s.length(); i++) {
table[s.charAt(i) - 'a']++;
}
for(int i = 0; i < s.length(); i++) {
if(table[s.charAt(i) - 'a'] == 1)
return s.charAt(i);
}
return ' ';
}
}