当前位置:网站首页>字符中的第一个唯一字符
字符中的第一个唯一字符
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牛客多校四)N-Particle Arts(思维)

pytroch、tensorflow对比学习—功能组件(数据管道、回调函数、特征列处理)

(2022 Niu Ke Duo School IV) K-NIO's Sword (Thinking)

typescript28 - value of enumeration type and data enumeration

NDK does not contain any platforms problem solving

(2022牛客多校四)A-Task Computing (排序+动态规划)

Selenium:弹窗处理

pytorch、tensorflow对比学习—计算图和微分机制

The solution to the inconsistency between the PaddleX deployment inference model and the GUI interface test results

MySQL-数据定义语言-DDLdatebase define language
随机推荐
文件的异步读写
What should I do if the neural network cannot be trained?
typescript22-接口继承
Selenium: mouse, keyboard events
Selenium: Manipulating Cookies
Check控件
Selenium:表单切换
The sword refers to Offer 68 - I. Nearest Common Ancestor of Binary Search Trees
Selenium:元素定位
(2022 Nioke Duo School IV) H-Wall Builder II (Thinking)
力扣(LeetCode)212. 单词搜索 II(2022.07.31)
Power button (LeetCode) 212. The word search II (2022.07.31)
Selenium:鼠标、键盘事件
Swastika line-by-line parsing and realization of the Transformer, and German translation practice (a)
Logitech Mouse Experience Record
Selenium: element positioning
Immutable
The solution to the inconsistency between the PaddleX deployment inference model and the GUI interface test results
Typescript22 - interface inheritance
(Codeforce 757) E. Bash Plays with Functions