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 ...
随机推荐
- MySQL——基本安装与使用
基本安装 下载地址:https://dev.mysql.com/downloads/mysql/ 选择解压版本:mysql-5.7.21-winx64.zip 以管理员身份打开cmd(除了安装服务不要 ...
- python学习笔记(7)——集合(set)
关键词#1.定义:无序不重复元素集, 基本功能包括关系测试和消除重复元素. 2.关键词:类似dict.只有key无value.常用于集合类数学运算. 3.创建 s=set() #入参可以是list.t ...
- 关于.Net的强名称(Strong Name)
下面是我在CSDN上发表的<关于.Net的强名称(Strong Name)>,转载于此. 关于.Net的强名称(Strong Name)
- MFC窗体大小变化
对话框的大小变化后,假若对话框上的控件大小不变化,看起来会比较难看.下面就介绍怎么让对话框上的控件随着对话框的大小的变化自动调整. 首先明确的是Windows有一个WM_SIZE消息响应函数,这个函数 ...
- Python之UDP编程
参考原文 廖雪峰Python教程 TCP是建立可靠连接,并且通信双方都可以以流的形式发送数据.相对TCP,UDP则是面向无连接的协议. 使用UDP协议时,不需要建立连接,只需要知道对方的IP地址和端口 ...
- acm相关(纯转载)
我觉得很好的文章,之所以放随笔是为了让大家看到这些优秀的博文 acm如何起步 acm重点题型 动态规划题目总结 背包九讲阅读网站
- 山建校赛B题公式证明
原题 证明
- <MySQL>入门三 数据定义语言 DDL
-- DDL 数据定义语言 /* 库和表的管理 一.库的管理:创建.修改.删除 二.表的管理:创建.修改.删除 创建:create 修改:alter 删除:drop */ 1.库的管理 -- 库的管理 ...
- 解决window.location.href参数太长
前言:一提到页面跳转,最常用的一般就是window.location.href,如果需要带参数,也许可以在后面用?拼上,但这样并不安全,而且有个更严重的问题,这样的拼接是有长度限制的,如果达到好几千个 ...
- slf4j-api、slf4j-log4j12、log4j的关系
在网上找到一篇关于这三个jar包的关系的博客,讲的很好,所以就转载了: https://blog.csdn.net/tengdazhang770960436/article/details/18006 ...