Easy | 剑指 Offer 50. 第一个只出现一次的字符

剑指 Offer 50. 第一个只出现一次的字符

在字符串 s 中找出第一个只出现一次的字符。如果没有,返回一个单空格。 s 只包含小写字母。

示例:

s = "abaccdeff"
返回 "b"

s = "" 
返回 " "

解题思路

第一遍使用Hash表统计出现的次数, 第二次从左往右遍历找只出现一次的

public char firstUniqChar(String s) {
    int[] countAlpha = new int[26];
    char[] str = s.toCharArray();
    for (Character c : str) {
        countAlpha[c - 'a']++;
    }
    for (Character c : str) {
        if (countAlpha[c - 'a'] == 1) {
            return c;
        }
    }
    return ' ';
}
原文地址:https://www.cnblogs.com/chenrj97/p/14295535.html