首页 > 其他 > 详细

LeetCode 1002. 查找常用字符

时间:2020-10-14 12:08:28      阅读:31      评论:0      收藏:0      [点我收藏+]

题目链接

1002. 查找常用字符

题目思路

这个题好像也没啥特别的思路,我个人就是使用一个二维数组去存放每个字符串中字符的出现次数(因为题目说只含有小写字母,所以可以直接使用数组),然后再使用一个二重循环,外层遍历小写字母,内层遍历字符串数组长度,去寻找每个字符出现的最少次数,然后再把它加入到结果集中即可。

代码实现

class Solution {
    public List<String> commonChars(String[] A) {
        int[][] count = new int[A.length][26];
        int idx = 0;
        for(String str: A){
            for(char x: str.toCharArray()){
                count[idx][x - ‘a‘]++;
            }
            idx++;
        }
        List<String> res = new ArrayList<>();
        for(int i = 0; i < 26; i++){
            int times = Integer.MAX_VALUE;
            for(int j = 0; j < A.length; j++){
                times = Math.min(times, count[j][i]);
                if(times == 0){
                    break;
                }
            }
            for(int k = 0; k < times; k++){
                res.add(String.valueOf((char)(‘a‘ + i)));
            }
        }
        return res;
    }
}

总结

最近LC的每日一题都是打卡题 还是很舒服的~
技术分享图片

LeetCode 1002. 查找常用字符

原文:https://www.cnblogs.com/ZJPaang/p/13812949.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!