当前位置:网站首页>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;
}
};
边栏推荐
- Read libco save and restore the on-site assembly code
- 「运维有小邓」用于云应用程序的单点登录解决方案
- Cartoon: a bloody case caused by a math problem
- 较文心损失一点点性能提升很多
- Disorganized series
- The five most difficult programming languages in the world
- Oracle recovery tools -- Oracle database recovery tool
- 为什么阳历中平年二月是28天
- 排错-关于clion not found visual studio 的问题
- How to modify MySQL fields as self growing fields
猜你喜欢
「运维有小邓」用于云应用程序的单点登录解决方案
LeetCode每日一题:合并两个有序数组
PMP认证需具备哪些条件啊?费用多少啊?
Count the running time of PHP program and set the maximum running time of PHP
哈趣K1和哈趣H1哪个性价比更高?谁更值得入手?
統計php程序運行時間及設置PHP最長運行時間
2022新版PMP考试有哪些变化?
Knowledge points of MySQL (7)
Six bad safety habits in the development of enterprise digitalization, each of which is very dangerous!
Which is more cost-effective, haqu K1 or haqu H1? Who is more worth starting with?
随机推荐
力扣解法汇总1200-最小绝对差
Complete solution instance of Oracle shrink table space
PMP认证需具备哪些条件啊?费用多少啊?
云主机oracle异常恢复----惜分飞
SQL Server(2)
How to modify MySQL fields as self growing fields
漫画:如何实现大整数相乘?(整合版)
7 pratiques devops pour améliorer la performance des applications
QT控制台打印输出
Humi analysis: the integrated application of industrial Internet identity analysis and enterprise information system
Teamcenter 消息注册前操作或後操作
IDEA 项目启动报错 Shorten the command line via JAR manifest or via a classpath file and rerun.
Tita performance treasure: how to prepare for the mid year examination?
Tita 绩效宝:如何为年中考核做准备?
ICML 2022 | Meta提出魯棒的多目標貝葉斯優化方法,有效應對輸入噪聲
Cmake tutorial Step3 (requirements for adding libraries)
忽米沄析:工业互联网标识解析与企业信息系统的融合应用
MATLAB查阅
服务器配置 jupyter环境
漫画:一道数学题引发的血案