当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢

数组问题之《两数之和》以及《三数之和 》

typescript21-接口和类型别名的对比

Pyspark机器学习:向量及其常用操作

Message queue design based on mysql

High Numbers | 【Re-integration】Line Area Score 880 Examples

typescript27-枚举类型呢

56:第五章:开发admin管理服务:9:开发【文件上传到,MongoDB的GridFS中,接口】;(把文件上传到GridFS的SOP)

产品经理访谈 | 第五代验证码的创新与背景

【愚公系列】2022年07月 Go教学课程 024-函数

PAT乙级 1002 写出这个数
随机推荐
typescript22-接口继承
初识shell脚本
Advice given by experts with four years of development experience in Flutter tutorial
认真对待每一个时刻
Progressive Reconstruction of Visual Structure for Image Inpainting 论文笔记
【云原生之kubernetes实战】kubernetes集群的检测工具——popeye
Li Chi's work and life summary in July 2022
The Principle Of Percona Toolkit Nibble Algorithm
The difference between scheduleWithFixedDelay and scheduleAtFixedRate
ModuleNotFoundError: No module named ‘tensorflow.keras‘报错信息的解决方法
怀念故乡的面条
(2022牛客多校四)D-Jobs (Easy Version)(三维前缀或)
Simulation of Active anti-islanding-AFD Active Anti-islanding Model Based on Simulink
SQL Analysis of ShardingSphere
TIM登陆时提示00001(TIM00001)
PMP 项目沟通管理
基于Arduino制作非接触式测温仪
Optional parameters typescript19 - object
【堆】小红的数组
25. Have you been asked these three common interview questions?