LeetCode 387. First Unique Character in a String (字符串中的第一个唯一字符)
题目标签:String, HashMap
题目给了我们一个 string,让我们找出 第一个 唯一的 char。
设立一个 hashmap,把 char 当作 key,char 的index 当作value。
遍历string,如果这个 char 没有在 map 里,说明第一次出现,存入 char,index;
如果这个 char 已经在 map 里,说明不是第一次出现,而且我们不在乎这种情况,更新 char, -1。
遍历hashmap,把最小的 index 的值找出来,返回即可。
Java Solution:
Runtime beats 41..82%
完成日期:08/19/2018
关键词:HashMap
关键点:把index 存入map
class Solution
{
public int firstUniqChar(String s)
{
HashMap<Character, Integer> map = new HashMap<>();
char[] arr = s.toCharArray(); for(int i=0; i<arr.length; i++)
{
char c = arr[i];
Integer v = map.get(c); if(v != null) // if a char goes here, means that it is not a unique one
map.put(c, -1);
else // unique char only goes here once
map.put(c, i);
} int minIndex = Integer.MAX_VALUE;
// find a min index char by iterating hashMap
for(Character key : map.keySet())
{
Integer v = map.get(key); if(v >= 0 )
minIndex = Math.min(minIndex, v); } return minIndex == Integer.MAX_VALUE ? -1 : minIndex;
}
}
参考资料:https://leetcode.com/problems/first-unique-character-in-a-string/discuss/161004/Java-one-pass-O(1)-space-solution
LeetCode 题目列表 - LeetCode Questions List
题目来源:https://leetcode.com/
LeetCode 387. First Unique Character in a String (字符串中的第一个唯一字符)的更多相关文章
- 387 First Unique Character in a String 字符串中的第一个唯一字符
给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1.案例:s = "leetcode"返回 0.s = "loveleetcode&qu ...
- [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 387. 字符串中的第一个唯一字符
目录 # 前端与算法 leetcode 387. 字符串中的第一个唯一字符 题目描述 概要 提示 解析 解法一:双循环 解法二:Set法单循环 算法 传入测试用例的运行结果 执行结果 GitHub仓库 ...
- Java实现 LeetCode 387 字符串中的第一个唯一字符
387. 字符串中的第一个唯一字符 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. 案例: s = "leetcode" 返回 0. s = ...
- LeetCode初级算法之字符串:387 字符串中的第一个唯一字符
字符串中的第一个唯一字符 题目地址:https://leetcode-cn.com/problems/first-unique-character-in-a-string/ 给定一个字符串,找到它的第 ...
- LeetCode初级算法--字符串02:字符串中的第一个唯一字符
LeetCode初级算法--字符串02:字符串中的第一个唯一字符 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog. ...
- 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]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 ...
随机推荐
- python __slots__ 详解(上篇)
转自:http://blog.csdn.net/sxingming/article/details/52892640 python中的new-style class要求继承Python中的一个内建类型 ...
- 【sqli-labs】【jsp/tomcat】 less29 less30 less31 less32 (GET型利用HTTP参数污染的注入)
sqli-labs带了几个Java版本的web注入,在tomcat-files.zip里 以Less29为例,查看源码,可以看出请求最后还是提交给了php应用,难怪less29文件夹下有一个没有任何防 ...
- HDU_1520_Anniversary party_树型dp
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 Anniversary party Time Limit: 2000/1000 MS (Java ...
- POJ_2536_Gopher II
题意:n只地鼠,m个地鼠洞,地鼠必须以v的速度在s秒内钻进洞且每个洞仅能容纳一只地鼠,问最少有几只地鼠会被老鹰吃掉. 分析:最大匹配问题,将s秒内地鼠能够跑到的洞与该地鼠连成一条边,在最后得到的图中使 ...
- Solr搜索引擎 — 通过mysql配置数据源
一,准备数据库数据表结构 CREATE TABLE `app` ( `id` int(11) NOT NULL AUTO_INCREMENT, `app_name` varchar(255) NOT ...
- CAD把当前图形保为一个jpg文件(com接口Delphi语言)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 procedure TForm2.Button1Click(Sender: TObje ...
- ls 命令还能这么玩?看一下这 20 个实用范例
Linux中一个基本命令是ls.没有这个命令,我们会在浏览目录条目时会遇到困难.这个命令必须被每个学习Linux的人知道. ls是什么 ls命令用于列出文件和目录.默认上,他会列出当前目录的内容.带上 ...
- Loadrunner12 安装与卸载
卸载:http://www.51testing.com/html/21/303921-216608.html 安装包下载: loadrunner12安装包下载:链接:https://pan.baidu ...
- vivo手机执行input命令提示killed
异常现象: 使用vivo手机时发现通过inputManager发送按键.执行屏幕滑动等动作失效,相关API并没有任何异常抛出,继续跟踪发现shell控制台执行input进行屏幕滑动.发送文本.模拟按键 ...
- 反片语(Ananagrams,Uva 156)
输入一些单词,找出所有满足如下条件的单词:该单词不能通过字母重排,得到输入文 本中的另外一个单词.在判断是否满足条件时,字母不分大小写,但在输出时应保留输入中 的大小写,按字典序进行排列(所有大写字母 ...