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 ...
随机推荐
- Java开源库
Java SE 7 API Docs from Oracle Apache IO库操作IO与文件 2.4 XML4j 1.6.1 Json.org google-json 2.5 WindowBuil ...
- Linux(Ubuntu)下安装NodeJs
用以下命令来升级系统,并且安装一些Node.JS必要的包. Linux(Ubuntu)下安装NodeJs 安装nodeJS之前,如果没有安装g++ make libssl-dev等, 1.更新系统和依 ...
- [Python] from scipy import sparse 报 DLL load failed:找不到指定模块错误
依赖vc运行环境.需要安装 vc_redist Py3.5要安装2015版 传送门: https://www.microsoft.com/zh-CN/download/details.aspx?id= ...
- log4net 2.0.4有问题,AdoNetAppender会报错
坑死老子了 <appSettings> <add key="log4net.Internal.Debug" value="true"/> ...
- centos 7 配置网络
文档: https://wiki.centos.org/FAQ/CentOS7
- CAD打印线条太粗、线条颜色设置
不管你是使用打印机,还是将CAD转换为PDF文件,如果出现以下情况,线条太粗,根本看不清楚,怎么解决呢? 或者,不想通过图层复杂.繁琐的设置,想将各种颜色线条的CAD全部打印成黑白,或者指定某一种颜色 ...
- 关于SimpleAdapter和ListView结合使用,实现列表视图的笔记
使用ListView需要为其添加适配器: 适配器有两种:1.ArrayAdapter --用于单独文字显示 2.SimpleAdapter --用于文字和图片显示 这里主要记录SimpleAdapt ...
- 利用chrome插件批量读取浏览器页面内容并写入数据库
试想一下,如果每天要收集100页网页数据甚至更多.如果采用人工收集会吐血,用程序去收集也就成为一个不二的选择.首先肯定会想到说用java.php.C#等高级语言,但这偏偏又有个登陆和验证码,搞到无所适 ...
- 关于Vue vuex vux 文档
01. vue 链接 http://vuejs.org.cn/guide/ 02. vuex ----->>状态管理模块儿<<------- https://vuex.vue ...
- STM32F412应用开发笔记之六:使用片上Flash存储参数
我们的项目中需要保存一些系统配置参数,这些数据的特点是:数量少而且不需要经常修改,但又不能定义为常量,因为每台设备可能不一样而且在以后还有修改的可能.这就需要考虑这些参数保存的问题.将这类数据存在指定 ...