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. (转)git学习教程

    转自:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000

  2. 如何着手学习WebRTC开发(转)

    文章链接:http://www.sohu.com/a/146536246_458408 WebRTC中文社区-国内镜像:https://webrtc.org.cn/mirror/#windows%E2 ...

  3. PyQt5学习笔记-从主窗体打开一个子窗体

    PyQt5学习笔记-从主窗体打开一个子窗体 软件环境: Eric6+Python3.5+PyQt5 试验目标: 1.点击菜单项Open,打开一个子窗体 2.点击按钮Open,打开一个子窗体 主窗体设计 ...

  4. c++ char转换成string

    第一种:利用赋值号直接赋值 ; string b = a; /* 错误.因为string是一个指针,存储的值是所指向的地址,而char型存储的是内容,所以不可以直接用赋值号赋值 */ const ch ...

  5. PLL

    PLL(Phase Locked Loop): 为锁相回路或锁相环,用来统一整合时脉讯号,使内存能正确的存取资料.PLL用于振荡器中的反馈技术. 许多电子设备要正常工作,通常需要外部的输入信号与内部的 ...

  6. STM32F103之ADC学习记录

    1.问题 1)10位ADC的误差是多少? 首先要分清分辨率与精度的区别. 10cm的尺子,有100个等分刻度,则该尺子的分辨率为1mm. 但不能说这把尺子的精度是1mm. 在冬天,尺子会热胀冷缩,依然 ...

  7. codeforces Codeforces Round #597 (Div. 2) Constanze's Machine 斐波拉契数列的应用

    #include<bits/stdc++.h> using namespace std; ]; ]; ; int main() { dp[] = ; scanf(); ); ; i< ...

  8. MySQL学习(四)死锁及死锁检测

    文章图片来自参考资料 MySQL 的锁     根据加锁的范围,MySQL里面的锁大致可以分成全局锁.表级锁和行锁三类.我们需要明白锁的服务是为了数据统一,或者说是事务,数据隔离. 全局锁     全 ...

  9. 后端工具——Maven——初篇——目录

    目录 Maven的知识体系包括四个部分.Maven的配置文件,Maven命令,Maven生命周期,Maven插件.在介绍Maven之前,首先需要介绍如何安装Maven. 安装:介绍Maven在Linu ...

  10. 各技能DBC参数

    推荐你  通过 引擎的帮助文件查找标准魔法DB 下面是 部分hero引擎的标准魔法DB 34,解毒术,2,26,16,0,0,0,0,0,2,42,50,44,100,46,200,40,, 35,老 ...