当前位置:网站首页>【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!
边栏推荐
- rsync做文件备份
- 520. 检测大写字母
- 提高INSERT速度
- 数据中台:民生银行的数据中台实践方案
- Wan Weiwei, a researcher from Osaka University, Japan, introduced the rapid integration method and application of robot based on WRS system
- Background management of uniapp hot update
- Database migration from PostgreSQL to MySQL
- 表单图片上传在Chorme中无法查看请求体的二进制图片信息
- Prompt code when MySQL inserts Chinese data due to character set problems: 1366
- 微博撰写-流程图-序列图-甘特图-mermaid流程图-效果不错
猜你喜欢
一文详解|增长那些事儿
rpiplay实现树莓派AirPlay投屏器
ZUCC_ Principles of compiling language and compilation_ Experiment 0607 grammar analysis ll analysis
The form image uploaded in chorme cannot view the binary image information of the request body
基于QingCloud的 “房地一体” 云解决方案
Telnet port login method with user name for liunx server
原生小程序用画布制作海报,等比例缩放,和uniapp差不多就是写法有点不同
À propos de ETL il suffit de lire cet article, trois minutes pour vous faire comprendre ce qu'est ETL
“不平凡的代理初始值设定不受支持”,出现的原因及解决方法
数据中台:中台架构及概述
随机推荐
orb slam build bug: undefined reference to symbol ‘_ ZN5boost6system15system_ categoryEv‘
Xiaohei ai4code code baseline nibble 1
Increase insert speed
Base64编码详解及其变种(解决加号在URL变空格问题)
One article explains in detail | those things about growth
Centos7 installation of jdk8, mysql5.7 and Navicat connection to virtual machine MySQL and solutions (solutions to MySQL download errors are attached)
2138. 将字符串拆分为若干长度为 k 的组
疫情、失业,2022,我们高喊着摆烂和躺平!
为什么ping不通,而traceroute却可以通
A tip to read on Medium for free
Get screen width and height tool class
偶然间得到的framework工具类 自用
Shell basic operators -- relational operators
Earthly container image construction tool -- the road to dream
工具类
How to mount a USB hard disk with NTFS file format under RHEL5 system
Earthly 容器镜像构建工具 —— 筑梦之路
Battle history between redis and me under billion level traffic
Shell pass parameters
Liunx change the port number of vsftpd