sweet1cris

Untitled

Feb 10th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.62 KB | None | 0 0
  1.  
  2. //高频班version
  3. public class Solution {
  4.     /**
  5.      * @param strs: the given array of strings
  6.      * @return: The anagrams which have been divided into groups
  7.      */
  8.     public List<List<String>> groupAnagrams(String[] strs) {
  9.         // write your code here
  10.         Map<String, List<String>> map = new HashMap<>();
  11.         for (String s : strs) {
  12.             char[] sc = s.toCharArray();
  13.             Arrays.sort(sc);
  14.             String key = String.valueOf(sc);
  15.             map.putIfAbsent(key, new ArrayList<>());
  16.             map.get(key).add(s);
  17.         }
  18.         return new ArrayList<>(map.values());
  19.     }
  20. }
Advertisement
Add Comment
Please, Sign In to add comment