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. 记录 shell学习过程(4)for 循环

    1. for in ` #seq 生成从1到10 如果生成从10到1则写作 seq 10 -1 1 do echo $i done for in 也可以循环出字符串 for i in where is ...

  2. pip install时报错

    因为需要mysqlclient这个模块,但是在pip的时候报错 Collecting mysqlclient  Using cached https://files.pythonhosted.org/ ...

  3. codeforces 1284D. New Year and Conference(线段树)

    链接:https://codeforces.com/problemset/problem/1284/D 题意:有n场讲座,有两个场地a和b,如果在a场地开讲座则需要占用[sai,eai],在b场地开讲 ...

  4. 诡异的BUG

    1.今天遇到一个诡异的BUG(一个很古老的项目),为什么说他诡异呢,我们本地都是OK的,但是现场部署就会报错? 2.描述下现象其实这个问题不难定位(关键是有个jar包没有源码不能进行验证性的编译) 我 ...

  5. Java内存管理(1)——垃圾收集

    其它语言(如C语言)要求程序员显式地分配内存.释放内存. 程序需要内存时分配内存,不需要时释放内存. 但是这种做法常常引起内存泄漏.所谓内存泄漏,就是由于某种原因使分配的内存始终没有得到释放.如果该任 ...

  6. 配置Mongodb

    MS Windows: 常用命令: 查看帮助 mongod help/mongod -h 查看版本 mongod --version 启动/停止/重启服务 net start/stop/restart ...

  7. springboot13(redis缓存)

    redis做springboot2.x的缓存 1.首先是引入依赖 <dependency> <groupId>org.springframework.boot</grou ...

  8. java 判断数据是否为空

    /** * 方法描述:自定义判断是否为空 * 创建作者:李兴武 * 创建日期:2017-06-22 19:50:01 * * @param str the str * @return the bool ...

  9. 部署项目到jetty

    一.打包项目 1.在pom.xml中添加以下依赖 <dependency> <groupId>org.mortbay.jetty</groupId> <art ...

  10. 16 符号 xargs

    03. 系统特殊符号: 1) 基础符号系列 美元符号:$ 叹号符号: ! 取反 竖线符号: | 管道符号 前一个命令执行结果交给后面命令处理 xargs 命令|xargs 命令 xargs: 将信息进 ...