原题链接在这里: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. 为 placeholder 自定义样式

    textarea::-webkit-input-placeholder{ padding: 1em; } textarea::-moz-placeholder{ padding: 1em; } 同理, ...

  2. 【基础知识】UML基础

    http://www.ibm.com/developerworks/cn/rational/r-uml/

  3. 无废话ExtJs 入门教程十七[列表:GridPanel]

    无废话ExtJs 入门教程十七[列表:GridPanel] extjs技术交流,欢迎加群(201926085) 在Extjs中,GridPanel用于数据显示,即我们平时说的列表页.在本节中,我们先对 ...

  4. Win10 Migrate apps to the Universal Windows Platform (UWP)

    https://msdn.microsoft.com/en-us/library/mt148501.aspx

  5. Dubbo详细介绍与安装使用过程

    今天看到一篇不错的dubbo介绍教程,原文链接:http://blog.csdn.net/xlgen157387/article/details/51865289 1 Dubbo介绍 1.1 dubb ...

  6. call()与apply()

    1.obj1.method1.call(obj2,argument1,argument2) call的作用就是把obj1的方法放到obj2上使用 2. add 来替换 sub,add.call(sub ...

  7. 首师大附中互测题:99999999海岛帝国后传:算法大会【D001】

    [D001]99999999海岛帝国后传:算法大会[难度:D] ———————————————————————————————————————————————————————————————————— ...

  8. 朋友圈常见单页面触屏滑动上下翻屏功能jQuery实现

    翻页插件:实现原理,用margin-top来控制页面容器位置来实现上下翻页.margin这属性很好用,可以用来制作侧栏动画滑出菜单(左菜单,右内容,控制两者的margin实现):或者head下滑菜单 ...

  9. 洛谷 P2735 电网 Electric Fences Label:计算几何--皮克定理

    题目描述 在本题中,格点是指横纵坐标皆为整数的点. 为了圈养他的牛,农夫约翰(Farmer John)建造了一个三角形的电网.他从原点(0,0)牵出一根通电的电线,连接格点(n,m)(0<=n& ...

  10. phpcmsv9自定义sql语句查询模型实现

    在phpcmsv9中,自定义sql语句查询可不太好实现,传入sql语句查询很容易被内部转入生成一系列莫名其妙的sql语句,比如最佳前缀等等,直接造成sql语句查询错误,在此也提供两种解决办法,1修改底 ...