leetcode修炼之路——387. First Unique Character in a String
最近公司搬家了,有两天没写了,今天闲下来了,继续开始算法之路。
leetcode的题目如下:
Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.
Examples:
s = "leetcode"
return 0. s = "loveleetcode",
return 2.
Note: You may assume the string contain only lowercase letters.
题目分析:
题目总体来说还是比较easy,就是查找第一个不重复的字符的位置,如果没有的话,返回-1.
代码实现:
public static int firstUniqChar(String s) {
List<Integer> list = new ArrayList<>();//记录已经重复的位置
if (s.length() == 1) {
return 0;
}
for (int i = 0; i < s.length(); i++) {
if (!list.contains(i) && i == s.length() - 1) {//最后一个且不在重复列表中
return i;
}
if (list.contains(i)) {//判断是否为重复的位置
continue;
} else {
for (int j = i + 1; j < s.length(); j++) {
if (s.charAt(i) == s.charAt(j)) {
if (!list.contains(i)) {//把当前的也add进去,最后会判断
list.add(i);
}
list.add(j);//把重复的位置add进去
}
if (j == s.length() - 1 && !list.contains(i)) {//最终判断条件
return i;
}
}
}
}
return -1;
}
上述方法在测试中没有发现问题,但是在效率上出现了比较大的问题,因为 list.contains(i)这是一个循环实现,这使得时间复杂度增加,因此,下面进行了优化:
public static int firstUniqChar2(String s) {
int[] count = new int[26];//最多26个字符
for (int i = 0; i < s.length(); i++) {
count[s.charAt(i) - 'a']++;//记录每个字符出现的次数
}
for (int i = 0; i < s.length(); i++) {
if (count[s.charAt(i) - 'a'] == 1) {//判断当前出现的字符是否值出现了一次
return i;
}
}
return -1;
}
这个方法在前几个算法中都出现了,这次在第一个算法出现了算法的执行时间超时的问题后就想到用这种方式来实现。(ps:在最后判断的时候脑子抽了,没有想到结果判断条件,最后还是查了下才恍然大明白mdzz)。
^_^
leetcode修炼之路——387. First Unique Character in a String的更多相关文章
- LeetCode 387. First Unique Character in a String (字符串中的第一个唯一字符)
题目标签:String, HashMap 题目给了我们一个 string,让我们找出 第一个 唯一的 char. 设立一个 hashmap,把 char 当作 key,char 的index 当作va ...
- LeetCode 387. First Unique Character in a String
Problem: Given a string, find the first non-repeating character in it and return it's index. If it d ...
- 18. leetcode 387. First Unique Character in a String
Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...
- [LeetCode&Python] Problem 387. First Unique Character in a String
Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...
- [leetcode]387. First Unique Character in a String第一个不重复字母
Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...
- Java [Leetcode 387]First Unique Character in a String
题目描述: Given a string, find the first non-repeating character in it and return it's index. If it does ...
- *387. First Unique Character in a String (linkedhashmap + string) debug manunally instead of using leetcode
The ability to debug and overall thinking need to improve Given a string, find the first non-repeati ...
- 【LeetCode】387. First Unique Character in a String
Difficulty:easy More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/first-unique-cha ...
- [LeetCode] 387. First Unique Character in a String 字符串的第一个唯一字符
Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...
随机推荐
- hadoop 2.2.0 eclipse 插件编译 及相关eclipse配置图解
https://github.com/winghc/hadoop2x-eclipse-plugin 官网 http://kangfoo.github.io/article/2013/12/build- ...
- hdu 5139 Formula
http://acm.hdu.edu.cn/showproblem.php?pid=5139 思路:这道题要先找规律,f(n)=n!*(n-1)!*(n-2)!.....1!; 不能直接打表,而是离 ...
- Protues中有源蜂鸣器BUZZER不响的解决办法(有图)
这个是BUZZER有源蜂鸣器的protues连线图(FM是我自己的电压探针,可以删除的) 下面是我个人设置的蜂鸣器的参数(为什么很多人的这个蜂鸣器不响,是参数没有设置正确) 蜂鸣器不响的原因是 Ope ...
- 通过预编译头文件来提高C++ Builder的编译速度
C++ Builder是最快的C++编译器之一,从编译速度来说也可以说是最快的win32C++编译器了.除了速度之外,C++builder的性能也在其它C++编译器的之上,但许多Delphi程序员仍受 ...
- C++ Primer 随笔 Chapter 2 变量和基本类型
2.1C++内置类型 C++ 算术类型 类型 含义 最小存储空间(随机器不同而不同) bool 布尔型 --- char 字符型 8位 wchar_t 宽字符型 16位 short 短整型 16位 i ...
- Building a Space Station(kruskal,说好的数论呢)
Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 3820 Accepted: 1950 Description You a ...
- BestCoder Round #52 (div.1)
这周六BC和CF又差点打架,精力不够啊...结果打BC没起来,就看了一眼题跑了...今天早上补补吧,(因为今天晚上还要打UER= =) 先放官方题解: 1000 Victor and Machine ...
- ACM第六周竞赛题目——A LightOJ 1317
A - A Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit Status P ...
- UVAlive3126 Taxi Cab Scheme(DAG的最小路径覆盖)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=32568 [思路] DAG的最小路径覆盖. 将每个人看做一个结点,如 ...
- FTP软件Filezilla出现“读取目录列表失败”的解决办法
FTP软件Filezilla出现“读取目录列表失败”情况一般出现在vista/win7系统上,之前在xp上没发现这种情况. 总的来说,不论是打开FTP出现乱码或者显示“读取目录列表失败”均是由字符集引 ...