zoukankan      html  css  js  c++  java
  • LeetCode:Anagrams(字母颠倒)

    problem:

    Given an array of strings, return all groups of strings that are anagrams.

    Note: All inputs will be in lower-case.

    思路解析:使用hashmap来存储已排好序的字符串。注意if else的结构。因为第一次出现的字符串是否是anagram是要靠第二次出现的字符串判断的

    所以在第一次入Hashmap时存储的为vector中的索引下标。之后把他置为-1是为了下一次出现的anagram做准备。

     1 class Solution {
     2 public:
     3     vector<string> anagrams(vector<string>& strs) {
     4         
     5         unordered_map<string,int> map;
     6         vector<string> result;
     7         
     8         for(int i=0;i<strs.size();i++)
     9         {
    10             string s=strs[i];
    11             sort(s.begin(),s.end());
    12             
    13             if(map.find(s)==map.end())
    14                  map[s]=i;
    15             else 
    16             {
    17                if(map[s]>=0)
    18             {
    19                 result.push_back(strs[map[s]]);
    20                 map[s]=-1;
    21             }
    22                 result.push_back(strs[i]);
    23             }
    24             
    25         }
    26         
    27         return result;
    28     }
    29         
    30 };
  • 相关阅读:
    记录我发现的第一个关于 Google 的 Bug
    iOS 中的 Delayed Transition
    Appstore|IPA
    地图|定位
    开发者账号
    App跳转
    国际化
    短信|彩信
    闪光灯
    Cornerstone|SVN
  • 原文地址:https://www.cnblogs.com/xiaoying1245970347/p/4645445.html
Copyright © 2011-2022 走看看