C++ | Leetcode C++题解之第387题字符串中的第一个唯一字符

题目:

题解:

cpp 复制代码
class Solution {
public:
    int firstUniqChar(string s) {
        unordered_map<char, int> position;
        queue<pair<char, int>> q;
        int n = s.size();
        for (int i = 0; i < n; ++i) {
            if (!position.count(s[i])) {
                position[s[i]] = i;
                q.emplace(s[i], i);
            }
            else {
                position[s[i]] = -1;
                while (!q.empty() && position[q.front().first] == -1) {
                    q.pop();
                }
            }
        }
        return q.empty() ? -1 : q.front().second;
    }
};
相关推荐
超爱笑嘻嘻19 分钟前
shared_ptr八股收集 C++
c++
我想进大厂38 分钟前
图论---朴素Prim(稠密图)
数据结构·c++·算法·图论
我想进大厂43 分钟前
图论---Bellman-Ford算法
数据结构·c++·算法·图论
光而不耀@lgy1 小时前
C++初登门槛
linux·开发语言·网络·c++·后端
啊丢_1 小时前
C++——Lambda表达式
开发语言·c++
Wendy_robot2 小时前
【滑动窗口+哈希表/数组记录】Leetcode 438. 找到字符串中所有字母异位词
c++·算法·leetcode
程序员-King.2 小时前
day49—双指针+贪心—验证回文串(LeetCode-680)
算法·leetcode·贪心算法·双指针
转基因3 小时前
Codeforces Round 1020 (Div. 3)(题解ABCDEF)
数据结构·c++·算法
whoarethenext3 小时前
c++的jsoncpp使用
开发语言·c++·jsoncpp
我想进大厂4 小时前
图论---Kruskal(稀疏图)
数据结构·c++·算法·图论