LeetCode First Unique Character in a String
原题链接在这里: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的更多相关文章
- [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 ...
- 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 ...
- 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 ...
- 【LeetCode】387. First Unique Character in a String
Difficulty:easy More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/first-unique-cha ...
- 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 ...
- LeetCode 387. First Unique Character in a String (字符串中的第一个唯一字符)
题目标签:String, HashMap 题目给了我们一个 string,让我们找出 第一个 唯一的 char. 设立一个 hashmap,把 char 当作 key,char 的index 当作va ...
- 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 ...
- leetcode修炼之路——387. First Unique Character in a String
最近公司搬家了,有两天没写了,今天闲下来了,继续开始算法之路. leetcode的题目如下: Given a string, find the first non-repeating characte ...
- 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 ...
随机推荐
- Python 迭代器 & __iter__方法
转载来自: http://blog.csdn.net/bluebird_237/article/details/38894617 迭代器就是重复地做一些事情,可以简单的理解为循环,在python中实现 ...
- AgileEAS.NET SOA 中间件平台.Net Socket通信框架-简单例子-实现简单的服务端客户端消息应答
一.AgileEAS.NET SOA中间件Socket/Tcp框架介绍 在文章AgileEAS.NET SOA 中间件平台Socket/Tcp通信框架介绍一文之中我们对AgileEAS.NET SOA ...
- sqlmap的安装方法
1.安装Python2.7,设置Python环境变量
- HYSBZ 2002 分块
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2002 题意:中文题面 思路:考虑分块,每个位置维护一个跳出该块需要的步数cnt[],和跳出 ...
- psql-08表:触发器
语句级触发器与行为触发器 //创建一个对student表的操作记录表 create table log( update_time timestamp, //操作时间 db_user varchar(4 ...
- Validation-jQuery表单验证插件使用方法
http://www.cnblogs.com/shuang121/archive/2012/04/23/2466628.html 作用 jquery.validate是jquery旗下的一个验证框架, ...
- 虚拟机和windows主机中的文件共享
22:54 2015/12/22 虚拟机和windows主机中的文件共享:特别推荐:我的一个老师特别推荐的方法:在windows安装SSH Secure File Transfer Client,直接 ...
- 【POJ3254】Corn Fields 状压DP第一次
!!!!!!! 第一次学状压DP,其实就是运用位运算来实现一些比较,挺神奇的.. 为什么要发“!!!”因为!x&y和!(x&y)..感受一下.. #include <iostre ...
- Django分析之导出为PDF文件
最近在公司一直忙着做exe安装包,以及为程序添加新功能,好久没有继续来写关于Django的东西了….难得这个周末清闲,来了解了解Django的一些小功能也是极好的了~ 那今天就来看看在Django的视 ...
- 【Raspberry Pi】新入手的Raspberry Pi3拼装日志
一.概述 2016年暑假某宝入手Raspberry Pi 3,装机清单: 树莓派主板 亚克力外壳 小风扇 散热片 30G SD card 螺丝若干颗 因机型问题,可能与你的机器有微小差异 二.装机过程 ...