LeetCode|387题|字符串中的第一个唯一字符
作者:互联网
数据结构和算法QQ交流群:600601067
LeetCode|387题|字符串中的第一个唯一字符
详细注释可执行代码
//
// Created by q on 2021/1/16.
//
#include <unordered_map>
#include <string>
#include <iostream>
using namespace std;
class Solution {
public:
int firstUniqChar(std::string s) {
std::unordered_map<int, int> frequency;
for (char ch: s) {
++frequency[ch];//先赋值再自增
}
for (int i = 0; i < s.size(); ++i) {
if (frequency[s[i]] == 1) {//获取每个字符串的频率值
return i;
}
}
return -1;
}
};
std::string s = "xiaoqiuslambiji";
int main(){
Solution solution;
cout << solution.firstUniqChar(s) << endl;
return 0;
}
CMakeLists.txt编译文件
cmake_minimum_required(VERSION 2.8)
project(week9)
set(CMAKE_CXX_STANDARD 11)
add_executable(firstUniqChar firstUniqChar.cpp)
标签:std,int,firstUniqChar,frequency,387,字符串,include,LeetCode 来源: https://blog.csdn.net/qq_21950671/article/details/112696319