剑指 Offer 50. 第一个只出现一次的字符
剑指 Offer 50. 第一个只出现一次的字符
在字符串 s 中找出第一个只出现一次的字符。如果没有,返回一个单空格。 s 只包含小写字母。
示例:
s = "abaccdeff"
返回 "b"
s = ""
返回 " "
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/di-yi-ge-zhi-chu-xian-yi-ci-de-zi-fu-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
class Solution {
public char firstUniqChar(String s) {
if(s.length() == 0) return ' ';
int[] a = new int[26]; //用标记法,建立26个小写字母数组
for(int i=0; i<s.length(); i++){
a[s.charAt(i) - 'a']++; //在对应位置+1
}
int t = -1;
for(int i=0; i<s.length(); i++){
if(a[s.charAt(i) - 'a'] == 1){ //对应位置为1的时候,那么就是答案
return s.charAt(i);
}
}
return ' ';
}
}
由于时间有限,写的不好请见谅,理解万岁(: