当前位置:网站首页>Leetcode daily question: the first unique character in the string
Leetcode daily question: the first unique character in the string
2022-07-05 17:48:00 【Sharp blade CC】
link : The first unique character in the string
( notes : The prompt string of this question only contains lowercase letters )
Conventional thinking : Each character is traversed once , Find out whether there are the same letters in the string .( But in the worst case, the time complexity is O(n^2), So... Is not recommended here )
Code :
class Solution {
public:
int firstUniqChar(string s) {
// Conventional thinking : Each time, we iterate to find whether there are equal , Time complexity O(n^2), Not recommended
for(size_t i = 0; i < s.size(); i++)
{
// use flag Mark with , If it becomes 0 The description has the same
int flag = 1;
for(size_t j = 0; j < s.size(); j++)
{
// If the subscripts are the same, no comparison is required
if(j == i)
continue;
if(s[j] != s[i])
continue;
else
{
flag = 0;
break;
}
}
if(flag == 1)
return i;
}
return -1;
}
};
Another way to think about it : Using the idea of counting and sorting , Count the number of occurrences of each letter in the string , Then add the first count array as 1 Letter return of .
notes : The time complexity of this idea is O(N).
Code :
class Solution {
public:
int firstUniqChar(string s) {
// The time complexity is O(n)
// Using the idea of counting and sorting , First count the number of times each letter appears
int arr[26] = {
0};
for(size_t i = 0; i < s.size(); i++)
{
arr[s[i] - 'a']++;
}
// lookup s In the corresponding arr Whether the number of occurrences is once
for(size_t i = 0; i < s.size(); i++)
{
if(arr[s[i] - 'a'] == 1)
return i;
}
return -1;
}
};
边栏推荐
- Complete solution instance of Oracle shrink table space
- Abnormal recovery of virtual machine Oracle -- Xi Fenfei
- Disabling and enabling inspections pycharm
- Cartoon: looking for the k-th element of an unordered array (Revised)
- Cloud security daily 220705: the red hat PHP interpreter has found a vulnerability of executing arbitrary code, which needs to be upgraded as soon as possible
- Check the WiFi password connected to your computer
- flask接口响应中的中文乱码(unicode)处理
- 为什么阳历中平年二月是28天
- GFS分布式文件系统
- Cmake tutorial Step2 (add Library)
猜你喜欢
2022新版PMP考试有哪些变化?
Cmake tutorial Step4 (installation and testing)
CVPR 2022 best student paper: single image estimation object pose estimation in 3D space
ICML 2022 | Meta提出鲁棒的多目标贝叶斯优化方法,有效应对输入噪声
Why is all (()) true and any (()) false?
Redis基础
MySQL之知识点(七)
求解为啥all(())是True, 而any(())是FALSE?
论文阅读_中文NLP_LTP
Anaconda中配置PyTorch环境——win10系统(小白包会)
随机推荐
ELK日志分析系统
读libco保存恢复现场汇编代码
Vulnerability recurrence - 48. Command injection in airflow DAG (cve-2020-11978)
Cartoon: how to multiply large integers? (I) revised version
世界上最难的5种编程语言
Beijing internal promotion | the machine learning group of Microsoft Research Asia recruits full-time researchers in nlp/ speech synthesis and other directions
Action avant ou après l'enregistrement du message teamcenter
Check the WiFi password connected to your computer
Simple query cost estimation
Accuracy of BigDecimal Division
Interpretation: how to deal with the current security problems faced by the Internet of things?
漫画:如何实现大整数相乘?(下)
GFS分布式文件系统
Database design in multi tenant mode
如何修改mysql字段为自增长字段
漏洞复现----48、Airflow dag中的命令注入(CVE-2020-11978)
Kafaka技术第一课
北京内推 | 微软亚洲研究院机器学习组招聘NLP/语音合成等方向全职研究员
Why is all (()) true and any (()) false?
毫无章法系列