1002.查找常用字符

1648545-20190708125104540-808833002.png
思路导图 先用几个二维数组保存 abcd 用ASCII码保存在二维数组中
1648545-20190708125112907-732118749.png
1648545-20190708125117155-1673079699.png

class Solution {
     public List<String> commonChars(String[] A) 
    {
        List<String> list = new ArrayList<String>();
        int[][] arr = new int[A.length][26];
        boolean bol = true ;
        //将所有数据用二维数组保存
        for (int x = 0; x < A.length ; x++) 
        {
            for (int i = 0; i < A[x].length(); i++) 
            {
        //      System.out.println((A[x].charAt(i) - 'a')+" <-" + A[x].charAt(i));
                arr[x][A[x].charAt(i) - 'a']++;
            }
        }
//开始逐个元素进行查看
        for (int x = 0; x < 26; x++) 
        {   
            int count = arr[A.length - 1][x];
            if (arr[A.length - 1][x] != 0) 
            {
                for (int i = 0; i < arr.length - 1; i++) 
                {
                    if (arr[i][x] == 0)  //如果有一个字符串某元素个数为0 不满足不成立
                    {   
                        bol = false;
                        break;
                    }
                    count = Math.min(arr[i][x],count);//统计每个字符在每个字符串中出现次数
                }
            }
            if(bol ) 
            {
                for (int temp = 0; temp < count; temp++) 
                {   
                //  System.out.print((char)(x + 97));
                    list.add(""+(char)(x + 97));
                }   
                
            }
            count=0;
            bol = true;
        }
        System.out.println();
        for(int i = 0;i<arr.length;i++) {
            for(int x = 0;x<26;x++) {
                //System.out.print((char)(x+97)+"->"+arr[i][x]+" ");
            }
//System.out.println();
        }
        return list;
    }
}

HashMap 一开始写不出来 卡了很久

class Solution {
    public List<String> commonChars(String[] A) {
        List<String> result = new ArrayList();
        List<HashMap<Character, Integer>> list = new ArrayList();
        for (String word: A) {
            HashMap<Character, Integer> table = new HashMap();//以字符串的形式保存到HashMap中
            for (int i = 0; i < word.length(); i++) {
                char ch = word.charAt(i);
                table.put(ch, table.getOrDefault(ch, 0) + 1);//新的getOrDefault()方法提供一个快捷的方式获取Map中的值
            }
            list.add(table);
        }
        HashMap<Character, Integer> map = list.get(0);
        for (Character ch: map.keySet()) {
            boolean flag = true;
            int min = map.get(ch);
            for (int i = 1; i < list.size(); i++) {
                HashMap<Character, Integer> charCnt = list.get(i);
                if (!charCnt.containsKey(ch)) {
                    flag = false;
                    break;
                }
                if (charCnt.get(ch) < min) {
                    min = charCnt.get(ch);
                }
            }
            if (flag) {
                for (int j = 0; j < min; j++) {
                    result.add("" + ch);
                }
            }
        }
        return result;
    }
}

转载于:https://www.cnblogs.com/cznczai/p/11150510.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值