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. linux 一个跟踪文件删除的小技巧

    最近有同事问我说他有个现场环境,经常会丢失业务文件,每天都出现,几百个里面丢失1到两个. 为了解决这个问题,我让他布置audit,具体可以man一下auditctl. 过了一天,他说audit.log ...

  2. Java泛型类型擦除以及类型擦除带来的问题

    目录 1.Java泛型的实现方法:类型擦除 1-2.通过两个例子证明Java类型的类型擦除 2.类型擦除后保留的原始类型 3.类型擦除引起的问题及解决方法 3-1.先检查,再编译以及编译的对象和引用传 ...

  3. gitbash上使用tree

    gitbash上使用tree vscode从cmd设置gitbash之后,想在使用windows下的tree命令发现运行不了,有两种解决方案. 1,在gitbash上cmd //c tree,就等同c ...

  4. 删除node_modules文件夹

    老版本的npm对有node_modules文件夹太长的问题,新版本就没有这个问题.2.7? npm install rimraf -g rimraf node_modules

  5. Hibernate 再接触 多对多单向双向关联

    情景:一个老师可能有多个学生,一个学生也可能有多个老师 多对一单向: 例如老师知道自己教哪些学生,学生却不知道自己被哪些老师教 方法:使用第三张表 分别存两张表的id annotation Stude ...

  6. (转)C#如何加载程序运行目录外的程序集

    https://www.cnblogs.com/guanglin/p/3200989.html 我们的应用程序部署的时候,目录结构一般不会只有运行程序的目录这一个,我们可能在运行目录下建子目录,也可能 ...

  7. SQL Server 2008读取域帐户信息

    参考:http://www.pawlowski.cz/2011/04/querying-active-directory-sql-server-t-sql/ 1.建立  link server . u ...

  8. Android sdk 目录结构说明

    1.add-on:附加的包:2.docs:HTML格式的离线文档:3.platforms:sdk核心内容:4.tool:工具. 在platforms中包含了的各个Android SDK版本的目录中,包 ...

  9. php yii 命令

    php yii 敲回车 This is Yii version 2.0.11. The following commands are available: - asset Allows you to ...

  10. 项目IDEA启动配置

    在所有java启动项中加入 -Djute.maxbuffer=2048000 tomcat 在catalina.bat 中第一行加入 set JAVA_OPTS=-Djute.maxbuffer=20 ...