zoukankan      html  css  js  c++  java
  • leetcode Group Anagrams

    Group Anagrams

    My Submissions
    Total Accepted: 54426 Total Submissions: 217518 Difficulty: Medium

    Given an array of strings, group anagrams together.

    For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"]
    Return:

    [
      ["ate", "eat","tea"],
      ["nat","tan"],
      ["bat"]
    ]

    Note:

    1. For the return value, each inner list's elements must follow the lexicographic order.
    2. All inputs will be in lower-case.

    Subscribe to see which companies asked this question

    觉得还是应该锻炼一下代码能力,这几天一直看课件,找知识,动手能力确实下降。

     1 class Solution {
     2 public:
     3     vector<vector<string>> groupAnagrams(vector<string>& strs) {
     4         vector<vector<string>> result;
     5         std:sort(strs.begin(),strs.end());
     6         map<string,vector<string>> strtoarray;
     7         int i;
     8         for(i=0;i<strs.size();i++){
     9             strtoarray[tostr(strs[i])].push_back(strs[i]);
    10         }
    11         map<string,vector<string>>::iterator it;
    12         for(it=strtoarray.begin();it!=strtoarray.end();it++){
    13             result.push_back(it->second);
    14         }
    15         return result;
    16     }
    17     
    18     
    19     string tostr(string str){
    20         std:sort(str.begin(),str.end());
    21         return str;
    22     }
    23 };
  • 相关阅读:
    第二章Maven安装与配置
    第一章 Maven简介
    什么是Maven?
    jbpm与OA项目-oa概念
    Hadoop学习常用的Linux命令
    包名命名规则
    判断网络类型
    webview的设置
    AlertDialog的实现
    SharedPreferences保存用户偏好参数
  • 原文地址:https://www.cnblogs.com/Deribs4/p/4948005.html
Copyright © 2011-2022 走看看