当前位置:网站首页>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;
}
如有错误,多多指教!
边栏推荐
- Selenium: Introduction
- 关于给Qt做一个软件初始化的进度条
- 2022年湖南工学院ACM集训第六次周测题解
- WPF项目-初步了解数据绑定 binding
- 小白的0基础教程SQL: 什么是SQL 01
- Induction jian hai JustFE 2022/07/29 team, I learned the efficient development summary (years)
- What should I do if the neural network cannot be trained?
- 曲柄滑块机构运动分析和参数优化
- pytorch、tensorflow对比学习—功能组件(优化器、评估指标、Module管理)
- Robot_Framework:关键字
猜你喜欢

WPF入门项目必知必会-初步了解数据绑定 binding

y83. Chapter 4 Prometheus Factory Monitoring System and Actual Combat -- Advanced Prometheus Alarm Mechanism (14)

leetcode125 验证回文串

pytroch、tensorflow对比学习—搭建模型范式(低阶、中阶、高阶API示例)

WPF项目-初步了解数据绑定 binding

matlab 风速模型 小波滤波

Robot_Framework:关键字

Induction jian hai JustFE 2022/07/29 team, I learned the efficient development summary (years)

Check控件

中国的机器人增长
随机推荐
LeetCode 387. 字符串中的第一个唯一字符
LeetCode 0149. 直线上最多的点数
Selenium: Dropdown Box Actions
About making a progress bar for software initialization for Qt
Robot_Framework:关键字
ModuleNotFoundError: No module named 'tensorflow.keras' error message solution
Selenium:操作Cookie
MySQL-Data Operation-Group Query-Join Query-Subquery-Pagination Query-Joint Query
state compressed dp
移动应用恶意攻击激增500% 三六零天御为APP免费构建安全屏障
说说js中使用for in遍历数组存在的bug
pytorch、tensorflow对比学习—功能组件(激活函数、模型层、损失函数)
pytroch、tensorflow对比学习—功能组件(数据管道、回调函数、特征列处理)
leetcode43 字符串相乘
华为Android开发面试后得出的面试秘诀
LeetCode 1189. “气球” 的最大数量
Selenium:简介
Selenium: element positioning
「游戏引擎 浅入浅出」4.1 Unity Shader和OpenGL Shader
pytroch、tensorflow对比学习—搭建模型范式(构建模型方法、训练模型范式)