hashmap in java


  • 0
    Z
    public List<List<String>> groupAnagrams(String[] strs) {
            List<List<String>> res = new ArrayList<List<String>>();
            if(strs.length == 0)return res;
            HashMap<String,List<String>> map = new HashMap<>();
            for(String str : strs){
                char[] tmp = str.toCharArray();
                Arrays.sort(tmp);
                if(!map.containsKey(new String(tmp))){
                    List<String> ls = new ArrayList<>();
                    ls.add(str);
                    map.put(new String(tmp),ls);
                }else{
                    List<String> ls = map.get(new String(tmp));
                    ls.add(str);
                    map.put(new String(tmp),ls);
                }
            }
            for(String str : map.keySet()){
                res.add(map.get(str));
            }
            return res;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.