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;
    }
};
相关推荐
「QT(C++)开发工程师」9 小时前
C++ 11 常用for循环
开发语言·c++
我还记得那天9 小时前
0 初识C++
开发语言·c++
「QT(C++)开发工程师」11 小时前
C++ std::move 详解
开发语言·c++
玖玥拾12 小时前
LeetCode 392 判断子序列
笔记·算法·leetcode
豆沙沙包?13 小时前
C++~~~set容器、map容器(p57-P69)
开发语言·c++
qeen8714 小时前
【数据结构】哈希表的C++实现与封装
数据结构·c++·散列表·哈希表
重生之后端学习14 小时前
239. 滑动窗口最大值[困难]✅
java·数据结构·算法·leetcode·职场和发展
乐观勇敢坚强的老彭15 小时前
C++信奥GESP四级的常见题型和解题模板速查表
开发语言·c++
欧特克_Glodon16 小时前
OpenCV计算机视觉开发入门与实践<二十二>:图像平滑之线性滤波
c++·人工智能·opencv·计算机视觉