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循环结合标签使用,控制跳转
public static void main(String[] args) { outer: for (int i = 0; true; i++) { inner: for (int j = 0; ...
- dns智能解析对网站排名的影响
网站排名是所有建站者都关系的问题,如何提升网站排名有很多因素,网站是否健康也与网站排名有关,下面智儒科技网站建设为你研究下如何判断自己的网站是否健康. 一般情况下,网站的排名在优化的基础上,怎么也上不 ...
- PGA突破pga_aggregate_target限制
SQL> show parameter pga NAME TYPE VALUE ------------------------------------ ----------- ...
- 【HDOJ】3309 Roll The Cube
BFS,考虑一球进洞仅一球滚动以及两球重叠的情况即可. /* 3309 */ #include <iostream> #include <queue> #include < ...
- ikely()与unlikely() 都等同于if, 此处只是做编译优化
ikely()与unlikely()在2.6内核中,随处可见,那为什么要用它们?它们之间有什么区别呢? 首先明确: if (likely(value))等价于if (value)if (likely( ...
- Naive and Silly Muggles (计算几何)
Naive and Silly Muggles Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- phpMyAdmin 完整路径泄露漏洞
漏洞名称: phpMyAdmin 完整路径泄露漏洞 CNNVD编号: CNNVD-201307-650 发布时间: 2013-08-09 更新时间: 2013-08-09 危害等级: 中危 漏洞类 ...
- HDOJ(HDU) 2113 Secret Number(遍历数字位数的每个数字)
Problem Description 有一天, KIKI 收到一张奇怪的信, 信上要KIKI 计算出给定数各个位上数字为偶数的和. eg. 5548 结果为12 , 等于 4 + 8 KIKI 很苦 ...
- Windows Shell Extension 系列文章
Windows Shell Extension 系列文章 http://www.codeproject.com/Articles/512956/NET-Shell-Extensions-Shell-C ...
- 从cpu加电到加载OS内核的详细过程(清华大学ucore-lab1总结一)
结合最近学习清华的OS课,先用“人话”来高度抽象的描述一下我自己的理解.CPU在系统加电也就是我们按下电源开关后,开始初始化他的寄存器,主要是cs和eip(本文基于x86架构),然后在ROM中找到一个 ...