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

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

    Note: All inputs will be in lower-case.

    这道题的意思是返回含有相同字母的字符串

    First, I don't know the real meaning of this problem. Next ,After searching the answer through Internet,I also feel puzzled.

    Last, I have no experience about the function of HashMap or Hashtable.

    I feel exhausted about the current of my attitude. Anyway What I should do is try my best.

    Fighting!

    慢慢积累

    char[] aChar = a.toCharArray();

    add是将传入的参数作为当前List中的一个Item存储,即使你传入一个List也只会另当前的List增加1个元素,而addAll是传入一个List,将此List中的所有元素加入到当前List中,也就是当前List会增加的元素个数为传入的List的大小

    不多说了,今天要再刷3道题,然后写论文!

    下面附上代码:

    public List<String> anagrams(String[] strs) {
           
            ArrayList<String> result = new ArrayList<String>();
            HashMap<String, ArrayList<String>> ht = new HashMap<String, ArrayList<String>>();
            for(int i =0;i<strs.length;i++){
                String sor = sorted(strs[i]);
                ArrayList<String> val = ht.get(sor);
                if(val!=null){
                   val.add(strs[i]);
                }
                else{
                    val = new ArrayList<String>();
                    val.add(strs[i]);
                    ht.put(sor,val);
                }
                
            }
            Set<String> set = ht.keySet();
            for(String s : set){
                ArrayList<String> val = ht.get(s);
                if(val.size()>1){
                    result.addAll(val);
                }
                
            }
            return result;
        }
         public String sorted(String a){
                char[] aChar = a.toCharArray();
                Arrays.sort(aChar);
                return new String(aChar);
            }
    

      

  • 相关阅读:
    [arm]虚拟机,2440开发板,主机三者互通
    Linux下的lds链接脚本简介(四)
    Linux下的lds链接脚本简介(三)
    Linux下的lds链接脚本简介(二)
    Linux下的lds链接脚本简介(一)
    程序员面试资源大收集
    Source Insight 3.50.0065使用详解
    DNW烧写FL2440 NAND Flash分区
    php isset()与empty()的使用
    JSON.parse()和JSON.stringify()的区别
  • 原文地址:https://www.cnblogs.com/gracyandjohn/p/4503447.html
Copyright © 2011-2022 走看看