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. Mike and strings

    Mike has n strings s1, s2, ..., sn each consisting of lowercase English letters. In one move he can ...

  2. AspxDashBorad_OnDashboardLoaded 获取对应的DashboardParameter

    protected void ASPxDashboardViewerThrend_OnDashboardLoaded(object sender, DashboardLoadedWebEventArg ...

  3. Linux命令 sleep 延迟

    用途说明 sleep命令常用于在Linux shell脚本中延迟时间 常用方式 注意:以下用法中<n>可以为小数. 格式:sleep <n> 格式:sleep <n> ...

  4. sftp,ftp文件下载

    一.sftp工具类 package com.ztesoft.iotcmp.util; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsc ...

  5. c++面向对象 之 内联函数 this 静态成员

    1,内联函数 如果一个函数是内联的,那么在编译时,编译器会把该函数的代码副本放置在每个调用该函数的地方.用inline指定,内联函数通常短小精悍没有while和for循环,能够帮助提升程序执行的速度 ...

  6. F与Q查询

    F查询: 之前构造的过滤器都是将字段值与某个我们设定的常亮做比较,如果我们要对两个字段的字段的值做比较久需要用到F查询:F查询可以用来比较同一个model事例中两个不同字段的值, 准备工作: 创建数据 ...

  7. 新的存储网站,和存储单元dropbox

    新的存储网站,和存储单元dropbox 待办 https://www.dropbox.com/home google 登陆 google邮箱 密码 521google 但是免费存储量只有2G goog ...

  8. 看端口是否被占用的python脚本

    在创建 tcp server 的时候,首先检测端口是否被占用. 代码如下: ----------------------------------------import socketdef net_i ...

  9. JFrog推出全球首个支持混合云架构,端到端的通用DevOps平台 ——JFrog Platform

            JFrog Platform,基于屡获殊荣的JFrog Artifactory制品仓库的独特能力,通过多合一的体验提供DevSecOps.CI / CD和软件分发的解决方案. 2020 ...

  10. D. Lunar New Year and a Wander bfs+优先队列

    D. Lunar New Year and a Wander bfs+优先队列 题意 给出一个图,从1点开始走,每个点至少要经过一次(可以很多次),每次经过一个没有走过的点就把他加到走过点序列中,问最 ...