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;
    }
};
相关推荐
薰衣草233313 分钟前
一天两道力扣(6)
算法·leetcode
逝雪Yuki14 分钟前
Leetcode——287. 寻找重复数
c++·leetcode·二分查找·双指针·环形链表
科大饭桶1 小时前
数据结构自学Day13 -- 快速排序--“前后指针法”
数据结构·算法·leetcode·排序算法·c
李永奉2 小时前
C语言-流程控制语句:for循环语句、while和do…while循环语句;
c语言·开发语言·c++·算法
打码农的篮球2 小时前
STL——list
开发语言·c++·list
C++ 老炮儿的技术栈3 小时前
在 Scintilla 中为 Squirrel 语言设置语法解析器的方法
linux·运维·c++·git·ubuntu·github·visual studio
@蓝莓果粒茶3 小时前
LeetCode第350题_两个数组的交集II
c++·python·学习·算法·leetcode·职场和发展·c#
设计师小聂!3 小时前
力扣热题100----------53最大子数组和
java·数据结构·算法·leetcode
艾莉丝努力练剑5 小时前
【LeetCode&数据结构】二叉树的应用(二)——二叉树的前序遍历问题、二叉树的中序遍历问题、二叉树的后序遍历问题详解
c语言·开发语言·数据结构·学习·算法·leetcode·链表
YuTaoShao5 小时前
【LeetCode 热题 100】51. N 皇后——回溯
java·算法·leetcode·职场和发展