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 ...
随机推荐
- java security
安全性是Java应用程序的非功能性需求的重要组成部分,如同其它的非功能性需求一样,安全性很容易被开发人员所忽略.当然,对于Java EE的开发人员来说,安全性的话题可能没那么陌生,用户认证和授权可能是 ...
- Android Wear开发 - 入门指引 - Eclipse开发平台搭建
开发平台配置 下载最新版本的ADT,详情见官网:http://developer.android.com/sdk/installing/installing-adt.html .(之前一直习惯于Goo ...
- 【HDOJ】1818 It's not a Bug, It's a Feature!
状态压缩+优先级bfs. /* 1818 */ #include <iostream> #include <queue> #include <cstdio> #in ...
- 嵌入式设备web服务器比较
目录(?)[-] Boa Thttpd Mini_httpd Shttpd Lighttpd Goahead AppWeb Apache 开发语言和开发工具 结论 备注 现在在嵌入式设备中所使用的 ...
- UVAlive2531 The K-League(最大流)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=33451 [思路] 最大流. 大体思路是枚举每个队伍,最大流判断是否 ...
- poj1065
题目大意: 木棍(好吧,承认确实做过这个题,嘎嘎) 有一堆木棍大约有n根,木棍的长度和重量都预先知道,这些木棍会在一个木工机械上一个接一个的处理,这需要一些时间,称为设置时间,为机械准备处理一根木头, ...
- 读《细说php》,php要点随记
近期读<细说php>,其实知识要点都接触过,体会下不同书籍对相同知识的描述差异,达到温故知心的目的. 未按章节顺序读,所谓要点并不是提纲式的所有要点,只是自己觉得工作中很重要(但是掌握的不 ...
- 九度online judge 1543 二叉树
题目1543:无限完全二叉树的层次遍历 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:389 解决:54 题目描述: 有一棵无限完全二叉树,他的根节点是1/1,且任意一个节点p/q的左儿 ...
- spring 定时器----quartz启动问题
今天,突然要用到定时器,在网上查了下资料,保存下以方便后面查找: 什么是动态定时任务:是由客户制定生成的,服务端只知道该去执行什么任务,但任务的定时是不确定的(是由客户制定). 这样总不能修改配置文件 ...
- [Flux] Stores
Store, in Flux which manager the state of the application. You can use EventEmiiter to listen to the ...