当前位置:网站首页>字符中的第一个唯一字符
字符中的第一个唯一字符
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;
}
如有错误,多多指教!
边栏推荐
- Selenium:操作JS
- Use controls as brushes to get bitmap code records
- 挑战52天背完小猪佩奇(第01天)
- I met a shell script
- MySQL-DML language-database operation language-insert-update-delete-truncate
- 律师解读 | 枪炮还是玫瑰?从大厂之争谈元宇宙互操作性
- (2022牛客多校四)A-Task Computing (排序+动态规划)
- typescript25 - type assertion
- pytorch、tensorflow对比学习—张量
- NDK does not contain any platforms problem solving
猜你喜欢
pytroch、tensorflow对比学习—使用GPU训练模型
typescript21-接口和类型别名的对比
剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
(more than 2022 cattle school four) A - Task Computing + dynamic programming (sort)
UE4 从鼠标位置射出射线检测
类神经网络训练不起来怎么办
MySQL-DML语言-数据库操作语言-insert-update-delete-truncate
typescript25 - type assertion
Power button (LeetCode) 212. The word search II (2022.07.31)
Selenium:弹窗处理
随机推荐
Malicious attacks on mobile applications surge by 500%
MySQL Practice Summary -
UE4 制作遇到的问题
Selenium:操作JS
万字逐行解析与实现Transformer,并进行德译英实战(三)
MySQL-Data Definition Language-DDLdatebase define language
USB3.0:VL817Q7-C0的LAYOUT指南(三)
pytroch、tensorflow对比学习—使用GPU训练模型
混合型界面:对话式UI的未来
JWL-11/2-99.9A电流继电器
4D line-by-line analysis and implementation of Transformer, and German translation into English (3)
Selenium:表单切换
Robot_Framework: Assertion
pytroch、tensorflow对比学习—搭建模型范式(构建模型方法、训练模型范式)
(2022牛客多校四)D-Jobs (Easy Version)(三维前缀或)
II. Binary tree to Offer 68 - recent common ancestor
Li Chi's work and life summary in July 2022
2022/07/29 入职健海JustFE团队,我学到了高效开发(年中总结)
Power button (LeetCode) 212. The word search II (2022.07.31)
LeetCode 1189. “气球” 的最大数量