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 doesn't exist, return -1.
Example:
s = "leetcode"
return 0. s = "loveleetcode",
return 2.
本题的第一想法是开一个字母表计数器,遍历string s中的元素,并利用计数器对每一个字母计数。最后再次遍历s,查找计数为1的字符然后返回其索引。该解法代码如下:
class Solution {
public:
int firstUniqChar(string s) {
int *alphabet = NULL;
alphabet = new int[]();
for(int i = ; i < s.length(); i++)
{
alphabet[int(s[i] - 'a')]++;
}
for(int i = ; i < s.length(); i++)
{
if(alphabet[int(s[i] - 'a')] == )
{
return i;
}
}
return -;
}
};
但是,若string非常长,两次遍历则会带来很大的开销,因此,可以考虑一次遍历,用hash table记录每个字母的次数和索引,其代码如下:
class Solution {
public:
int firstUniqChar(string s) {
unordered_map<char, pair<int,int>> m;
int idx = s.length();
for(int i = ; i < s.length(); i++)
{
m[s[i]].first++;
m[s[i]].second = i;
}
for(auto &p:m)
{
if(p.second.first == )
{
idx = min(idx, p.second.second);
}
}
return idx == s.length()? - : idx;
}
};
LeetCode 387. First Unique Character in a String的更多相关文章
- LeetCode 387. First Unique Character in a String (字符串中的第一个唯一字符)
题目标签:String, HashMap 题目给了我们一个 string,让我们找出 第一个 唯一的 char. 设立一个 hashmap,把 char 当作 key,char 的index 当作va ...
- 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 ...
- [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 ...
- 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 ...
- [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 ...
- leetcode修炼之路——387. First Unique Character in a String
最近公司搬家了,有两天没写了,今天闲下来了,继续开始算法之路. leetcode的题目如下: Given a string, find the first non-repeating characte ...
- [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 ...
- *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 ...
- 【LeetCode】387. First Unique Character in a String
Difficulty:easy More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/first-unique-cha ...
随机推荐
- 1.openGL 初探
GLEW, GLFW和GLM介绍 现在你有了工程,就让我们开始介绍下工程所用到的开源库和为啥需要这些. The OpenGL Extension Wrangler (GLEW)是用来访问OpenGL ...
- jquery-fullpage-js制作页全屏滚动插件
有一个很棒的插件 http://www.ijquery.cn/demo/fullPage/
- JS学习:第二周——NO.3盒子模型
1.CSS盒子模型包括四个部分组成:设定的宽高+padding+border+margin: 2.JS盒子模型:通过系统提供的属性和方法,来获取当前元素的样式值 JS提供的属性和方法: clien ...
- android Viewpager HorizontalScrollView 实现分页栏拖拽
源码:http://files.cnblogs.com/android100/ViewPaperDemo.rar首先我们先看一个效果: 前两个是网易的,它做的title不能拖拽,.不过点击动画效果挺 ...
- file_get_contents带bom
$dmText = file_get_contents( AROOT .'data' . DS . 'DMType.json.php'); if(preg_match('/^\xEF\xBB\xBF/ ...
- 客户有两台windows服务器要做sql server双机切换
基本架构 2 windows 2008 server:安装成域控制器,实现故障转移(虚拟ip访问,共享磁盘阵列卷链接主服务器),安装sqlserver2012 1磁盘阵列共享卷:数据库文件放于其中,两 ...
- Noip2016 总结&反思
一直在期盼的联赛,真正来临时,却远不像我想象的样子. 有些事,真的不敢再想. 算法可以离线,时光却不能倒流.dfs可以回溯,现实却没有如果. 有些事,注定只能成为缺憾,抱恨终生. 不得不说今年Noip ...
- 使用Dir,遍历文件夹下所有子文件夹及文件
'------------------------------------------- '获取某文件夹下所有文件和子目录下的文件 '--------------------------------- ...
- 【java回调】java两个类之间的回调函数传递
背景交代:熟悉用js开发的cordovaAPP:对java一窍不通的我,老师让做一个监测用户拍照事件的功能,无奈没有找到现成的库,无奈自己动手开发java插件~~0基础java GreenHand,祝 ...
- 【bb平台刷课记】wireshark结合实例学抓包
[bb平台刷课记]wireshark结合实例学抓包 背景:本校形势与政策课程课需要在网上观看视频的方式来修得学分,视频网页自带"播放器不可快进+离开窗口自动暂停+看完一集解锁下一集(即不能同 ...