• 算法提升——LeetCode第385场周赛总结


    题目

    统计前后缀下标对 I

    给你一个下标从0开始的字符串数组words。

    定义一个布尔函数isPrefixAndSuffix,它接受两个字符串参数str1和str2:

    当str1同时是str2的前缀(prefix)和后缀(suffix)时,isPrefixAndSuffix(str1,str2)返回true,否则返回false。
    例如,isPrefixAndSuffix(“aba”,“ababa”)返回true,因为"aba"既是"ababa"的前缀,也是"ababa"的后缀,但是isPrefixAndSuffix(“abc”,“abcd”)返回false。

    以整数形式,返回满足i

    解题思路

    暴力判断每一个字符串是否是开头或结尾,代码如下:

    class Solution {
        public int countPrefixSuffixPairs(String[] words) {
            int res=0;
            int len=words.length;
            for(int i=0;i<len;i++){
                for(int j=i+1;j<len;j++){
                    if (isPrefixAndSuffix(words[i],words[j])){
                        res++;
                    }
                }
            }
            return res;
        }
        
        public boolean isPrefixAndSuffix(String a,String b){
            return  b.startsWith(a)&&b.endsWith(a);
        }
        
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    最长公共前缀长度

    给你两个正整数数组arr1和arr2。

    正整数的前缀是其最左边的一位或多位数字组成的整数。例如,123是整数12345的前缀,而234不是。

    设若整数c是整数a和b的公共前缀,那么c需要同时是a和b的前缀。例如,5655359和56554有公共前缀565,而1223和43456没有公共前缀。

    你需要找出属于arr1的整数x和属于arr2的整数y组成的所有数对(x,y)之中最长的公共前缀的长度。

    返回所有数对之中最长公共前缀的长度。如果它们之间不存在公共前缀,则返回0。

    解题思路

    预先处理arr1所有前缀到set中,然后arr2依次判断即可,代码如下:

    class Solution {
        public int longestCommonPrefix(int[] arr1, int[] arr2) {
            Set<Integer> st = new HashSet<>();
            for (int x : arr1) {
                for (; x > 0; x /= 10) {
                    st.add(x);
                }
            }
    
            int mx = 0;
            for (int x : arr2) {
                for (; x > 0 && !st.contains(x); x /= 10) ;
                mx = Math.max(mx, x);
            }
            return mx > 0 ? Integer.toString(mx).length() : 0;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    出现频率最高的质数

    给你一个大小为mxn、下标从0开始的二维矩阵mat。在每个单元格,你可以按以下方式生成数字:

    最多有8条路径可以选择:东,东南,南,西南,西,西北,北,东北。

    选择其中一条路径,沿着这个方向移动,并且将路径上的数字添加到正在形成的数字后面。

    注意,每一步都会生成数字,例如,如果路径上的数字是1,9,1,那么在这个方向上会生成三个数字:1,19,191。

    返回在遍历矩阵所创建的所有数字中,出现频率最高的、大于10的质数;如果不存在这样的质数,则返回-1。如果存在多个出现频率最高的质数,那么返回其中最大的那个。

    注意:移动过程中不允许改变方向。

    解题思路

    对于每个单元格,枚举八个方向,生成数字,统计其中质数个数。代码如下:

    class Solution {
        private static final int[][] DIRS = {{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
    
        public int mostFrequentPrime(int[][] mat) {
            int m = mat.length;
            int n = mat[0].length;
            Map<Integer, Integer> cnt = new HashMap<>();
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    for (int[] d : DIRS) {
                        int x = i + d[0];
                        int y = j + d[1];
                        int v = mat[i][j];
                        while (x >= 0 && x < m && y >= 0 && y < n) {
                            v = v * 10 + mat[x][y];
                            if (isPrime(v)) {
                                cnt.merge(v, 1, Integer::sum);
                            }
                            x += d[0];
                            y += d[1];
                        }
                    }
                }
            }
    
            int ans = -1;
            int maxCnt = 0;
            for (Map.Entry<Integer, Integer> e : cnt.entrySet()) {
                int v = e.getKey();
                int c = e.getValue();
                if (c > maxCnt) {
                    ans = v;
                    maxCnt = c;
                } else if (c == maxCnt) {
                    ans = Math.max(ans, v);
                }
            }
            return ans;
        }
    
        private boolean isPrime(int n) {
            for (int i = 2; i * i <= n; i++) {
                if (n % i == 0) {
                    return false;
                }
            }
            return true;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    统计前后缀下标对 II

    给你一个下标从0开始的字符串数组words。

    定义一个布尔函数isPrefixAndSuffix,它接受两个字符串参数str1和str2:

    当str1同时是str2的前缀(prefix)和后缀(suffix)时,isPrefixAndSuffix(str1,str2)返回true,否则返回false。
    例如,isPrefixAndSuffix(“aba”,“ababa”)返回true,因为"aba"既是"ababa"的前缀,也是"ababa"的后缀,但是isPrefixAndSuffix(“abc”,“abcd”)返回false。

    以整数形式,返回满足i

    解题思路

    本题跟第一题一致,不过在用暴力法就没办法解决了。可以用字典树解决本题。

    class Node {
        Map<Integer, Node> son = new HashMap<>();
        int cnt;
    }
    
    class Solution {
        public long countPrefixSuffixPairs(String[] words) {
            long ans = 0;
            Node root = new Node();
            for (String S : words) {
                char[] s = S.toCharArray();
                int n = s.length;
                Node cur = root;
                for (int i = 0; i < n; i++) {
                    int p = (s[i] - 'a') << 5 | (s[n - 1 - i] - 'a');
                    cur = cur.son.computeIfAbsent(p, k -> new Node());
                    ans += cur.cnt;
                }
                cur.cnt++;
            }
            return ans;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    总结

    参与了许多周赛,却始终在第三题上遇到瓶颈,难以突破。反复总结经验后发现,虽然题解看起来简单,但亲自动手解决时总是遇到难题,无法顺利通过。为了改进这一状况,在接下来的练习中,我打算从第三题开始着手,以此作为突破口,提升我的解题能力。

  • 相关阅读:
    对Cookie运用的实战与原理剖析详解
    API高频量化交易平台:数字货币市场的革新与挑战
    ATT&CK红队评估实战靶场二
    精准防疫有“利器”| 芯讯通助力数字哨兵护航复市
    速盾网络:cdn加速技术和云计算的区别
    Windows环境下Hadoop/Hbase环境的配置
    leetcode:415. 字符串相加(模拟竖式计算)
    Eclipse 教程Ⅳ
    MTK Camera 冷启动、前后摄切换性能优化分析
    书剑宠物疫苗接种管理软件操作教程
  • 原文地址:https://blog.csdn.net/Luck_gun/article/details/136220573