当前位置:网站首页>leetcode每日一题:字符串中的第一个唯一字符
leetcode每日一题:字符串中的第一个唯一字符
2022-07-05 17:17:00 【利刃Cc】

链接:字符串中的第一个唯一字符
(注:该题提示字符串只包含小写字母)
常规思路:对每个字符都进行一次遍历,查找是否字符串中是否有相同的字母。(但是最坏情况下时间复杂度为O(n^2),所以这里不推荐)
代码:
class Solution {
public:
int firstUniqChar(string s) {
//常规思路:每次都遍历查找是否有相等的,时间复杂度O(n^2),不推荐
for(size_t i = 0; i < s.size(); i++)
{
//用flag用做标记,若变为0说明有相同的
int flag = 1;
for(size_t j = 0; j < s.size(); j++)
{
//若下标相同则不需要对比
if(j == i)
continue;
if(s[j] != s[i])
continue;
else
{
flag = 0;
break;
}
}
if(flag == 1)
return i;
}
return -1;
}
};
另一种思路: 使用计数排序的思想,统计字符串中每个字母出现的次数,然后再将第一个计数数组中为1的字母返回。
注:该思路的时间复杂度为O(N)。
代码:
class Solution {
public:
int firstUniqChar(string s) {
// 时间复杂度为O(n)
// 利用计数排序的思想,先统计每个字母出现的次数
int arr[26] = {
0};
for(size_t i = 0; i < s.size(); i++)
{
arr[s[i] - 'a']++;
}
//查找s中对应arr出现的次数是否为一次
for(size_t i = 0; i < s.size(); i++)
{
if(arr[s[i] - 'a'] == 1)
return i;
}
return -1;
}
};

边栏推荐
- [7.7 live broadcast preview] the lecturer of "typical architecture of SaaS cloud native applications" teaches you to easily build cloud native SaaS applications. Once the problem is solved, Huawei's s
- Compter le temps d'exécution du programme PHP et définir le temps d'exécution maximum de PHP
- Humi analysis: the integrated application of industrial Internet identity analysis and enterprise information system
- WR | Jufeng group of West Lake University revealed the impact of microplastics pollution on the flora and denitrification function of constructed wetlands
- MySql 查询符合条件的最新数据行
- Ant financial's sudden wealth has not yet begun, but the myth of zoom continues!
- Cmake tutorial step6 (add custom commands and generate files)
- Cartoon: a bloody case caused by a math problem
- Flask solves the problem of CORS err
- C#(Winform) 当前线程不在单线程单元中,因此无法实例化 ActiveX 控件
猜你喜欢
mysql中取出json字段的小技巧

漏洞复现----48、Airflow dag中的命令注入(CVE-2020-11978)

Knowledge points of MySQL (6)

IDC报告:腾讯云数据库稳居关系型数据库市场TOP 2!

Which is more cost-effective, haqu K1 or haqu H1? Who is more worth starting with?
In depth understanding of redis memory obsolescence strategy

33: Chapter 3: develop pass service: 16: use redis to cache user information; (to reduce the pressure on the database)
Complete solution instance of Oracle shrink table space

mongodb(快速上手)(一)

提高應用程序性能的7個DevOps實踐
随机推荐
机器学习02:模型评估
ThoughtWorks global CTO: build the architecture according to needs, and excessive engineering will only "waste people and money"
Rider set the highlighted side of the selected word, remove the warning and suggest highlighting
Oracle缩表空间的完整解决实例
Force deduction solution summary 1200 minimum absolute difference
ICML 2022 | meta proposes a robust multi-objective Bayesian optimization method to effectively deal with input noise
Flow characteristics of kitchen knife, ant sword, ice scorpion and Godzilla
Cartoon: how to multiply large integers? (I) revised version
Matery主题自定义(一)黑夜模式
Rider 设置选中单词侧边高亮,去除警告建议高亮
漫画:如何实现大整数相乘?(下)
证券网上开户安全吗?证券融资利率一般是多少?
漫画:有趣的【海盗】问题
Design of electronic clock based on 51 single chip microcomputer
Tita 绩效宝:如何为年中考核做准备?
Knowledge points of MySQL (7)
goto Statement
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
解读:如何应对物联网目前面临的安全问题?
Redis+caffeine two-level cache enables smooth access speed