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;
    }
};
相关推荐
Word码9 小时前
[C++语法] 继承 (用法详解)
java·jvm·c++
@––––––9 小时前
力扣hot100—系列2-多维动态规划
算法·leetcode·动态规划
lxl13079 小时前
C++算法(1)双指针
开发语言·c++
淀粉肠kk9 小时前
C++11列表初始化:{}的革命性进化
c++
zhooyu10 小时前
C++和OpenGL手搓3D游戏编程(20160207进展和效果)
开发语言·c++·游戏·3d·opengl
HAPPY酷10 小时前
C++ 和 Python 的“容器”对决:从万金油到核武器
开发语言·c++·python
YuTaoShao11 小时前
【LeetCode 每日一题】1653. 使字符串平衡的最少删除次数——(解法三)DP 空间优化
算法·leetcode·职场和发展
茉莉玫瑰花茶11 小时前
C++ 17 详细特性解析(5)
开发语言·c++·算法
cpp_250111 小时前
P10570 [JRKSJ R8] 网球
数据结构·c++·算法·题解
cpp_250111 小时前
P8377 [PFOI Round1] 暴龙的火锅
数据结构·c++·算法·题解·洛谷