387. First Unique Character in a String

Easy

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.

Note: You may assume the string contain only lowercase letters.

package leetcode.easy;

public class FirstUniqueCharacterInAString {
public int firstUniqChar(String s) {
java.util.HashMap<Character, Integer> count = new java.util.HashMap<Character, Integer>();
int n = s.length();
// build hash map : character and how often it appears
for (int i = 0; i < n; i++) {
char c = s.charAt(i);
count.put(c, count.getOrDefault(c, 0) + 1);
} // find the index
for (int i = 0; i < n; i++) {
if (count.get(s.charAt(i)) == 1)
return i;
}
return -1;
} @org.junit.Test
public void test() {
System.out.println(firstUniqChar("leetcode"));
System.out.println(firstUniqChar("loveleetcode"));
}
}

LeetCode_387. First Unique Character in a String的更多相关文章

  1. 209. First Unique Character in a String

    Description Find the first unique character in a given string. You can assume that there is at least ...

  2. Leetcode算法比赛----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 ...

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

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

  4. [LeetCode] 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. 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. 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 ...

  7. LeetCode First Unique Character in a String

    原题链接在这里:https://leetcode.com/problems/first-unique-character-in-a-string/ 题目: Given a string, find t ...

  8. leetcode修炼之路——387. First Unique Character in a String

    最近公司搬家了,有两天没写了,今天闲下来了,继续开始算法之路. leetcode的题目如下: Given a string, find the first non-repeating characte ...

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

随机推荐

  1. com.alibaba.fastjson把JSONObject转换为Map<String, String>对象

    https://www.cnblogs.com/fomeiherz/p/6351287.html JSONObject obj = new JSONObject();{obj.put("ke ...

  2. CSP-J总结&题解

    总结: 这一次,最后一次,还是不行啊. 文件操作方面:没有FCLOSE,血的教训. 考场复盘: 首先一二题没什么好讲的,秒切.但是第三题由于一开始看出来是完全背包,但是好像又不是,去年又有摆渡车阴影, ...

  3. Kubernetes 学习8 Pod控制器

    一.回顾 1.Pod是标准的kubernetes资源,因此其遵循为其资源清单配置定义的基本格式,包含:apiVersion,kind,metadata,spec,status(只读) 2.spec的内 ...

  4. SSH远程连接虚拟机,将虚拟机映射本地端口

    本周学习内容: 1.继续学习了网络是怎么连接的和JavaScript的内容: 2.使用JavaScript实现在页面打印九九乘法表,将编写的乘法表部署到本地IIS服务器: 3.安装sshd服务,使用S ...

  5. lixuxmint系统定制与配置(3)-字体

    小书匠Linux 有些系统自带的字体实在太难看了,看起来不清晰,不明确,有一个好的字体,可以带来好心情,并提高工作与效率. 1.常用中文字体 文泉驿微黑,微软雅黑,思源黑体 2.字体安装 2.1检查已 ...

  6. yyy

    def delete(ps): import os filename = ps[-] delelemetns = ps[] with open(filename, encoding='utf-8') ...

  7. mybatis-sqlite日期类型对应关系

    1.问题 sqlite数据库 user表,create_time字段,类型DATETIME,设置默认值datetime('now') mybatis,User实体,createTime类型为java. ...

  8. 2018-2019-2 网络对抗技术 20165212 Exp6 信息搜集与漏洞扫描

    2018-2019-2 网络对抗技术 20165212 Exp6 信息搜集与漏洞扫描 原理与实践说明 1.实践原理 信息搜集:渗透测试中首先要做的重要事项之一,搜集关于目标机器的一切信息 间接收集 D ...

  9. PCR | RT-PCR 的原理及应用

    生物的东西必须要主动去了解,否则视野容易受到限制,尤其是分子生物学的核心技术. PCR - 聚合酶链式反应 The Complete Guide to PCR (How it Works, Prime ...

  10. BeanDefinitionParserDelegate与资源解析

    继续上一次的分析 XmlBeanDefinitionReader.java 中 1.registerBeanDefinitions方法 documentReader.registerBeanDefin ...