• 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];
             }
         }
        }
    };

     

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

     

  • 相关阅读:
    从分布式系统的角度看REST
    修改python系统默认编码的一种方法
    Base PyQt4, Simple Web APP Framwork
    用python写的测试网页和文件编码的小程序
    Python学习笔记(二):标准流与重定向
    How to escape (percentencode) a URL with Python « SaltyCrane Blog
    python操作Excel读写使用xlrd
    Quickstart — Requests 0.10.7 documentation
    irb的子会话 相思雨 博客园
    基于python的汉字转GBK码
  • 原文地址:https://www.cnblogs.com/Tang-tangt/p/9189183.html
Copyright © 2020-2023  润新知