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 ...
随机推荐
- N.O.W,O.R,N.E.V.E.R--12days to LNOI2015
双向链表 单调队列,双端队列 单调栈 堆 带权并查集 hash 表 双hash 树状数组 线段树合并 平衡树 Treap 随机平衡二叉树 Scapegoat Tree 替罪羊树 朝鲜树 块状数组,块状 ...
- BZOJ 1015 星球大战
Description 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系.某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过 ...
- yum仅下载RPM包不安装
http://www.ttlsa.com/linux/howto-yum-download-rpm-without-install/
- Election Time
Election Time Time Limit: 1000MS Memory limit: 65536K 题目描述 The cows are having their first election ...
- Krypton Factor 困难的串-Uva 129(回溯)
原题:https://uva.onlinejudge.org/external/1/129.pdf 按照字典顺序生成第n个“困难的串” “困难的串”指的是形如ABAB, ABCABC, CDFGZEF ...
- JEFF BANKS_百度百科
JEFF BANKS_百度百科 JEFF BANKS
- git 七图七模式 -- 工作流
git 是现在最流行的代码合作工作方式,本文通过七张图来描述一下当下最流行的 git 工作模式 集中式工作流 集中式工作流 功能分支工作流 Gitflow工作流 Forking 工作流 Pull Re ...
- AE 3D摄像机工作原理
看了AE教程的3D可视化音频和序列法导入三维模型之后对于视频解析3D是有了更深的认识.很感谢AE在CS6之后加入了3D摄像机跟踪器的功能.它是通过摄像机跟踪反求来得到影片中的平面特征点.然后由用户指定 ...
- 解决python “No module named pip”
python 升级后导致不能使用原来的pip命令 windows平台 cmd中敲命令:python -m ensurepip 得到pip的setuptools 然后就可以用:easy_install ...
- Python中 os._exit() sys.exit() exit()区别
Python退出程序的方式有两种:os._exit(), sys.exit() 1)os._exit() 直接退出 Python程序,其后的代码也不会继续执行. 2)sys.exit() 引发一个 S ...