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. RCNN,Fast RCNN,Faster RCNN 的前生今世:(3) SPP - Net

    SPP-Net是出自2015年发表在IEEE上的论文-<Spatial Pyramid Pooling in Deep ConvolutionalNetworks for Visual Reco ...

  2. log4j+junit+maven

    本文在开发第一个maven示例的基础上进行扩展. 日志级别测试 在src\main\resources文件夹下新建log4j.properties log4j.rootLogger = warn,st ...

  3. go同步互斥锁

    import "sync" var ( myMap = make(map[int]int, 10) lock sync.Mutex //声明一个全局的互斥锁 //sync 包 同步 ...

  4. Linux 命令-egrep

    egrep 可以看着是grep的扩展,参数-e支持正则匹配 满足其中任一条件 egrep -e "tiaojian1|tiaojian2" test.txt

  5. 交互设计算法基础(3) - Quick Sort

    int pivotIndex, pivot, swapIndex; void swap(int[] arr, int x, int y) { int temp = arr[x]; arr[x] = a ...

  6. 笔记-读官方Git教程(1)~认识Git

    小书匠版本管理 教程内容基本来自git官方教程,认真都了系列的文章,然后对一些重点的记录下来,做了简单的归纳并写上自己的思考. 目录: 1.Git介绍 2.Git版本控制原理 3.Git特点 4.Gi ...

  7. 洛谷P1325 雷达安装

    题目 考虑对于一个小岛,如果有雷达可以覆盖它,则这些雷达肯定在一个区间里,则原题内容则变为区间选点问题 #include <bits/stdc++.h> using namespace s ...

  8. npropress进度条插件的使用

    官网下载地址:http://ricostacruz.com/nprogress/ npropress.css /* Make clicks pass-through */ #nprogress { p ...

  9. raycaster选取捕获obj模型&&选中高亮代码

    目录 raycaster选取捕获obj模型&&选中高亮代码 raycaster关键代码 选中高亮代码 obj整体上色 raycaster选取捕获obj模型&&选中高亮代 ...

  10. [NOI.AC]NOI2019省选模拟赛 第二场

    传送门 Solution A. 一共有\(T\)组数据 每次询问你\([l,r]\)中有多少个数能被他的所有数位整除(如果数位中含有\(0\)忽略掉) 数位dp,咕咕咕 B. 题面略 考虑一个个只有两 ...