当前位置:网站首页>first unique character in characters
first unique character in characters
2022-08-01 05:31:00 【old fish 37】

class Solution {
public:
int firstUniqChar(string s) {
int count[256]={
0};
int size=s.size();
//记录每个数字出现的次数
for(int i=0;i<size;i++)
{
count[s[i]]+=1;
}
//Found only once is digital
for(int j=0;j<size;j++)
{
if(1==count[s[j]])
{
return j;
}
}
return -1;
}
};
解法二:
class Solution {
public:
int firstUniqChar(string s) {
for(int i=0;i<s.size();i++)
{
if(s.find(s[i])==s.rfind(s[i]))
{
return i;
}
}
return -1;
}
};
解法三:利用哈希表
class Solution {
public:
int firstUniqChar(string s) {
//创建一个哈希表
map<char,int>v;//Record the number of characters and character appear
for(int i=0;i<s.size();i++)
{
++v[s[i]];
}
map<char,int>::const_iterator it=v.begin();
for(int i=0;i<s.size();i++)
{
if(v[s[i]]==1)
{
return i;
}
}
return -1;
}
如有错误,多多指教!
边栏推荐
- pytroch、tensorflow对比学习—搭建模型范式(低阶、中阶、高阶API示例)
- (2022 Niu Ke Duo School IV) N-Particle Arts (Thinking)
- leetcode125 验证回文串
- 将CSV文件快速导入MySQL中
- Selenium: mouse, keyboard events
- 【翻译】确保云原生通信的安全:从入口到服务网及更远的地方
- A,H,K,N
- Selenium:操作Cookie
- The sword refers to Offer 68 - I. Nearest Common Ancestor of Binary Search Trees
- 用位运算为你的程序加速
猜你喜欢
随机推荐
Selenium:弹窗处理
图片更新之后Glide加载依旧是原来的图片问题
曲柄滑块机构运动分析和参数优化
混合型界面:对话式UI的未来
Robot_Framework: Assertion
中国的机器人增长
Asynchronous reading and writing of files
说说js中使用for in遍历数组存在的bug
第5章——以程序方式处理MySQL数据表的数据
pytorch、tensorflow对比学习—张量
Check控件
Vsce package after the Command failed: NPM list - production - parseable - the depth = 99999 - loglevel = error exception
pytroch、tensorflow对比学习—功能组件(数据管道、回调函数、特征列处理)
Hunan institute of technology in 2022 ACM training sixth week antithesis
Robot_Framework: keyword
MySQL-DML language-database operation language-insert-update-delete-truncate
PAT serie b write the number 1002
戴尔PowerEdge服务器R450 RAID配置步骤
(2022 Niu Ke Duo School IV) N-Particle Arts (Thinking)
About making a progress bar for software initialization for Qt








