zoukankan      html  css  js  c++  java
  • 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];
             }
         }
        }
    };

     

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

     

  • 相关阅读:
    Java 注解指导手册(下)
    CentOS安装Redis Sentinel HA集群
    EasyBCD安装CentOS双系统
    读《大型网站技术架构核心原理与案例分析》
    CentOS的Redis内存分配策略配置
    CentOS搭建VSFTP
    freemaker分页备忘
    jenkins持续集成配置备忘
    Redis常用命令
    stream转byte数组几种方式
  • 原文地址:https://www.cnblogs.com/Tang-tangt/p/9189183.html
Copyright © 2011-2022 走看看