• LeetCode 1002. 查找常用字符


    题目链接

    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的每日一题都是打卡题 还是很舒服的~

  • 相关阅读:
    补充 函数详解
    Python web前端 11 form 和 ajax
    进程线程之间的通信
    面向对象epoll并发
    socket发送静态页面
    进程与线程的表示,属性,守护模式
    并发
    django, tornado
    并行
    非阻塞套接字编程, IO多路复用(epoll)
  • 原文地址:https://www.cnblogs.com/ZJPaang/p/13812949.html
Copyright © 2020-2023  润新知