2351.第一个出现两次的字母
链接:2351.第一个出现两次的字母
难度:Easy
标签:位运算、哈希表、字符串、计数
简介:给你一个由小写英文字母组成的字符串 s ,请你找出并返回第一个出现 两次 的字母。
题解 1 - cpp
- 编辑时间:2023-01-01
- 内存消耗:6MB
- 编程语言:cpp
- 解法介绍:遍历。
class Solution {
public:
    char repeatedCharacter(string s) {
        int list[26] = {0};
        for (auto &c : s) {
            if (list[c - 'a']++ == 1) return c;
        }
        return ' ';
    }
};
题解 2 - rust
- 编辑时间:2023-01-01
- 内存消耗:2.3MB
- 编程语言:rust
- 解法介绍:遍历。
impl Solution {
    pub fn repeated_character(s: String) -> char {
        let s = s.as_bytes();
        let mut list = [0; 26];
        for c in s {
            let i = *c as usize - 'a' as usize;
            if list[i] == 1 {
                return *c as char;
            }
            list[i] += 1;
        }
        ' '
    }
}