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的更多相关文章

  1. LeetCode 387. First Unique Character in a String (字符串中的第一个唯一字符)

    题目标签:String, HashMap 题目给了我们一个 string,让我们找出 第一个 唯一的 char. 设立一个 hashmap,把 char 当作 key,char 的index 当作va ...

  2. 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 ...

  3. [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 ...

  4. 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 ...

  5. [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 ...

  6. leetcode修炼之路——387. First Unique Character in a String

    最近公司搬家了,有两天没写了,今天闲下来了,继续开始算法之路. leetcode的题目如下: Given a string, find the first non-repeating characte ...

  7. [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 ...

  8. *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 ...

  9. 【LeetCode】387. First Unique Character in a String

    Difficulty:easy  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/first-unique-cha ...

随机推荐

  1. uboot 第三天学习

    make jCPU_NUM 以CPU_NUM数量同时进行编译CPU_NUM = 当前PC的处理器数量*处理器的核心数 从上电开始1.系统上电,执行固化在IROM中的代码,目的初始化基本的系统功能,已经 ...

  2. solr 安装

    1:solr简介 solr是一个开源的搜索引擎,是对lucene做了封装,对外提供类似于webservice接口, 可以使用http请求的方式对solr进行操作. lucene.solr.elasti ...

  3. 在Azure虚拟机上安装VNC

    我们知道,Azure提供的linux虚拟机镜像是没有桌面的,是base版的,大多情况下能满足绝大部分工作需要,甚至很多习惯使用命令行的读者,反而用不惯带桌面的linux OS,但是有些情况下,桌面还是 ...

  4. lua获取时间

    更多好的文章就在 blog.haoitsoft.com,请大家多多支持! local getTime = os.date("%c"); 其中的%c可以是以下的一种:(注意大小写) ...

  5. Gerrit增加SSL证书

    在http的基础上增加SSL 配置gerrit.config文件 [gerrit] basePath = git canonicalWebUrl = https://172.16.99.212/ .. ...

  6. optparse

    Python 有两个内建的模块用于处理命令行参数: 一个是 getopt,<Deep in python>一书中也有提到,只能简单处理 命令行参数: 另一个是 optparse,它功能强大 ...

  7. selenium Remote Server 实现原理

    selenium作为一个出色的web automation框架,被越来越多的企业采用究其原因,框架设计的比较remarkable, 作为一个开源的框架,能够开辟出一套协议,以至于针对app测试的app ...

  8. win32调试打印

    void __cdecl OutputDebugStringF(const char* format ...){ va_list vlArgs; char* stringBuffer=(char*)G ...

  9. postgres索引创建、 存储过程的创建以及在c#中的调用

    postgres创建索引参考 http://www.cnblogs.com/stephen-liu74/archive/2012/05/09/2298182.html CREATE TABLE tes ...

  10. 创建Hello World程序(part-1)

    写在前面: 2006年,刚上大学,班上有几个计算机文盲,1分钟打二十几个字都困难,很不幸,我就是其中的一个.强烈的自尊心驱使我不停恶补,翻遍了图书馆的计算机文化基础,知耻而后勇...后来,C语言居然考 ...