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;
    }
};
相关推荐
nju_spy2 小时前
力扣每日一题(二)任务安排问题 + 区间变换问题 + 排列组合数学推式子
算法·leetcode·二分查找·贪心·排列组合·容斥原理·最大堆
代码对我眨眼睛2 小时前
226. 翻转二叉树 LeetCode 热题 HOT 100
算法·leetcode·职场和发展
黑色的山岗在沉睡3 小时前
LeetCode 494. 目标和
算法·leetcode·职场和发展
Predestination王瀞潞5 小时前
IO操作(Num22)
开发语言·c++
宋恩淇要努力6 小时前
C++继承
开发语言·c++
江公望8 小时前
Qt qmlRegisterSingletonType()函数浅谈
c++·qt
逆小舟10 小时前
【C/C++】指针
c语言·c++·笔记·学习
江公望10 小时前
Qt QtConcurrent使用入门浅解
c++·qt·qml
我是华为OD~HR~栗栗呀10 小时前
23届考研-Java面经(华为OD)
java·c++·python·华为od·华为·面试
爱吃喵的鲤鱼10 小时前
仿mudou——Connection模块(连接管理)
linux·运维·服务器·开发语言·网络·c++