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;
    }
};
相关推荐
元亓亓亓20 小时前
LeetCode热题100--152. 乘积最大子数组--中等
算法·leetcode·职场和发展
bkspiderx21 小时前
C++变量生命周期:从创建到销毁的完整旅程
c++·生命周期·作用域·变量生命周期
梭七y21 小时前
【力扣hot100题】(103)移动零
数据结构·算法·leetcode
T0uken1 天前
现代 C++ 项目的 CMake 工程组织
c++
Jeremy爱编码1 天前
leetcode热题腐烂的橘子
算法·leetcode·职场和发展
H CHY1 天前
C++代码
c语言·开发语言·数据结构·c++·算法·青少年编程
alphaTao1 天前
LeetCode 每日一题 2025/12/22-2025/12/28
算法·leetcode
xiaolang_8616_wjl1 天前
c++题目_传桶(改编于atcoder(题目:Heavy Buckets))
数据结构·c++·算法
小小8程序员1 天前
除了 gcc/g++,还有哪些常用的 C/C++ 编译器?
c语言·开发语言·c++
希望_睿智1 天前
实战设计模式之中介者模式
c++·设计模式·架构