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. Python 百分号字符串拼接

    # %s可以接收一切 %d只能接收数字 msg = 'i am %s my hobby is %s' %('lhf','alex') print msg msg2 = 'i am %s my hobb ...

  2. Python集合的基本操作

    #-*coding:utf-8 -* list =set([2,3,4]) list2 =set([5,3,7]) #交集 #print (list.intersection(list2)) #并集 ...

  3. js ajax 数据获取

    在js中应用ajax 获取数据的方法,也写一个出来供复习所用 1.建议一个user.json 文件如下,保存名字为 user.json { "name": "huanyi ...

  4. sublime text3:sublime text3本地服务器方式运行文件

    网址:https://blog.csdn.net/md1688/article/details/70562381 1.Ctrl + Shift +P,启动Sublime Text的命令行(如果没有需要 ...

  5. Python3.7中urllib.urlopen 报错问题

    import urllib web = urllib.urlopen('https://www.baidu.com') f = web.read() print(f) 报错: Traceback (m ...

  6. Servlet之ServletContext获取web上下文路径、全局参数、和Attribute(域)

    1)获取web上下文路径 public void doGet(HttpServletRequest request, HttpServletResponse response) throws Serv ...

  7. ARP协议,以及ARP欺骗

    1.定义: 地址解析协议,即ARP(Address Resolution Protocol),是根据IP地址获取物理地址的一个TCP/IP协议.主机发送信息时将包含目标IP地址的ARP请求广播到网络上 ...

  8. sp_executesql 或者 EXECUTE 执行动态sql的权限问题

    当 sp_executesql 或 EXECUTE 语句执行字符串时,字符串将作为它的自包含批处理执行.SQL Server 会将字符串中的一个或多个 Transact-SQL 语句编译为独立于批处理 ...

  9. ArcGIS案例学习笔记3_1_地理配准案例_目视找点

    ArcGIS案例学习笔记3_1_地理配准案例_目视找点 计划时间:第3天上午 方法:地理配准/添加链接点/左键/右键/输入坐标 数据:江苏省.zip 矢量:省界,市界,GPS WGS84 地理坐标系 ...

  10. 转换es6

    { "presets": [["env", { "modules": false }],"stage-3"," ...