当前位置:网站首页>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;
}
如有错误,多多指教!
边栏推荐
- II. Binary tree to Offer 68 - recent common ancestor
- The solution to the inconsistency between the PaddleX deployment inference model and the GUI interface test results
- torch
- ModuleNotFoundError: No module named 'tensorflow.keras' error message solution
- 微信小程序获取手机号phonenumber.getPhoneNumber接口开发
- pytroch、tensorflow对比学习—功能组件(数据管道、回调函数、特征列处理)
- 小白的0基础教程SQL: 关系数据库概述 02
- Selenium:上传、下载文件
- Vsce package after the Command failed: NPM list - production - parseable - the depth = 99999 - loglevel = error exception
- [Translation] Securing cloud-native communications: From ingress to service mesh and beyond
猜你喜欢
pytorch、tensorflow对比学习—功能组件(优化器、评估指标、Module管理)
声音信号处理基频检测和时频分析
pytroch、tensorflow对比学习—功能组件(数据管道、回调函数、特征列处理)
SL-12/2过流继电器
Selenium: Manipulating Cookies
MySQL-DML language-database operation language-insert-update-delete-truncate
AspNet.WebApi.Owin 自定义Token请求参数
备战金九银十,如何顺利通过互联网大厂Android的笔面试?
The solution to the inconsistency between the PaddleX deployment inference model and the GUI interface test results
The sword refers to Offer 68 - I. Nearest Common Ancestor of Binary Search Trees
随机推荐
II. Binary tree to Offer 68 - recent common ancestor
torch
「游戏引擎 浅入浅出」4.1 Unity Shader和OpenGL Shader
牛客多校2022第四场A,H,K,N
LeetCode 0149. 直线上最多的点数
Flip letters using string container
Robot_Framework: Assertion
LeetCode 27. 移除元素
Dialogue with the father of MySQL: One excellent programmer is worth 5 ordinary programmers
Selenium:表单切换
冲刺金九银十,Android开发面试(内含面试资料|面试题|源码)
LeetCode 0150. 逆波兰表达式求值
Check控件
字符中的第一个唯一字符
leetcode125 验证回文串
uva12326
使用string 容器翻转 字母
PAT serie b write the number 1002
NUMPY
Selenium:元素定位