当前位置:网站首页>【LeetCode】387. 字符串中的第一个唯一字符
【LeetCode】387. 字符串中的第一个唯一字符
2022-06-24 07:07:00 【Uaena_An】
越做题越觉得自己是菜鸡

🧸读题
找到第一个,出现一次的字符,的下标。
🧸代码 O(N)
思路:用映射的方式查找,开一个数组记录每个字幕出现的次数,再找出数组中第一个是1的映射的字符!
class Solution {
public:
int firstUniqChar(string s) {
int countArr[26] = {
0 };
//统计次数
for (int i = 0; i < s.size(); ++i)
{
countArr[s[i] - 'a']++;
}
for (int j = 0; j < s.size();++j)
{
if (countArr[s[j] - 'a'] == 1)
{
return j;
}
}
return -1;
}
};
🧸解读代码
通过映射的方式解决问题int countArr[26] = { 0 };
题目说只有小写字母,所以映射的数组直接开26个即可!
并且初始化为0;
将s中的每个字符映射到,countArr所在的下标,countArr下标存的数字就是 这个字符出现的次数!
for (int i = 0; i < s.size(); ++i){
countArr[s[i] - 'a']++;}
i是countArr数组的下标,范围是s.size()
例如查找l,假设s[i]存储的是l,遍历字符数组
用'l' - 'a'等于11,也就是l映射到countArr的下标
所以在countArr[11]的位置上++,如果l出现两次则在countArr[11]的位置再++
for (int j = 0; j < s.size();++j){
if (countArr[s[j] - 'a'] == 1)
{
return j;
}}
这个循环是遍历
s字符串j是s字符串的下标,范围是s.size()
例如查找l,假设l的位置是s[j]
因为'l' - 'a'等于11,也就是l映射到countArr[]的下标,这个下标存的是l出现的次数!
所以如果countArr[s[j] - 'a'] == 1
那么s[j]中的j就是,只出现一次的l字符,的下标!
🧸我快写蒙圈了,希望你看懂了!!!
🧸其他大佬的解法
这个解法是O(N2)
遍历数组,同时在两头向中间找,如果都找到了并且下标相同,则这个字符在整个s中只出现一次,此时直接返回下标即可
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;
}
};
加油,祝你拿到心仪的offer!
边栏推荐
- Matlab camera calibrator camera calibration
- Shell pass parameters
- 2138. 将字符串拆分为若干长度为 k 的组
- Using skills of xargs -- the way to build a dream
- 剑指 Offer 55 - I. 二叉树的深度-dfs法
- 微博撰写-流程图-序列图-甘特图-mermaid流程图-效果不错
- 2022.06.23(LC_144,94,145_二叉树的前序、中序、后序遍历)
- 数据中台:数据治理概述
- Jenkins is deployed automatically and cannot connect to the dependent service [solved]
- 第七章 操作位和位串(三)
猜你喜欢

opencv最大值滤波(不局限于图像)

Telnet port login method with user name for liunx server

Pymysql inserts data into MySQL and reports an error for no reason

It is enough to read this article about ETL. Three minutes will let you understand what ETL is

What is SRE? A detailed explanation of SRE operation and maintenance system

数据中台:数据采集和抽取的技术栈详解
![打印出来的对象是[object object],解决方法](/img/fc/9199e26b827a1c6304fcd250f2301e.png)
打印出来的对象是[object object],解决方法

MySQL 因字符集问题插入中文数据时提示代码 :1366

【团队管理】测试团队绩效管理的25点小建议

Centos7 installation of jdk8, mysql5.7 and Navicat connection to virtual machine MySQL and solutions (solutions to MySQL download errors are attached)
随机推荐
How to handle the problem that calling easycvr address integration cannot be played through easyplayer player?
Camera projection matrix calculation
工具类
JS to find and update the specified value in the object through the key
Jenkins自动化部署,连接不到所依赖的服务【已解决】
Distributed | how to make "secret calls" with dble
“不平凡的代理初始值设定不受支持”,出现的原因及解决方法
图片工具
Rsync for file backup
[force deduction 10 days SQL introduction] Day3
基于QingCloud的 “房地一体” 云解决方案
【MySQL从入门到精通】【高级篇】(一)字符集的修改与底层原理
Xiaohei ai4code code baseline nibble 1
Smart power plant: how to make use of easycvr to build a safe, stable, green and environment-friendly intelligent inspection platform
Using skills of xargs -- the way to build a dream
How to mount a USB hard disk with NTFS file format under RHEL5 system
orb slam build bug: undefined reference to symbol ‘_ ZN5boost6system15system_ categoryEv‘
数据中台:数据中台技术架构详解
表单图片上传在Chorme中无法查看请求体的二进制图片信息
String to Base64