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 ...
随机推荐
- 【译】x86程序员手册26-7.5任务切换
7.5 Task Switching 任务切换 The 80386 switches execution to another task in any of four cases: 80386在以下四 ...
- STA之Concepts (2)
3 Skew between signals Skew is the difference in timing between two or more signals, maybe data, clo ...
- 4星|《OKR工作法》:关注公司的真正目标,以周为单位做计划和考核
本书篇幅比较小,两个小时就可以看完.主要内容讲OKR工作法的基本概念,然后用一个虚拟的创业公司的创业故事来演示实施OKR过程中可能遇到的问题.OKR给创业带来的好处. OKR工作法相对来说是比较简单的 ...
- 在CorelDRAW中的自定义彩虹笔刷创建迷幻背景
在60年代的艺术形式中,迷幻艺术对设计和图形艺术的影响尤为重要.在下面这个CorelDRAW教程中我们主要使用图形纸工具和艺术笔工具创建一个迷幻风格的背景,在这之前我们需要先创建出一个彩虹笔刷. 1. ...
- Redis系列(二)--分布式锁、分布式ID简单实现及思路
分布式锁: Redis可以实现分布式锁,只是讨论Redis的实现思路,而真的实现分布式锁,Zookeeper更加可靠 为什么使用分布式锁: 单机环境下只存在多线程,通过同步操作就可以实现对并发环境的安 ...
- ORB-SLAM2:一种开源的VSLAM方案(译文)
摘要: ORB-SLAM2是基于单目,双目和RGB-D相机的一套完整的SLAM方案.它能够实现地图重用,回环检测和重新定位的功能.无论是在室内的小型手持设备,还是到工厂环境的无人机和城市里驾驶的汽车, ...
- Linux kernel memory-faq.txt
## Linux kernel memory-faq.txt What is some existing documentation on Linux memory management? Ulric ...
- iphone6,iphone6 plus适配,旧项目出现黑线问题
问题:可能开始适配iPhone6和iPhone6 plus的朋友很快就发现,模块器头部和底部会出线一条黑线.但是在其他模拟器完全没有问题.程序也能正常跑.如下图 很清楚的看到头部有一条黑线. 解决办法 ...
- LeetCode_18 4Sum
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...
- scrapy 按顺序抓取text内容
需求:获得如下li.clearfix 下的所有text,并且按顺序输出 1. x.css('div.reply-doc h4 a::text').extract(); 2. x.css('div.r ...