<LeetCode OJ> 387. First Unique Character in a String

  • Total Accepted: 4516 
  • Total Submissions: 10603 
  • Difficulty: Easy

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) {
        map<char,int> mapping;
        vector<int>  record(26,0);
        bool flag=true;
        for(int i=0;i<s.size();i++)
            record[s[i]-'a']++;//统计每个单词出现次数
        for(int i=0;i<s.size();i++){
            if(record[s[i]-'a']==1)//找到首个出现次数为1的,找到了就返回其位置
                return i;
        }        
        return -1;        
    }
};




注:本博文为EbowTang原创,后续可能继续更新本文。如果转载,请务必复制本条信息!

原文地址:http://blog.csdn.net/ebowtang/article/details/52187799

原作者博客:http://blog.csdn.net/ebowtang

本博客LeetCode题解索引:http://blog.csdn.net/ebowtang/article/details/50668895


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值