题目描述

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. POI的XWPFTable的方法总结

    1. void  addNewCol(): 为该表中的每一行添加一个新列 2. void addRow(XWPFTableRow row): 向表中添加新行 3. boolean addRow(XWP ...

  2. 例子:Vue 配合 vue-resource 从接口获取数据

    vue-resource 是 vue 的一个与服务器端通信的 HTTP 插件,用来从服务器端请求数据. 结合例子——图片列表来写一下 Vue获取接口数据. html : <div id=&quo ...

  3. js中的splice方法和slice方法简单总结

    slice:是截取用的 splice:是做删除 插入 替换用的 slice(start,end): 参数: start:开始位置的索引 end:结束位置的索引(但不包含该索引位置的元素) 例如: va ...

  4. opencv_traincascade级联训练人脸数据

    正负样本格式:  正样本 灰度化 24*24 2000张  负样本 灰度化 50*50 1000张 训练过程  第一步:dir /b >pos.txt 以及dir /b >neg.txt ...

  5. Android检测服务是否运行

    直接上代码: /** * 判断服务是否后台运行 * * @param context * Context * @param className * 判断的服务名字 * @return true 在运行 ...

  6. Windows 10 搭建Python3 安装使用 protobuf

    Protobuf对比XML.Json等其他序列化的优势 protobuf 不管是处理时间上,还是空间占用上都优于现有的其他序列化方式.内存暂用是java 序列化的1/9,时间也是差了一个数量级,一次操 ...

  7. Qt编写自定义控件28-颜色滑块面板

    一.前言 相比于上一个颜色按钮面板,此控件就要难很多,颜色值有三种表示形式,除了程序员最常用的RGB以外,还有HSB和CMY方式. RGB色彩模式是工业界的一种颜色标准,是通过对红(R).绿(G).蓝 ...

  8. Entity Framework链接数据库设置

    本人不才,学习EntityFramwork同时做个记录供大家参考.不多说,直接上步骤 1.在WebConfig中添加如下代码段 <connectionStrings> <add na ...

  9. python中dict对象和字符串string对象互相转换

    使用json包 import json dict1 = {"A":"a","B":"b"} # 转换为字符串 json. ...

  10. 利用官方的ucosiii包中测试板的工程移植到属于自己的开发板(stmf103ZE)上

    ucosIII官方下载地址:https://www.micrium.com 第一:是不是ucosIII:第二,工具链是不是keil(我用的是keil,如何用的是IAR就选有IAR的):第三MCU是不是 ...