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. 题解【洛谷P1886】滑动窗口 /【模板】单调队列

    题面 单调队列模板题. 单调队列可以从队首和队尾出队. 队列中的元素大小具有一定的顺序. 具体可参考这一篇题解 #include <bits/stdc++.h> #define itn i ...

  2. Git分支基本命令+coding webhook+lnmp

    首先介绍一写基本的git操作命令: 查看当前项目的远程地址: git remote -v 查看远程地址所有分支: git branch -a 或者 git branch -r 查看本地分支与远程分支的 ...

  3. C++-数据抽象入门

    一.假定数据是如何存储的 隐藏某些实现逻辑时,我们是想要隐藏绘制子弹的细节.我们是通过使用一个可以调用的函数,而不是直接写出绘制子弹到屏幕上的代码来实现的.这里同样可以使用一个函数来隐藏棋盘存储的细节 ...

  4. echart如何去掉X 、Y轴的网格线

    1.如何去掉X.Y轴的网格线,关键是splitLine{show:false} xAxis:[{ type:'value', splitNumber:2, scale:true, splitLine: ...

  5. 如何在Word中批量选中特定文本

    如何在Word中批量选中特定文本 举个例子,我们对如下文本进行操作,将文本中所有的“1111111”标红,所有的“2222222”标绿,所有的“3333333”标蓝 在Word中找到“查找”下的“高级 ...

  6. 分组拼接字符串,GROUP_CONCAT

    背景 一年过的很快,文中后来的两位员工 馮大 和 馮二 也要面对无情的 KPI 考核了,他们工作干的很不错,performance 分别是 4 和 5 新需求来了,静悄悄的来了!!! 领导想要查看每个 ...

  7. layui之表单验证

    这篇文章的表单验证我只是随便记录下,望各位看官理解 1.排序 验证 html代码 <div class="layui-form-item"> <label cla ...

  8. 通过Spring Resource接口获取资源

    目录 1       Resource简介 2       通过ResourceLoader获取资源 3       在bean中获取Resource的方式 1       Resource简介 在S ...

  9. Axure 8.1.0.3377 激活码 授权码 (亲测有效)

    适用版本 Axure 8.1.0.3377 zdfans.com gP5uuK2gH+iIVO3YFZwoKyxAdHpXRGNnZWN8Obntqv7++FF3pAz7dTu8B61ySxli 亲测 ...

  10. LED Decorative Light Manufacturer - Decorative Lighting: Functionality And Aesthetics

    Whether it is for general ambient lighting, task lighting or accent lighting, the decorative lightin ...