【一次过】Lintcode 487 .姓名去重

给一串名字,将他们去重之后返回。两个名字重复是说在忽略大小写的情况下是一样的。

样例

给出:

[
  "James",
  "james",
  "Bill Gates",
  "bill Gates",
  "Hello World",
  "HELLO WORLD",
  "Helloworld"
]

返回:

[
  "james",
  "bill gates",
  "hello world",
  "helloworld"
]

返回名字必须都是小写字母。

解题思路:

    对于这种去重问题,使用哈希表即可。先将大写转换为小写,然后插入哈希表,最后将哈希表中的内容取出来放进结果中。

class Solution {
public:
    /**
     * @param names: a string array
     * @return: a string array
     */
    vector<string> nameDeduplication(vector<string> &names) 
    {
        // write your code here
        unordered_set<string> sstr;
        
        for(int i=0;i<names.size();i++)
        {
            for(int j=0;j<names[i].size();j++)
            {
                names[i][j] = tolower(names[i][j]);
            }
            sstr.insert(names[i]);
        }
        
        vector<string> res;
        for(auto it=sstr.begin();it != sstr.end();it++)
            res.push_back(*it);
        
        return res;
    }
};


猜你喜欢

转载自blog.csdn.net/majichen95/article/details/80742653