公众号首图制作网站,关于网站集约化建设的意见,西安百度竞价托管代运营,网站建设实验周志与总结1. 题目
在字符串 s 中找出第一个只出现一次的字符。如果没有#xff0c;返回一个单空格。
示例:
s abaccdeff
返回 bs
返回 限制#xff1a;
0 s 的长度 50000来源#xff1a;力扣#xff08;LeetCode返回一个单空格。
示例:
s abaccdeff
返回 bs
返回 限制
0 s 的长度 50000来源力扣LeetCode 链接https://leetcode-cn.com/problems/di-yi-ge-zhi-chu-xian-yi-ci-de-zi-fu-lcof 著作权归领扣网络所有。商业转载请联系官方授权非商业转载请注明出处。
2. 解题
class Solution {
public:char firstUniqChar(string s) {unordered_mapchar,int m;for(int i 0; i s.size(); i){if(m.count(s[i]))m[s[i]] -1;//标记为-1表示出现多次elsem[s[i]] i;//存储位置}int idx INT_MAX;char ans ;for(auto mi : m){if(mi.second ! -1 mi.second idx){idx mi.second;ans mi.first;}}return ans;}
};class Solution {
public:char firstUniqChar(string s) {vectorint count(128,-1);for(int i 0; i s.size(); i){if(count[s[i]] -1)//-1表示没有出现count[s[i]] i;//存储位置else//出现过count[s[i]] -2;//表示重复}char ans ;int idx INT_MAX;for(int i 0; i 128; i)if(count[i] ! -1 count[i] ! -2 count[i] idx){ans i;idx count[i];}return ans;}
};