当前位置:网站首页>字符中的第一个唯一字符
字符中的第一个唯一字符
2022-08-01 05:11:00 【老鱼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;
}
//找到只出现一次是数字
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;//记录字符和字符出现的次数
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;
}
如有错误,多多指教!
边栏推荐
- 2022/07/29 入职健海JustFE团队,我学到了高效开发(年中总结)
- WPF入门项目必知必会-初步了解数据绑定 binding
- Check控件
- Code Interview Guide for Programmers CD15 Generating an Array of Windowed Maximums
- 混合型界面:对话式UI的未来
- y83.第四章 Prometheus大厂监控体系及实战 -- prometheus告警机制进阶(十四)
- PaddleX部署推理模型和GUI界面测试结果不一致的解决方法
- SL-12/2过流继电器
- Logitech Mouse Experience Record
- ModuleNotFoundError: No module named 'tensorflow.keras' error message solution
猜你喜欢

USB3.0:VL817Q7-C0的LAYOUT指南(三)

Risk strategy important steps of tuning method

可视化全链路日志追踪

PAT serie b write the number 1002

Robot_Framework:关键字

移动应用恶意攻击激增500% 三六零天御为APP免费构建安全屏障

pytroch、tensorflow对比学习—专栏介绍

MySQL-数据操作-分组查询-连接查询-子查询-分页查询-联合查询

High Numbers | 【Re-integration】Line Area Score 880 Examples

(2022 Nioke Duo School IV) D-Jobs (Easy Version) (3D prefix or)
随机推荐
Selenium: Popup Handling
Selenium:元素判断
Code Interview Guide for Programmers CD15 Generating an Array of Windowed Maximums
25. 这三道常见的面试题,你有被问过吗?
挑战52天背完小猪佩奇(第01天)
Selenium: JS operation
Selenium:鼠标、键盘事件
25. Have you been asked these three common interview questions?
A,H,K,N
mysql中解决存储过程表名通过变量传递的方法
PaddleX部署推理模型和GUI界面测试结果不一致的解决方法
Robot_Framework:断言
API设计笔记:pimpl技巧
李迟2022年7月工作生活总结
II. Binary tree to Offer 68 - recent common ancestor
Selenium:操作Cookie
ModuleNotFoundError: No module named 'tensorflow.keras' error message solution
MySQL-DML语言-数据库操作语言-insert-update-delete-truncate
牛客多校2022第四场A,H,K,N
冲刺金九银十,Android开发面试(内含面试资料|面试题|源码)