题目描述

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.

Note: You may assume the string contain only lowercase letters.

参考答案

 class Solution {
public:
int firstUniqChar(string s) { int count[] = {};
for(char c:s){
count[c-'a']++;
}
for(size_t i = ; i<s.size();i++){
if(count[s[i] - 'a'] == ) return i;
}
return -; /*
unordered_map<char,int> map;
int min = 0; for(size_t i = 0 ; i < s.length();++i){
if(map.count(s[i])>0){
map[s[i]] = -1;
}else{
map[s[i]] = i;
}
}
for(size_t i = 0 ; i < s.length();++i){
if(map[s[i]] != -1){
return map[s[i]];
}
}
return -1;*/
}
};

补充说明

第一次自己想出了accepted的答案,虽然和第一名的大佬差了很多,但是感觉还是很兴奋的。

答案中,使用了 count [ c - 'a' ] ,巧妙地将字母表示成为index。

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

    Problem: Given a string, find the first non-repeating character in it and return it's index. If it d ...

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

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

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

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

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

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

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

随机推荐

  1. zip flags 1 and 8 are not supported解决方案

    原因是因为使用了mac自带的软件打包成了zip,这种zip包unzip命令无法解压的. 所以解决方案就是使用zip命令进行压缩,zip -r 目标文件 源文件

  2. Linux rpm 安装MySQL

    1 检查是否存在旧版本mysql (1) mysql 执行命令:rpm -qa|grep mysql,若存在旧mysql,删除查询到的旧mysql,执行: rpm -e --nodeps XXXX  ...

  3. onNewIntent

    当Activity不是Standard模式,并且被复用的时候,会触发onNewIntent(Intent intent) 这个方法,一般用来获取新的Intent传递的数据 我们一般会把MainAcit ...

  4. linux简单命令7--管道符和通配符

    ”&&“和管道符“|”不一样. ---------------------------------------------------------通配符---------------- ...

  5. maven创建父项目和子项目

    创建父级项目 new    -----    others    ------ maven project 创建好后删除,pom.xml以外的文件 点击pom.xml文件,修改Packaging属性为 ...

  6. 程序间获取ALV显示数据(读取ALV GRID上的数据)

    程序间获取ALV数据的两种方法: 方法1:通过修改SUBMIT的目标程序,把内表EXPORT到内存,SUBMIT后IMPORT ,该方法需要修改目标程序,可以任意设置目标程序的中断点: * Execu ...

  7. handlebars基础及循环使用示例

    var source = "<p>Hello, my name is {{name}}. I am from {{hometown}}. I have " + &quo ...

  8. Django -- DateTimeField

    默认为时区时间时,需要导入django内置的timezone模块 from django.utils import timezone create_at = models.DateTimeField( ...

  9. unity读取灰度图生成三维地形mesh

    准备灰度图 IGray.png及草地贴图 IGrass.jpg ,放入Assets下StreamingAssets文件夹中.     创建空材质,用作参数传入脚本.   脚本如下,挂载并传入材质球即可 ...

  10. HDU 1159 最长公共子序列

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...