原题链接在这里:https://leetcode.com/problems/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.

题解:

扫两遍string. 第一遍s.chatAt(i)的对应count++. 第二遍找第一个count为1的char, return其index.

Time Complexity: O(s.length()). Space: O(1), 用了count array.

AC Java:

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

类似Sort Characters By Frequency.

LeetCode First Unique Character in a String的更多相关文章

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

  2. LeetCode_387. First Unique Character in a String

    387. First Unique Character in a String Easy Given a string, find the first non-repeating character ...

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

  4. 【LeetCode】387. First Unique Character in a String

    Difficulty:easy  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/first-unique-cha ...

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

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

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

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

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

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

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

随机推荐

  1. 根据字体计算CGRect

    UILabel *label = [[UILabel alloc]init]; label.numberOfLines = ;//多行显示 label.backgroundColor = [UICol ...

  2. PHP操作数据库

    一.PHP连接到MySQL // //比较规范的写法是地址,登录名,密码这样写,比较安全 define("DB_HOST", 'localhost'); define('DB_US ...

  3. Linux命令總結

    查看指定端口的監聽 lsof -i tcp:1521

  4. github之git基本命令介绍的简单整理

    git 格式: git [--version] [--exec-path[=<path>]] [--html-path] [--info-path] [-p|--paginate|--no ...

  5. 【MongoDB】5.MongoDB与java的简单结合

    1.首先 你的清楚你的MongoDB的版本是多少  就下载对应的架包 下载地址如下: http://mongodb.github.io/mongo-java-driver/ 2.新建一个项目  把架包 ...

  6. MIT 6.828 JOS学习笔记9. Exercise 1.5

    Lab 1 Exercise 5 再一次追踪一下boot loader的一开始的几句指令,找到第一条满足如下条件的指令处: 当我修改了boot loader的链接地址,这个指令就会出现错误. 找到这样 ...

  7. SOAPUI使用教程-MockServices工作原理

    在soapUI的可让您只需从WSDL基础服务创建一个基于WSDL服务的符合标准的模拟.被称为“MockService”,这可以直接从内部的soapUI运行,命令行浇道,或甚至标准servlet容器. ...

  8. Leetcode Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  9. Java的算数运算符、关系运算符、逻辑运算符、位运算符

    JAVA的运算符,分为四类: 算数运算符.关系运算符.逻辑运算符.位运算符 算数运算符(9):+  -  *  /  %  ++  -- 关系运算符(6):==  !=  >  >=  & ...

  10. JS字符串与汉字的字节获取

    JS英文为一个字节,中文GBK为3个字节,UTF-8为2个字节. 1.通过for循环 function getStrLeng(str){ var realLength = 0; var len = s ...