上岸算法 I LeetCode Weekly Contest 234解题报告

No.1 字符串中不同整数的数目

解题思路

使用正则表达式去掉非数字字符和每个字符串的前导 0 即可。

代码展示

class Solution {
    public int numDifferentIntegers(String word) {
        String[] nums = word.replaceAll("\\D", " ").split(" +");
        Set<String> set = new HashSet<>();
        for (String num : nums) {
            if (!num.equals("")) {
                String trimLeadingZero = num.replaceAll("^0*", "");
                trimLeadingZero = trimLeadingZero.equals("") ? "0" : trimLeadingZero;
                set.add(trimLeadingZero);
            }
        }
        return set.size();
    }
}

No.2 还原排列的最少操作步数

解题思路

模拟还原过程。

代码展示

class Solution {
    public int reinitializePermutation(int n) {
        int[] perm = new int[n];
        for (int i = 0; i < n; i++) {
            perm[i] = i;
        }
        int count = 1;
        perm = conv(perm);
        while (!check(perm)) {
            perm = conv(perm);
            count++;
        }
        return count;
    }

    boolean check(int[] perm) {
        for (int i = 0; i < perm.length; i++) {
            if (perm[i] != i) {
                return false;
            }
        }
        return true;
    }

    int[] conv(int[] perm) {
        int[] result = new int[perm.length];
        for (int i = 0; i < perm.length; i++) {
            if (i % 2 == 0) {
                result[i] = perm[i / 2];
            } else {
                result[i] = perm[perm.length / 2 + (i - 1) / 2];
            }
        }
        return result;
    }
}

No.3 替换字符串中的括号内容

解题思路

使用 Map 储存 knowledge,然后查找替换即可。

代码展示

class Solution {
    public String evaluate(String s, List<List<String>> knowledge) {
        Map<String, String> map = new HashMap<>();
        for (var kv : knowledge) {
            map.put(kv.get(0), kv.get(1));
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) != '(') {
                sb.append(s.charAt(i));
                continue;
            }
            int j = i + 1;
            while (s.charAt(j) != ')') {
                j++;
            }
            sb.append(map.getOrDefault(s.substring(i + 1, j), "?"));
            i = j;
        }
        return sb.toString();
    }
}

No.4 好因子的最大数目

解题思路

假如 n 有质因子 a, a, a, …, b, b, b, …, c, c, c…,那么它的一个好因子 x 必定至少由一个 a、一个 b、一个 c 组成。所以 n 的好因子数量就是 numsOfA * numsOfB * numsOfC

那么该题目就等价于:将 primeFactors 拆分成若干个数的和,使这些数的和乘积最大。

证明思路参考:https://blog.csdn.net/nameofcsdn/article/details/53333542

代码展示

class Solution {
    public int maxNiceDivisors(int primeFactors) {
        int k = ((primeFactors - 2) % 3) + 2;
        int num3 = (primeFactors - k) / 3;
        long mod = (long) (1e9 + 7);
        long res = k * pow(3, num3, mod) % mod;
        return (int) res;
    }

    long pow(long x, long y, long mod) {
        if (y == 0) {
            return 1;
        }
        long half = pow(x, y >> 1, mod);
        if ((y & 1) != 0) {
            return half * half * x % mod;
        }
        return half * half % mod;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值