当前位置:网站首页>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;
}
};
边栏推荐
- RSA主要攻击方法
- API Design Notes: The pimpl trick
- Passive anti-islanding-UVP/OVP and UFP/OFP passive anti-islanding model simulation based on simulink
- 数组问题之《两数之和》以及《三数之和 》
- PAT乙级 1002 写出这个数
- 【目标检测】YOLOv7理论简介+实践测试
- 博客系统(完整版)
- typescript27-枚举类型呢
- 报错:AttributeError: module ‘matplotlib’ has no attribute ‘figure’
- safari浏览器怎么导入书签
猜你喜欢
随机推荐
Immutable
Flink 1.13 (8) CDC
leetcode:126. Word Solitaire II
万字逐行解析与实现Transformer,并进行德译英实战(二)
万字逐行解析与实现Transformer,并进行德译英实战(一)
开源许可证 GPL、BSD、MIT、Mozilla、Apache和LGPL的区别
剑指 Offer 68 - II. 二叉树的最近公共祖先
在沈自所的半年总结
UE4 rays flashed from mouse position detection
What is a programming language
The difference between scheduleWithFixedDelay and scheduleAtFixedRate
博客系统(完整版)
【愚公系列】2022年07月 Go教学课程 023-Go容器之列表
Optional parameters typescript19 - object
6-23漏洞利用-postgresql代码执行利用
[FPGA tutorial case 43] Image case 3 - image sobel edge extraction through verilog, auxiliary verification through MATLAB
UE4 模型OnClick事件不生效的两种原因
typescript25-类型断言
初识shell脚本
MySQL-DML语言-数据库操作语言-insert-update-delete-truncate









