当前位置:网站首页>LeetCode 387. 字符串中的第一个唯一字符
LeetCode 387. 字符串中的第一个唯一字符
2022-08-01 04:49:00 【PUdd】
我的思路
建立26个字母的数组character[26],如a出现一次则character[0]==1;b出现则character[1]==1;a再出现一次则character[0]==2;
遍历过s后,只需再从头找一遍每个字母对应character数组中的出现次数,小于2就直接返回,如果全部找过一遍没有那就直接返回-1了。
代码
class Solution {
public:
int firstUniqChar(string s)
{
int character[26]={
0};
for(int i=0;i<s.length();i++)
{
character[(s[i]-97)]+=1;
}
for(int i=0;i<s.length();i++)
{
if(character[(s[i]-97)]<2 ) return i;
}
return -1;
}
};
边栏推荐
猜你喜欢
TypeScript simplifies running ts-node
【愚公系列】2022年07月 Go教学课程 024-函数
7月编程排行榜来啦!这次有何新变化?
Flink 1.13 (8) CDC
【愚公系列】2022年07月 Go教学课程 025-递归函数
认真对待每一个时刻
The method of solving stored procedure table name passing through variable in mysql
typescript23-元组
【kali-信息收集】枚举——DNS枚举:DNSenum、fierce
JS new fun(); class and instance JS is based on object language Can only act as a class by writing constructors
随机推荐
Valentine's Day Romantic 3D Photo Wall [with source code]
力扣(LeetCode)212. 单词搜索 II(2022.07.31)
程序员代码面试指南 CD15 生成窗口最大值数组
在沈自所的半年总结
一个往年的朋友
基于Arduino制作非接触式测温仪
基于ProXmoX VE的虚拟化家庭服务器(篇一)—ProXmoX VE 安装及基础配置
MySQL3
请问shake数据库中为什么读取100个collection 后,直接就退出了,不继续读了呢?
This article takes you to understand the past and present of Mimir, Grafana's latest open source project
Input input box cursor automatically jumps to the last bug after the previous input
UE4 模型OnClick事件不生效的两种原因
EntityFramework saves to SQLServer decimal precision is lost
typescript27 - what about enumeration types
How to write a high-quality digital good article recommendation
Lawyer Interpretation | Guns or Roses?Talking about Metaverse Interoperability from the Battle of Big Manufacturers
[FPGA tutorial case 43] Image case 3 - image sobel edge extraction through verilog, auxiliary verification through MATLAB
【云原生之kubernetes实战】kubernetes集群的检测工具——popeye
PMP工具与技术总结
IJCAI2022 | Hybrid Probabilistic Reasoning with Algebraic and Logical Constraints