当前位置:网站首页>【LeetCode】387. First unique character in string
【LeetCode】387. First unique character in string
2022-06-24 08:59:00 【Uaena_ An】
The more you do the questions, the more you feel like a vegetable chicken

🧸 Reading questions
Find the first , The character that appears once , The subscript .
🧸 Code O(N)
Ideas : Find by mapping , Open an array to record the number of times each caption appears , Then find out that the first one in the array is 1 Mapped characters for !
class Solution {
public:
int firstUniqChar(string s) {
int countArr[26] = {
0 };
// Count the times
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;
}
};
🧸 Decoding code
Solve the problem by mapping int countArr[26] = { 0 };
The title says there are only lowercase letters , So the mapped array is opened directly 26 Just one !
And initialized to0;
takesEach character in maps to ,countArrThe subscript ,countArrThe number stored in the subscript is The number of times this character appears !
for (int i = 0; i < s.size(); ++i){
countArr[s[i] - 'a']++;}
iyescountArrIndex of the array , The scope iss.size()
For example, findl, hypothesiss[i]What's stored isl, Traversal character array
use'l' - 'a'be equal to11, That is to saylMapping tocountArrThe subscript
So incountArr[11]Location++, IflAppear twice incountArr[11]The position of++
for (int j = 0; j < s.size();++j){
if (countArr[s[j] - 'a'] == 1)
{
return j;
}}
This loop is traversal
scharacter stringjyessThe subscript of a string , The scope iss.size()
For example, findl, hypothesislThe position iss[j]
because'l' - 'a'be equal to11, That is to saylMapping tocountArr[]The subscript , This subscript storeslNumber of occurrences !
So ifcountArr[s[j] - 'a'] == 1
thats[j]MediumjNamely , Only oncelcharacter , The subscript !
🧸 I'm almost writing circles , I hope you understand !!!
🧸 The solution of other big guys
The solution is O(N2)
Traversal array , Look in the middle at both ends , If both are found and the subscripts are the same , Then this character is in the whole s Only once in a while , In this case, you can directly return the subscript
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;
}
};
come on. , I wish you get what you want offer!
边栏推荐
- 110. 平衡二叉树-递归法
- 【牛客】HJ1 字符串最后一个单词的长度
- Double pointer analog
- YOLOX backbone——CSPDarknet的实现
- Data middle office: the data middle office practice scheme of Minsheng Bank
- 陆奇:我现在最看好这四大技术趋势
- 数据中台:中台实践与总结
- Data middle office: detailed explanation of technical architecture of data middle office
- Qingcloud based R & D cloud solution for geographic information enterprises
- “论解不了数独所以选择做个数独游戏这件事”
猜你喜欢

A tip to read on Medium for free

MySQL | store notes of Master Kong MySQL from introduction to advanced

MySQL | 视图《康师傅MySQL从入门到高级》笔记

What is SRE? A detailed explanation of SRE operation and maintenance system
![打印出来的对象是[object object],解决方法](/img/fc/9199e26b827a1c6304fcd250f2301e.png)
打印出来的对象是[object object],解决方法

关于 GIN 的路由树

I heard that you are still spending money to buy ppt templates from the Internet?

什么是图神经网络?图神经网络有什么用?

【量化投资】离散傅里叶变换求数组周期

YOLOX backbone——CSPDarknet的实现
随机推荐
MySQL | view notes on Master Kong MySQL from introduction to advanced
陆奇:我现在最看好这四大技术趋势
Essay - Reflection
【NOI模拟赛】寄(树形DP)
Database migration from PostgreSQL to MySQL
linux(centos7.9)安装部署mysql-cluster 7.6
input的聚焦后的边框问题
Analyze the meaning of Internet advertising terms CPM, CPC, CPA, CPS, CPL and CPR
520. 检测大写字母
OpenCV每日函数 结构分析和形状描述符(7) 寻找多边形(轮廓)/旋转矩形交集
1704. 判断字符串的两半是否相似
520. detect capital letters
PM2 deploy nuxt3 JS project
Jenkins is deployed automatically and cannot connect to the dependent service [solved]
How to configure environment variables and distinguish environment packaging for multi terminal project of uniapp development
MySQL——SQL语句
4275. Dijkstra sequence
every()、map()、forEarch()方法。数组里面有对象的情况
opencv最大值滤波(不局限于图像)
[pytoch basic tutorial 31] youtubednn model analysis