上岸算法 发表于 2021-3-28 23:34:49

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

本帖最后由 上岸算法 于 2021-3-28 23:40 编辑

上岸算法任何只教知识的课程都是耍流氓我们直击上岸关注我们,第一时间获得大厂面试真题讲解

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;
      for (int i = 0; i < n; i++) {
            perm = 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) {
                return false;
            }
      }
      return true;
    }

    int[] conv(int[] perm) {
      int[] result = new int;
      for (int i = 0; i < perm.length; i++) {
            if (i % 2 == 0) {
                result = perm;
            } else {
                result = perm;
            }
      }
      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;
    }
}

上岸DS秋招最新大厂面经公开课

活动介绍:为北美同学免费提供大厂面试官面经重点,为你在线提分
活动时间:2021/4/5-2021/4/26活动全程免费 免费 免费!!!活动安排: 4/05小k 老师(资深FLAG 面试官):简历项目深挖4/17莎莎老师(资深数据科学家):上岸大厂有哪些技巧4/19小春老师(FLAG 现职工程师):推荐系统公开课4/26小雨老师(资深数据科学家):SQL 带刷专场另有两场DS 求职技术讲座参与方式:联系小年糕,邀请进群
页: [1]
查看完整版本: 上岸算法 I LeetCode Weekly Contest 234解题报告