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 ...
随机推荐
- 【PostgreSQL-9.6.3】使用pg_settings表查看参数的生效条件
PostgreSQL数据库的配置参数都在postgresql.conf文件中,此文件的目录为数据库的数据目录($PGDATA).这些参数有些是直接修改就可以生效,有些需要重启数据库才能生效,而有些根本 ...
- 【sqli-labs】 对于less34 less36的宽字节注入的一点深入
1.AddSlashes() 首先来观察一下是如何通过构造吃掉转义字符的 先将less 34的网页编码换成gbk 加上一些输出 echo "Before addslashes(): &quo ...
- 如何在网页中浏览和编辑DWG文件 梦想CAD控件
如何在网页中浏览和编辑DWG文件 梦想CAD控件 www.mxdraw.com 梦想绘图控件5.2 是国内最强,最专业的CAD开发组件(控件),不需要AutoCAD就能独立运行.控件使用VC 201 ...
- ThinkPHP---TP功能类之验证码
[一]验证码 验证码全称:captcha(全自动识别机器与人类的图灵测试),简单理解就是区分当前操作是人执行的还是机器执行的 常见验证码分3种:页面上图片形式.短信验证码(邮箱验证可以归类到短信验证码 ...
- Python学习-变量
什么是变量? 概念:变量就是会变化的量,主要是“变”与“量”二字.变即是“变化”. 特点:与其他编程语言相同,变量是最基本的存储单位,是用来存放数据的容器.可以引用一个具体的数值,进而直接去改变这个引 ...
- 每日命令:(9)touch
linux的touch命令不常用,一般在使用make的时候可能会用到,用来修改文件时间戳,或者新建一个不存在的文件. 1.命令格式: touch [选项]... 文件... 2.命令参数: -a ...
- 制作一个最小Linux系统
使用的是itop4412开发板(仅记录个人的学习回顾,如有不当之处欢迎指出) ---------致谢 准备:busybox软件.uboot(一般和开发板配套).zImage(kernel内核).ram ...
- buf.writeInt32BE()函数详解
buf.writeInt32BE(value, offset[, noAssert]) buf.writeInt32LE(value, offset[, noAssert]) value {Numbe ...
- Tampermonkey脚本安装问题及自用脚本推荐
对于高手来说,chrome浏览器中即使没有其他任何chrome插件,可能都无关紧要.但是有一个插件必不可少, 那就是Tampermonkey油猴插件.Tampermonkey是Chrome上最流行的用 ...
- Glossary in Turbulence
Table of Contents 1. Concepts/Glossary 1.1. Turbulent eddy viscosity ,μt 1.2. Turbulent kinetic ener ...