题目标签: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 (字符串中的第一个唯一字符)的更多相关文章

  1. 387 First Unique Character in a String 字符串中的第一个唯一字符

    给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1.案例:s = "leetcode"返回 0.s = "loveleetcode&qu ...

  2. [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 ...

  3. 前端与算法 leetcode 387. 字符串中的第一个唯一字符

    目录 # 前端与算法 leetcode 387. 字符串中的第一个唯一字符 题目描述 概要 提示 解析 解法一:双循环 解法二:Set法单循环 算法 传入测试用例的运行结果 执行结果 GitHub仓库 ...

  4. Java实现 LeetCode 387 字符串中的第一个唯一字符

    387. 字符串中的第一个唯一字符 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. 案例: s = "leetcode" 返回 0. s = ...

  5. LeetCode初级算法之字符串:387 字符串中的第一个唯一字符

    字符串中的第一个唯一字符 题目地址:https://leetcode-cn.com/problems/first-unique-character-in-a-string/ 给定一个字符串,找到它的第 ...

  6. LeetCode初级算法--字符串02:字符串中的第一个唯一字符

    LeetCode初级算法--字符串02:字符串中的第一个唯一字符 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog. ...

  7. 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 ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. Selenium基于Python web自动化测试框架 -- PO

    关于selenium测试框架首先想到的就是PO模型,简单说下PO模型 PO模型的概念和理解: PO就是一个设计思想,将代码以页面为单位进行组织,针对这个页面上的所有信息.相关操作都放到一个类中,从而使 ...

  2. postgreSQL在Centos6下编译安装

    1.准备安装源 下载地址:https://www.postgresql.org/ftp/source/ 下载并解压. 2.软件编译安装 配置.检查安装环境 ./configure --prefix=/ ...

  3. navicat创建存储过程报错

    搞了半天这个恶心的报错,最后发现竟然是存储过程的一个varchar类型的参数没给长度,如varchar(64)长度必须指定不然就会报错: mark一记

  4. DB2 系统命令与配置参数大全

    主要包括4个部分,分别为: DB2 系统命令 DB2 数据库管理器配置参数 DB2 数据库系统配置参数 DB2 管理服务器(DAS)配置参数DB2 系统命令 dasauto - 自动启动 DB2 管理 ...

  5. Qt 杂记——QTableWidget列表添加、删除(备份)

    1.列表的添加 需求:向一个有两列的Table中添加一条数据 思路:新建一个inputDialog,通过按钮打开Qt自带的inputDialog,传递回输入的数据,再添加到列表中 界面: 代码: in ...

  6. c3p0参数详解

    <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数.Default: 3 --> <property name="acquireIncrement"& ...

  7. HDU_2079_(01背包)(dfs)

    选课时间(题目已修改,注意读题) Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. 洛谷P1107 & BZOJ1270 [BJWC2008]雷涛的小猫

    一道DP. 给你一个矩阵里面有很多数,你需要从上往下找到一种跳跃方法使得经过的点的价值之和最大. 具体题面见链接 洛谷P1107 BZOJ1270 很明显是一个二维的DP. #include<b ...

  9. python3中post请求里带list报错

    这个post请求的数据太长,一般data=,json=就够了. 但是今天这个一直报错,用json吧,报缺少参数,用data吧,报多余[. 后来改成data=,并把数据中的[] 用引号括起来," ...

  10. Literature Review on Tidal Turbine

    source: scopus , SCIE keywords in tilte: tidal turbine Software: Bibliometrix