zoukankan      html  css  js  c++  java
  • LeetCode Anagrams

    原题链接在这里:https://leetcode.com/problems/anagrams/

    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.

    新的做法如下:与之前的字母排序不同,用getHash算出一个string 对应的hashcode. 用这个hash code当HashMap的key, 可以节省一部分时间。

     AC Java:

     1 public class Solution {
     2     public List<List<String>> groupAnagrams(String[] strs) {
     3         List<List<String>> res = new ArrayList<List<String>>();
     4         if(strs == null || strs.length == 0){
     5             return res;
     6         }
     7         Arrays.sort(strs); //error
     8         HashMap<Integer, List<String>> hm = new HashMap<Integer, List<String>>();
     9         for(int i = 0; i<strs.length; i++){
    10             int [] count = getCount(strs[i]);
    11             int hash = getHash(count);
    12             if(!hm.containsKey(hash)){
    13                 List<String> item = new ArrayList<String>(); //error
    14                 item.add(strs[i]);
    15                 hm.put(hash, item);    
    16             }else{
    17                 hm.get(hash).add(strs[i]);
    18             }
    19         }
    20         
    21         for(List<String> ls : hm.values()){
    22             res.add(ls);
    23         }
    24         return res;
    25     }
    26     
    27     private int[] getCount(String s){
    28         int [] count = new int[26];
    29         if(s == null || s.length() == 0){
    30             return count;
    31         }
    32         for(int i = 0; i<s.length(); i++){
    33             count[s.charAt(i) - 'a']++;
    34         }
    35         return count;
    36     }
    37     
    38     private int getHash(int [] count){
    39         int a = 378551;
    40         int b = 63689;
    41         int hash = 0;
    42         for(int i = 0; i<count.length; i++){
    43             hash = hash*a + count[i];
    44             a*=b;
    45         }
    46         return hash;
    47     }
    48 }

      

    做这道题让我想起刚才的 Valid Anagram 简直想的太麻烦了。

    这道题的思路就是sort strs 数组中的每一个string,建立HashMap,key 是 sorted string, value 是 list of all original strings.

    最后iterate hm,所有value 长度大于1的都是anagram。

    Note:1. sort string 就用 string.toCharArray(), 然后用Arrays.sort(), 最后建立一个new String。这里注意是Arrays 而不是 Array。

        2. list 的长度用 list.size().

        3. 链接现有list 到一个新的list上可以直接用 mainList.addAll(subList) 来完成。

    AC Java:

     1 public class Solution {
     2     public List<String> anagrams(String[] strs) {
     3         if(strs == null || strs.length == 0)
     4             return null;
     5             
     6         List<String> res = new ArrayList<String>();
     7         
     8         HashMap<String,ArrayList<String>> hm = new HashMap<String,ArrayList<String>>();
     9         
    10         for(int i = 0; i < strs.length; i++){
    11             String key = sortStr(strs[i]);
    12             if(hm.containsKey(key)){
    13                 hm.get(key).add(strs[i]);
    14             }else{
    15                 ArrayList<String> valList = new ArrayList<String>();
    16                 valList.add(strs[i]);
    17                 hm.put(key, valList);
    18             }
    19         }
    20         
    21         for(String s : hm.keySet()){
    22             if(hm.get(s).size() > 1){
    23                 res.addAll(hm.get(s));
    24             }
    25         }
    26         return res;        
    27         
    28     }
    29     
    30     private String sortStr(String s){
    31         char[] temp = s.toCharArray();
    32         Arrays.sort(temp); //error
    33         String str = new String(temp);
    34         return str;
    35     }
    36
  • 相关阅读:
    python全栈学习--day39(multiprocess模块)
    python全栈学习--day38(多进程在python中的操作)
    python全栈学习--day36(socket验证客户端的合法性、 并发编程)
    python全栈学习--day35(黏包机制)
    python全栈学习--day33(网络编程-socket)
    python-sys模块
    python-os模块
    python-生成随机数random
    python-常用模块-time、datetime模块
    python-模块、包
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/4825054.html
Copyright © 2011-2022 走看看