209 First Unique Character in a String

原题网址:https://www.lintcode.com/problem/first-unique-character-in-a-string/description

描述

给出一个字符串,找出第一个只出现一次的字符。

您在真实的面试中是否遇到过这个题?  是

样例

对于 "abaccdeff"‘b‘为第一个只出现一次的字符.

标签

字符串处理

思路:用两个哈希表,一个统计每个字符出现次数,另一个统计字符的下标。遍历第一个哈希表,找到字符出现次数为1并且下标最小的return出去。

AC代码:

class Solution {
public:
    /**
     * @param str: str: the given string
     * @return: char: the first unique character in a given string
     */
    char firstUniqChar(string &str) {
        // Write your code here
     char result;
     int index=-1;
     int n=str.size();
     map<char,int> chCount;
     map<char,int> chIndex;

     for (int i=0;i<n;i++)
     {
         if (chCount.find(str[i])!=chCount.end())//统计字符数量;
         {
             chCount[str[i]]++;
         }
         else
         {
             chCount[str[i]]=1;
         }
         if (chIndex.find(str[i])==chIndex.end())//找到字符的第一个索引;
         {
             chIndex[str[i]]=i;
         }
     }

     map<char,int>::iterator it;
     for (it=chCount.begin();it!=chCount.end();it++)
     {
         if (it->second==1&&index==-1)
         {
             result=it->first;
             index=chIndex[it->first];
         }
         else if (it->second==1&&chIndex[it->first]<index)
         {
             result=it->first;
             index=chIndex[it->first];
         }
     }

     return result;
    }
};

总觉得这样有点麻烦,想了想把第二个哈希表去掉,统计字符次数时如果字符存在于哈希表中,数量- -,否则数量为1。最后返回哈希表第一个元素的first。想法很美好,然而思路有问题。代码提交后只通过部分数据,原因是这样只能消除数量为偶数个的字符,若字符数量为奇数且不为1,结果错误。

在网上搜了搜果然看到了更简洁的代码,真想拍自己一脑门……   参考:https://www.cnblogs.com/grandyang/p/5802109.html

只用一个哈希表统计字符数量,然后遍历字符串,若当前字符数量为1则返回该字符。

AC代码:

class Solution {
public:
    /**
     * @param str: str: the given string
     * @return: char: the first unique character in a given string
     */
    char firstUniqChar(string &str) {
        // Write your code here
    int n=str.size();
     map<char,int> chCount;
     for (int i=0;i<n;i++)
     {
         if (chCount.find(str[i])!=chCount.end())//统计字符数量;
         {
             chCount[str[i]]++;
         }
         else
         {
             chCount[str[i]]=1;
         }
     }
     for (int i=0;i<n;i++)
     {
         if (chCount[str[i]]==1)
         {
             return str[i];
         }
     }
    }
};

题目并不难,只是脑子经常驴了……

原文地址:https://www.cnblogs.com/Tang-tangt/p/9189183.html

时间: 2024-11-03 22:31:53

209 First Unique Character in a String的相关文章

Leetcode-387 First Unique Character in a String

#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 exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. Note: You m

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 exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. Note: You may assume the string contain only lowerc

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

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

最近公司搬家了,有两天没写了,今天闲下来了,继续开始算法之路. leetcode的题目如下: 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: Y

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 exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. Note: You may assume the string contain only lowercase

[LeetCode] NO. 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 exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. Note: You may assume the string contain only lowe

387. First Unique Character in a String

https://leetcode.com/problems/first-unique-character-in-a-string/#/description 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 = "lovel

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 doesn't exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. 解题思路: 开个26个数的数组,然后先对字符串过一遍,统计每个字母出现的次数,然后从头再国一遍,

[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 exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. Note: You may assume the string contain only lowercase