zoukankan      html  css  js  c++  java
  • [LeetCode系列] 变序词查找问题(Anagrams)

    给定一系列词, 找出其中所有的变序词组合.

    Note: 变序词 - 组成字符完全相同但次序不同的单词. 如dog和god, ate和eat.

    算法描述: 使用map<string, vector<string> >存储所有的结果. 最后将map中size > 1的vector<string>插入到结果中.

    代码:

     1 class Solution {
     2 public:
     3     vector<string> anagrams(vector<string> &strs) {
     4         vector<string> res;
     5         map<string, vector<string> > rec;
     6         if (strs.size() == 0) return res;
     7         
     8         for (string s : strs) {
     9             string ts(s);
    10             sort(ts.begin(), ts.end());
    11             rec[ts].push_back(s);
    12         }
    13         
    14         for (auto map : rec) {
    15             if (map.second.size() > 1)
    16                 res.insert(res.end(), map.second.begin(), map.second.end());
    17         }
    18         
    19         return res;
    20     }
    21 };

    其中map.second代表的是map中的value, 即vector<string>.

  • 相关阅读:
    springmvc的注解式开发
    springmvc
    spring整合Mybatis
    spring的事务管理
    注解定义增强的两个方法
    动态代理
    错题解析
    SpringMVC的基本操作
    Spring整合MyBatis
    配置事务以及事务回滚
  • 原文地址:https://www.cnblogs.com/lancelod/p/3973371.html
Copyright © 2011-2022 走看看