在字符串 s 中找出第一个只出现一次的字符。如果没有,返回一个单空格。 s 只包含小写字母。
剑指 Offer 50. 第一个只出现一次的字符 - 力扣(LeetCode) (leetcode-cn.com)
思路:如果某个字符第一次出现的位置和最后一次出现的位置相等,说明该字符只出现了一次
class Solution {
public char firstUniqChar(String s) {
if(s == null || s.length() == 0){
return ' ';
}
char c;
for(int i = 0; i < s.length(); i++){
c = s.charAt(i);
if(s.indexOf(c) == s.lastIndexOf(c)){
return c;
}
}
return ' ';
}
}