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 ...
随机推荐
- 深度优先搜索DFS和广度优先搜索BFS简单解析
转自:https://www.cnblogs.com/FZfangzheng/p/8529132.html 深度优先搜索DFS和广度优先搜索BFS简单解析 与树的遍历类似,图的遍历要求从某一点出发,每 ...
- mongodb GUI工具
人性化,界面工具 网上搜索找的一些 1. 官方网站 tools 2. adminMongo 这个也是我在使用的 3. MongoClient 4. NoSQL Manager for MongoDB ...
- 《网络管理》IP地址管理与子网划分
IP地址管理——ipmaster ipmaster是一款对IP地址进行管理的软件,使用该软件可以提高网络管理员的工作效率.在大型网络中,使用该软件可以有序且高效地实现大中小型企业网IP地址的分配和管理 ...
- CAD与用户交互在图面上选择一个实体(com接口VB语言)
主要用到函数说明: IMxDrawUtility::GetEntity 与用户交互到在图面上选择一个实体,详细说明如下: 参数 说明 [out] IMxDrawPoint** pPickPoint 返 ...
- JavaScipt30(第七个案例)(主要知识点:数组some,every,findIndex方法)
承接上文,这是第7个案例,这个案例没什么说的,主要有三个注意点: 附上项目链接: https://github.com/wesbos/JavaScript30 // 1. slice(begin, e ...
- js数组的处理
//重写Array中的indexOf方法,获取数组中指定值的元素的索引 Array.prototype.indexOf = function (val) { for (var i = 0; i < ...
- BZOJ1013 + BZOJ1923 + POJ1830 (高斯消元)
三个题放在一起写了 主要是搞搞模板 在这里简述一下怎么写高斯消元 就和代数里学的加减消元学的一样 把矩阵化为上三角形形式 然后进行回代 同时枚举当前要消元的未知数和当前化简到哪一行了 然后从这一行往后 ...
- JPA API与注解
一.JPA API Persistence 类:用于获取 EntityManagerFactory 实例,该类含有静态方法 createEntityManagerFactory. //persiste ...
- knockout.js--基本用法
1,HTML元素的面向对象的赋值,数据绑定 text绑定:为p,span,div,td等加text属性值(即元素内部显示的文本), value绑定:为input添加value属性值, attr绑定:为 ...
- linux性能优化cpu-02平均负载
每次我们系统变慢时,我们通常做的第一件事就是top命令或者uptime命令,看一下系统的负载情况,比如下面: 我在命令行中输入uptime 22:15:51 表示当前系统时间 up 13 min ...