LeetCode_387. First Unique Character in a String
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 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的更多相关文章
- 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 ...
- 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 ...
- LeetCode 387. First Unique Character in a String (字符串中的第一个唯一字符)
题目标签:String, HashMap 题目给了我们一个 string,让我们找出 第一个 唯一的 char. 设立一个 hashmap,把 char 当作 key,char 的index 当作va ...
- [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 ...
- 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 ...
- 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 ...
- LeetCode First Unique Character in a String
原题链接在这里:https://leetcode.com/problems/first-unique-character-in-a-string/ 题目: Given a string, find t ...
- leetcode修炼之路——387. First Unique Character in a String
最近公司搬家了,有两天没写了,今天闲下来了,继续开始算法之路. leetcode的题目如下: Given a string, find the first non-repeating characte ...
- 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 ...
随机推荐
- oracel: 通过特殊表序列来实现oracle自增id (mybatis实现自增id)
本项目结合mybaits来使用. 加入有一个用户表:sys_user, 自增id为user_id. 怎么来实现id的自增呢? 通过sql语句select * from user_sequences,检 ...
- Set的常用实现类HashSet和TreeSet
Set HashSet public static void main(String[] args) { //不可以重复 并且是无序的 //自然排序 从A-Z //eqauls从Object继 ...
- .Net Core WebApi实现跨域
.Net Core 需要引用一个包 Microsoft.AspNetCore.Cors 让接口实现跨域,需要配置两个地方. 一.Startup.cs 这里需要配置两个地方 public void C ...
- (尚001)Vue框架介绍
框架出现时间: Angular -->React(组件化+虚拟动) -->Vue(读作view) 1.Vue.js是什么?(作者:尤雨溪(一位华裔前Google工程师)) 尤 ...
- bzoj 5299: [Cqoi2018]解锁屏幕 状压dp+二进制
比较简单的状压 dp,令 $f[S][i]$ 表示已经经过的点集为 $S$,且最后一个访问的位置为 $i$ 的方案数. 然后随便转移一下就可以了,可以用 $lowbit$ 来优化一下枚举. code: ...
- 机器学习---用python实现感知机算法和口袋算法(Machine Learning PLA Pocket Algorithm Application)
之前在<机器学习---感知机(Machine Learning Perceptron)>一文中介绍了感知机算法的理论知识,现在让我们来实践一下. 有两个数据文件:data1和data2,分 ...
- Sage Math中的语法
1.赋值后不能立即输出,而需要停顿.x= 3 不能输出显示,而 x= 3; x 可以显示. 2.可以用分号连续书写多行. 3.矩阵可以用 mtx[i, j]引用,但是行列号通常从0开始,维度n, m ...
- java如何实现多线程?线程的状态有哪些?
java实现多线程有两种方法 1.继承Thread类 2.实现Runnable接口 这两种方法的共同点: 不论用哪种方法,都必须用Thread(如果是Thead子类就用它本身) ...
- Iptables 之二扩展模块 nat
问题一:如果开发被动模式的ftp服务? 21号端口是命令连接端口,数据连接端口不固定 三步骤: (1)装载ftp追踪时的专用的模块 /lib/modules/$(uname-r)/kernel/ker ...
- 怎么把分化成元,并且保留两位小数,用vue来做
<el-table-column prop="amount" label="申请提现金额" width="120" align=&qu ...