原题链接在这里: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. sqlserver2000 数据库 'tempdb' 的日志已满

    方法一解决过程: 查看了下数据库的属性,是自动增长,不指定文件大小上限.在网上Google了很久,试了些方法都不行:数据库所在磁盘还有很大的可用空间,试着下重药了.直接把tempdb的数据文件和日志文 ...

  2. PHP日期与时间

    时间戳是自 1970 年 1 月 1 日(00:00:00 GMT)以来的秒数.它也被称为 Unix 时间戳(Unix Timestamp).Unix时间戳(Unix timestamp),或称Uni ...

  3. 计算机网络中的帧封装(C实现)

    这段时间开始复习计算机网络,看到帧封装这一节,结合以前的课程设计,就用C写了个帧封装的程序,说实话C学的确实不怎么样,实现的时候对于文件操作那部分查了好多资料,下面说说帧封装是啥情况. 学过计算机网络 ...

  4. cacti添加主机监控

    登入cacti后 主机-->添加-->填入信息.如下图所示 填好后点击右下角添加 添加完.左上角出现如下信息.说明被监控的服务器snmp工作正常. 然后咱们给这个主机添加图形. 当前主机模 ...

  5. 协同过滤和简单SVD优化

    协同过滤(collaborative filtering) 推荐系统: 百度百科的定义是:它是利用电子商务网站向客户提供商品信息和建议,帮助用户决定应该购买什么产品,模拟销售人员帮助客户完成购买过程主 ...

  6. ubuntu wifi连接不上或经常断网,重启就好

    问题1.知道wifi密码,驱动也有,可以点击连接,总是提示"连接断开,您现在处于离线状态". 1.打开终端"ctrl+alt+T" 2.输入: sudo vim ...

  7. 【Mybatis框架】查询缓存(一级缓存)

    做Java的各位程序员们,估计SSH和SSM是我们的基础必备框架.也就是说我们都已经至少接触过了这两套常见的集成框架.当我们用SSH的时候,相信很多人都接触过hibernate的两级缓存,同样,相对应 ...

  8. 用 TWebBrowser 查找网页上的按钮,编辑框,

    Form1.wb1.Navigate(Aurl); <table> <tr> <td style="text-align:right;">< ...

  9. 求两条线段交点zz

    "求线段交点"是一种非常基础的几何计算, 在很多游戏中都会被使用到. 下面我就现学现卖的把最近才学会的一些"求线段交点"的算法说一说, 希望对大家有所帮助. 本 ...

  10. 【NOI2016】优秀的拆分 题解(95分)

    题目大意: 求一个字符串中形如AABB的子串个数. 思路: 用哈希做到O(1)判断字符串是否相同,O($n^2$)预处理,ans[i]为开头位置为i的形如AA的子串个数.再用O($n^2$)枚举出AA ...