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.

思路

1.  use int array to simplify a hashmap

2. one pass to mark each char's occurrence

3. second pass to check 1st index whose char's occurrence == 1

代码

 class Solution {
public int firstUniqChar(String s) {
int map [] = new int[256];
for(int i = 0; i < s.length(); i ++)
map [s.charAt(i) ] ++;
for(int i = 0; i < s.length(); i ++)
if(map [s.charAt(i) ] == 1)
return i;
return -1;
}
}

[leetcode]387. First Unique Character in a String第一个不重复字母的更多相关文章

  1. [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 ...

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

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

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

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

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

  7. 387 First Unique Character in a String 字符串中的第一个唯一字符

    给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1.案例:s = "leetcode"返回 0.s = "loveleetcode&qu ...

  8. [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 ...

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

随机推荐

  1. TCP连接异常断开检测(转)

    TCP是一种面向连接的协议,连接的建立和断开需要通过收发相应的分节来实现.某些时候,由于网络的故障或是一方主机的突然崩溃而另一方无法检测到,以致始终保持着不存在的连接.下面介绍一种方法来检测这种异常断 ...

  2. 使用arguments对象验证函数的参数是否合法

    <script>function sum(arg1,arg2) //加法函数{ var realArgCount = arguments.length; //调用函数时传递的实参个数 va ...

  3. windows和linux下如何远程获取操作系统版本和主机名

    远程获取windows和linux操作系统版本和主机名需要具备以下条件: 假设 主机A(windows 7),ip:192.168.12.2 主机B(centos 6.3),ip:192.168.12 ...

  4. StarRatingBar星星切换动画《IT蓝豹》

    StarRatingBar星星切换动画 StarRatingBar星星切换动画,很久没有学习一下这个RatingBar了,今天来看看这个RatingBar的动画切换效果,本例子主要是RatingBar ...

  5. 吴裕雄 05-mysql删除数据库

    drop database <数据库名>; 例如删除名为 RUNOOB 的数据库:drop database RUNOOB; 使用 mysqladmin 删除数据库你也可以使用 mysql ...

  6. HTTPConnectionPool(host:XX)Max retries exceeded with url

    爬虫多次访问同一个网站一段时间后会出现错误 HTTPConnectionPool(host:XX)Max retries exceeded with url '<requests.package ...

  7. tensorflow的save和restore

    使用tensorflow中的save和restore可以对模型进行保存和恢复 import tensorflow as tf v1 = tf.Variable(tf.random_normal([1, ...

  8. js高级-作用域链

    作用域链存放的就是 VO  AO 参数 变量 等

  9. Java的学习路线图

    在网上看到一个关于Java的学习路线图,个人感觉很详细.https://blog.csdn.net/s1547823103/article/details/79768938

  10. win10自带IE上不了网的解决办法

    1.cmd以管理员身份运行powershell. 2.输入以下三条程序. netsh int tcp set heuristics disabled 回车执行后再输入 netsh int tcp se ...