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 ...
随机推荐
- 使用Flexible 实现手淘H5 页面的终端适配学习
Amfe阿里无线前端团队双11技术连载之际,一个实战案例来展示多终端适配. Device metrics 1.0 mdpi 2.0 xhdpi 3.0xxhdpi(iphone 6 plus) 手淘h ...
- 详解SSH注解配置,bean注解、事物注解等
使用过SSH注解的屌丝们都知道,要想使用注解需要在applicationContext.xml配置文件里面开启注解配置,开启方式如下:1.头部声明需加入xmlns:context="http ...
- War3Tool dota改键v3.3版
wartool魔兽全屏改键功能:1.支持11平台自定义改建,自动进局域网(同类软件暂时没发现这个功能)2.技能改键,可以有效的切换适合你的技能键3.war3路径扫描,运行本程序一键就能打开war3 ( ...
- COJ 0288 路径(2015升级版)
路径(2015升级版) 难度级别:D: 运行时间限制:1000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 WZJ在生日当天决定在他的领地举行一场马拉松比赛,他的 ...
- WordPress /wp-admin/users.php畸形s参数路径泄漏漏洞
漏洞版本: WordPress 2.7.x WordPress 2.8.x WordPress 2.9.x WordPress 3.0.x WordPress 3.1.x WordPress 3.2. ...
- 「Poetize5」水叮当的舞步
Description 水叮当得到了一块五颜六色的格子形地毯作为生日礼物,更加特别的是,地毯上格子的颜色还能随着踩踏而改变. 为了讨好她的偶像虹猫,水叮当决定在地毯上跳一支轻盈的舞来卖萌~~~ 地毯上 ...
- 有两个指针pa,pb分别指向有两个数,a,b,请写一个函数交换两个指针的指向,也就是让pa指向b,让pb指向a
题目:有两个指针pa,pb分别指向有两个数,a,b,请写一个函数交换两个指针的指向,也就是让pa指向b,让pb指向a,具体实现如下: #include<stdlib.h> #include ...
- 网络流(最大独立点集):POJ 1466 Girls and Boys
Girls and Boys Time Limit: 5000ms Memory Limit: 10000KB This problem will be judged on PKU. Original ...
- selenium webdriver python 操作IE浏览器
Step1:下载IEDriverServer 下载路径:http://selenium-release.storage.googleapis.com/index.html 我下载的是2.48版本的IE ...
- git 七图七模式 -- 工作流
git 是现在最流行的代码合作工作方式,本文通过七张图来描述一下当下最流行的 git 工作模式 集中式工作流 集中式工作流 功能分支工作流 Gitflow工作流 Forking 工作流 Pull Re ...