面试题50

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

示例:

s = "abaccdeff"
返回 "b"

s = "" 
返回 " "

限制:

  • 0 <= s 的长度 <= 50000

Solutions

  1. straight forward

class Solution {
public:
    char firstUniqChar(string s) {
        // remember to initialize, otherwise values are undefined.
        int counter[128] = {0};
        for (auto & c : s)
            counter[c]++;
        for (auto & c : s)
            if (counter[c] == 1)
                return c;

        return ' ';
    }
};

Last updated

Was this helpful?