Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.

Examples:

s = "leetcode"
return 0. s = "loveleetcode",
return 2.

给定一个字符串,查找其中的第一个非重复字符并返回其索引。如果它不存在,返回-1。

例子:

s =“leetcode”
返回0。

s =“loveleetcode”,
返回2。

class Solution {
public int firstUniqChar(String s) {
int result = -1;
HashMap<Character, Integer> hm1 = new HashMap<Character, Integer>();
for (char c : s.toCharArray()) {
if (hm1.containsKey(c)) {
hm1.put(c, hm1.get(c) + 1);
} else {
hm1.put(c, 1);
}
}
for (int count = 0; count < s.length(); count++) {
if (hm1.get(s.charAt(count)) == 1) {
result = count;
break;
}
}
return result;
}
}

  

leetcode 387的更多相关文章

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

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

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

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

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

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

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

  6. Java [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 does ...

  7. LeetCode 387. First Unique Character in a String (字符串中的第一个唯一字符)

    题目标签:String, HashMap 题目给了我们一个 string,让我们找出 第一个 唯一的 char. 设立一个 hashmap,把 char 当作 key,char 的index 当作va ...

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

    题目: 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. Given a string, find the first non-repeating charact ...

  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. 在sql server中如何检测一个字符串中是否包含另一个字符串

    select CHARINDEX('456','123456')   SQL语句使用CHARINDEX函数,来测试一个字符串中是否包含另一个字符串中的方法: 一.CHARINDEX函数介绍 1.函数功 ...

  2. OpenShift 4.3环境中创建基于Go的Operator

    详细步骤可以参考官方文档 https://docs.openshift.com/container-platform/4.3/operators/operator_sdk/osdk-getting-s ...

  3. CentOS7.5下基于FTP服务的局域网yum源搭建

    实验环境 yum源服务器和yum客户端都处于同一局域网之中 yum源服务器 hostname:zabbix OS版本:CentOS 7.5 IP:192.168.101.201 yum客户端 host ...

  4. HTML的页面结构

    HTML的页面结构 以下为命名的规范: header:标题头部区域的内容(用于页面或页面中的一块区域) footer:标记脚步区域的内容(用于整个页面或页面的一块区域) nav:导航类复杂内容 以上3 ...

  5. koa2第一天 安装koa2found 1 low severity vulnerability run `npm audit fix` to fix them, or `npm audit` for details

    安装全局koa2:npm install -g koa2 -generator 创建一个koa2文件夹:koa2 -e koa2 进入koa2文件夹:cd koa2 安装npm模块:npm insta ...

  6. 2019牛客多校第四场A meeting 思维

    meeting 题意 一个树上有若干点上有人,找出一个集合点,使得所有人都到达这个点的时间最短(无碰撞) 思路 就是找树的直径,找直径的时候记得要找有人的点 #include<bits/stdc ...

  7. [AGC027E]ABBreviate

    Description AGC027E 给定一个仅由\(AB\)构成的字符串\(S\),给定两个操作,把\(AA\)换成\(B\),和把\(BB\)换成\(A\),问由这个字符串和任意次操作可以得到几 ...

  8. hdu 1281 棋盘游戏(二分图)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1281 每行每列作为节点,给可以放车的点将对应的行列连边,最多可放置的车即为建二分图后的最大匹配,跑一边 ...

  9. Java实现Word/Pdf/TXT转html

    引言: 最近公司在做一个教育培训学习及在线考试的项目,本人主要从事网络课程模块,主要做课程分类,课程,课件的创建及在线学习和统计的功能,因为课件涉及到多种类型,像视频,音频,图文,外部链接及文档类型. ...

  10. dw 快捷键

    <html></html> 创建一个HTML文档<head></head> 设置文档标题和其它在网页中不显示的信息<title></t ...