给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。

案例:

s = "leetcode" 返回 0. s = "loveleetcode", 返回 2.

注意事项:您可以假定该字符串只包含小写字母。

class Solution {
public:
int firstUniqChar(string s) {
int len = s.size();
map<char, int> check;
for(int i = 0; i < len; i++)
{
check[s[i]]++;
}
for(int i = 0; i < len; i++)
{
if(check[s[i]] == 1)
return i;
}
return -1;
}
};

LeetCode387First Unique Character in a String字符串中第一个唯一字符的更多相关文章

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

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

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

  3. 387 First Unique Character in a String 字符串中的第一个唯一字符

    给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1.案例:s = "leetcode"返回 0.s = "loveleetcode&qu ...

  4. [CareerCup] 1.1 Unique Characters of a String 字符串中不同的字符

    1.1 Implement an algorithm to determine if a string has all unique characters. What if you cannot us ...

  5. [LeetCode] 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. 209. First Unique Character in a String

    Description Find the first unique character in a given string. You can assume that there is at least ...

  7. 【LeetCode】387. First Unique Character in a String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. String 字符串中含有 Unicode 编码时,转为UTF-8

    1.单纯的Unicode 转码 String a = "\u53ef\u4ee5\u6ce8\u518c"; a = new String(a.getBytes("UTF ...

  9. LeetCode_387. First Unique Character in a String

    387. First Unique Character in a String Easy Given a string, find the first non-repeating character ...

随机推荐

  1. JVM中堆栈

    1.JVM是基于堆栈的虚拟机.JVM为每个新创建的线程都分配一个堆栈.也就是说,对于一个Java程序来说,它的运行就是通过对堆栈的操作来完成的.堆栈以帧为单位保存线程的状态.JVM对堆栈只进行两种操作 ...

  2. 强连通图缩点——cf999E

    问题转换成缩点求度数为0的点的个数,s点所在联通块作额外处理 缩点写的很烂调了一早上.. #include<bits/stdc++.h> #include<vector> us ...

  3. 三模数NTT模板

    求两个多项式的卷积对任意数p取模 两个好记的FNT模数: 5*2^25+1 7*2^26+1 原根都为3 //Achen #include<algorithm> #include<i ...

  4. dart 异步

    使用异步有两种方法 then 或者 async/await. async/await 方法更易于理解,

  5. CygWin、MinGw和Msys的区别

    做了6年的Windows C++,觉得已经没什么挑战力:而且Windows C++已经没落,不得不转Linux C++: 习惯了Windows的界面,习惯了傻瓜式的VS IDE,现在遇到Linux命令 ...

  6. cocos2dx触摸响应

      Layer其实继承了触控的接口. 所以只需要重写一些函数即可.   在helloword类中重写:     virtual bool init();     /** Callback functi ...

  7. SUMMARY | JAVA中的数据结构

    String String类是不可修改的,创建需要修改的字符串需要使用StringBuffer(线程同步,安全性更高)或者StringBuilder(线程非同步,速度更快). 可以用“+”连接Stri ...

  8. 几道关于this的经典练习题的理解与分析

    1. var num = 1; var myObject = { num: 2, add: function() { this.num = 3; (function() { console.log(t ...

  9. nodejs实现读取文件

    今天后端同事下班,想让我读取一个文件的内容.我想这个是小忙啊,就立马答应了. 我知道这个是nodejs读取,可是我又想,平时我们都要起一个服务才能够运行node服务器, 比如如下代码 var http ...

  10. 「题解」NOIP模拟测试题解乱写I(29-31)

    NOIP模拟29(B) T1爬山 简单题,赛时找到了$O(1)$查询的规律于是切了. 从倍增LCA那里借鉴了一点东西:先将a.b抬到同一高度,然后再一起往上爬.所用的步数$×2$就是了. 抬升到同一高 ...