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 ...
随机推荐
- 关于C#匿名方法
作者 陈嘉栋(慕容小匹夫) 阅读目录 0x00 前言 0x01 不必构造委托对象 0x02 匿名方法初探 0x03 使用匿名方法省略参数 0x04 匿名方法和闭包 0x05 匿名方法如何捕获外部变量 ...
- BZOJ 1501 智慧珠游戏
Description Input 文件中包含初始的盘件描述,一共有10行,第i行有i个字符.如果第i行的第j个字符是字母”A”至”L”中的一个,则表示第i行第j列的格子上已经放了零件,零件的编号为对 ...
- Unity3d 跑酷游戏 之Character Controller篇
unity3d Character Controller @by 广州小龙 做3D跑酷游戏,也慢慢的学习了一些东西,从开发过程中积累了一些小的知识点跟大家分享一下! 1. 这个Revert按钮的意 ...
- Js中的window.parent ,window.top,window.self详解
在应用有frameset或者iframe的页面时,parent是父窗口,top是最顶级父窗口(有的窗口中套了好几层frameset或者iframe),self是当前窗口, opener是用open方法 ...
- strtotime的几种用法区别
strtotime不仅可以使用类似Y-m-d此类标准的时间/日期字符串来转化时间戳, 还可以用类似自然语言的来生成时间戳, 类似: strtotime('last day'); strtotime(' ...
- Extjs4中用combox做下拉带图片的下拉框
今天,莫名的来个一个需求,需要做的一个下拉框中,需要有图片,这不...谷歌+度娘然后找网友,终于搞定.现在感谢这些提供资料的友友... 效果如图:
- form.Show()和form.ShowDialog()的区别、新建一个form和MessageBox.Show()的常见用法
一:form.Show()和form.ShowDialog()的区别 a. 任何窗体(派生于基类Form的类),都可以以两种方式进行显示. //非模式窗体From qform=new Form();q ...
- 检测CPU是否支持虚拟化
一:下载检测软件 地址:http://files.cnblogs.com/hongmaju/Coreinfo.rar 二:使用方法 打开运行窗口,找到Coreinfo.exe,运行如下: 现在你要做的 ...
- C# dll玩注入!.net运行时库内置!?
Contents Introduction Back To Fundamentals Load The CLR Fundamentals Advanced DLL Injection Fundamen ...
- Linked List Cycle——LeetCode
Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...