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.
分析:找出字符串中第一次出现且不重复的字符,返回其字符的下标,如果不存在就返回-1.
用hashMap进行求,hashMap中可以通过ey-value的存储位置对和位置,对数据进行标记
public int firstUniqChar(String s) {
Map<Character,Integer> map=new HashMap<Character,Integer>();
for(int i=0;i<s.length();i++){
char c=s.charAt(i);
if(map.containsKey(c)){
map.put(c,-1);
}else{
map.put(c,1);
}
}
for(int i=0;i<s.length();i++){
if(map.get(s.charAt(i))==1){
return i;
}
}
return -1;
}
387. First Unique Character in a String的更多相关文章
- LeetCode 387. First Unique Character in a String (字符串中的第一个唯一字符)
题目标签:String, HashMap 题目给了我们一个 string,让我们找出 第一个 唯一的 char. 设立一个 hashmap,把 char 当作 key,char 的index 当作va ...
- 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修炼之路——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 ...
- [LeetCode&Python] Problem 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第一个不重复字母
Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...
- 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 ...
- *387. First Unique Character in a String (linkedhashmap + string) debug manunally instead of using leetcode
The ability to debug and overall thinking need to improve Given a string, find the first non-repeati ...
- 【LeetCode】387. First Unique Character in a String
Difficulty:easy More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/first-unique-cha ...
随机推荐
- 从零自学Hadoop(18):Hive的CLI和JDBC
阅读目录 序 Hive CLI(old CLI) Beeline CLI(new CLI) JDBC Demo下载 系列索引 本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出 ...
- Oracle基础——学习笔记
一[用户]sys\system\sysman\scott 1.查看数据库所有用户(dba_users数据字典): select username from dba_users; 2.查看当前用户: s ...
- nginx 网站搭建
nginx目录详解 默认nginx做了nginx配置文件的备份 #查看nginx配置文件去掉#号的内容,并且追加到nginx.conf.tmp egrep -v "#|^$" ng ...
- 【Linux】重定向与管道
重定向 redirection 每个命令有输入源和输出目的地,默认行为,是标准输入和标准输出.大多数情况,标准输入是键盘,标准输出是屏幕.可以为单独的操作修改输入和输出,这就是重定向.重定向可以使某个 ...
- css3
CSS3的换行 如果某个单词太长,不适合在一个区域内,它扩展到外面: 自动换行属性允许您强制文本换行 - 即使这意味着分裂它中间的一个字: 允许长文本换行: p {word-wrap:break-wo ...
- android socket 线程连接openwrt与arduino单片机串口双向通信
package zcd.netanything; import java.io.BufferedReader; import java.io.InputStreamReader; import jav ...
- NHibernate常见问题及解决方法
NHibernate常见问题及解决方法 曾经学过NHibernate的,但是自从工作到现在快一年了却从未用到过,近来要巩固一下却发现忘记了许多,一个"in expected: <end ...
- 唯物 VS 唯心
对于唯物和唯心,我了解的并不多.我所知道的那点皮毛,也只是源于教课书.只是近来恋上了阳明大哥,而在某年的高考模拟题中,阳明竟被作为唯心主义的代表,供考生们批判.这一批搞得我着实不爽,决定要为他平反 ...
- [LeetCode] Plus One 加一运算
Given a non-negative number represented as an array of digits, plus one to the number. The digits ar ...
- 红豆电信JS
http://www.hodo170.com/login.jsp 先定义window,再定义RSAUtils=window.RSAUtils || a.RSAUtils; /* * RSA, a su ...