【红海游戏】第 281 场力扣周赛复盘

简介: 【红海游戏】第 281 场力扣周赛复盘

一、引言

10点起床,等到10:30,刚想打周赛,力扣弹出:502 Bad Gateway Error

这是不是说明,最近程序员都在学习算法,人员太多把力扣网关搞崩溃了

后来,力扣恢复了正常,本来想着放弃这场周赛

后来想了想,还是打了算了

二、6012. 统计各位数字之和为偶数的整数个数

1、题目简介

2、题目解析

  • 签到题目
  • 直接暴力枚举 1 ~ num 每个位置的数字是符合相加为偶数

3、题目代码

class Solution {
    public int countEven(int num) {
        int sum = 0;
        for(int i = 1; i <= num; i++){
            if(f(i)){
                sum++;
            }
        }
        return sum;
    }
    public boolean f(int n){
        int sum = 0;
        while(n != 0){
            sum = sum + n % 10;
            n = n / 10;
        }
        return sum % 2 == 0 ? true : false;
    }
}

三、6013. 合并零之间的节点

1、题目简介

2、题目解析

  • 链表模拟题目
  • 我们需要遍历整个链表,将两个 ListNode.val == 0 中间的数字进行相加
  • 形成一个新的链表

3、题目代码

class Solution {
    public ListNode mergeNodes(ListNode head) {
        ListNode p1 = new ListNode(0);
        ListNode p2 = p1;
        ListNode newHead = head.next;
        int sum = 0;
        while(newHead != null){
            if(newHead.val == 0){
                ListNode next = new ListNode(sum);
                p2.next = next;
                p2 = p2.next;
                sum = 0;
            }else{
                sum = sum + newHead.val;
            }
            newHead = newHead.next;
        }
        return p1.next;
    }
}

四、6014. 构造限制重复的字符串

1、题目简介

2、题目解析

  • 字符串贪心题目
  • 我们最终形成的字符串需要满足字典序最大(z > f > a),并且连续的字符不能超过 repeatLimit
  • 我们整理出每一个字符出现的次数,将其放入优先队列中,优先队列按照 字符的大小 排序
  • 我们每一次贪心都去拿字典序最大的
  • 如果字典序最大的字符的数量小于 repeatLimit ,直接拿走最大的字符,队列中 poll()
  • 如果字典序最大的字符的数量大于 repeatLimit,拿走 repeatLimit 个最大的字符,并且查看一下次大的字符,取 一个
  • 记得需要判断当前的字符是否连续出现过 repeatLimit 次:if (stringBuffer.length() != 0 && val[0] + 'a' == stringBuffer.charAt(stringBuffer.length() - 1))

3、题目代码

class Solution {
    public String repeatLimitedString(String s, int repeatLimit) {
        PriorityQueue<int[]> priorityQueue = new PriorityQueue<>(new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o2[0] - o1[0];
            }
        });
        int[] arr = new int[26];
        for (int i = 0; i < s.length(); i++) {
            arr[s.charAt(i) - 'a']++;
        }
        for (int i = 0; i < 26; i++) {
            if (arr[i] != 0) {
                priorityQueue.add(new int[]{i, arr[i]});
            }
        }
        StringBuffer stringBuffer = new StringBuffer();
        while (!priorityQueue.isEmpty()) {
            int[] val = priorityQueue.peek();
            if (stringBuffer.length() != 0 && val[0] + 'a' == stringBuffer.charAt(stringBuffer.length() - 1)) {
                return stringBuffer.toString();
            }
            if (val[1] <= repeatLimit) {
                stringBuffer.append(f(val[0], val[1]));
                priorityQueue.poll();
            } else {
                stringBuffer.append(f(val[0], repeatLimit));
                int[] first = priorityQueue.poll();
                if (priorityQueue.isEmpty()) {
                    return stringBuffer.toString();
                } else {
                    int[] sed = priorityQueue.peek();
                    if (sed[1] > 0) {
                        stringBuffer.append(f(sed[0], 1));
                        priorityQueue.poll();
                        first[1] = first[1] - repeatLimit;
                        sed[1] = sed[1] - 1;
                        if (first[1] >= 1) {
                            priorityQueue.add(first);
                        }
                        if (sed[1] >= 1) {
                            priorityQueue.add(sed);
                        }
                    }
                }
            }
        }
        return stringBuffer.toString();
    }
    public String f(int num, int value) {
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < value; i++) {
            stringBuffer.append((char) (num + 'a'));
        }
        return stringBuffer.toString();
    }
}

五、6015. 统计可以被 K 整除的下标对数目

1、题目简介

2、题目解析

  • 题面相对简介,题目十分困难本题考了数论如果 x * y % k == 0,那么 gcd(x, k) * gcd(y, k) % k == 0
  • 解析证明见:证明地址
  • 这样的话,我们可以把数组中每个值与 Kgcd 求出来,放入 Map
for(Integer key1 : map.keySet()){
    for(Integer key2 : map.keySet()){
      // 这里需要注意:key1 * key2 会超越int的范围,需要转换为long
      if((long)key1 * (long)key2 % k == 0 && key1 <= key2){
        // 逻辑处理
      }
    }
  }

为了避免重复,我们这里只考虑 key1 <= key2

  • 如果 key1 == key2,那么 count = count + value1 * (value2 - 1) / 2;
  • 如果 key1 < key2 ,那么 count = count + value1 * value2;
  • 最终返回 count 即可

3、题目代码

class Solution {
    public long coutPairs(int[] nums, int k) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for(int num : nums){
            int value = gcd(num, k);
            map.put(value, map.getOrDefault(value, 0) + 1);
        }
        long count = 0;
        for(Integer key1 : map.keySet()){
            for(Integer key2 : map.keySet()){
              // 这里需要注意:key1 * key2 会超越int的范围,需要转换为long
                if(key1 <= key2 && (long)key1 * (long)key2 % k == 0){
                  // 同样,value1 * value2 也会越int的界,需要转换为long
                    long value1 = (long)map.get(key1);
                    long value2 = (long)map.get(key2);
                    if(key1 < key2){
                        count = count + value1 * value2;
                    }else{
                        count = count + value1 * (value2 - 1) / 2;
                    }
                }
            }
        }
        return count;
    }
    public int gcd(int a, int b) {
        return b == 0 ? a : gcd(b, a % b);
    }
}









相关文章
|
21天前
LeetCode题:174. 地下城游戏
LeetCode题:174. 地下城游戏
41 0
LeetCode题:174. 地下城游戏
|
21天前
|
Go
golang力扣leetcode第 294 场周赛
golang力扣leetcode第 294 场周赛
35 0
|
21天前
|
算法 Java Go
golang力扣leetcode 第 293 场周赛
golang力扣leetcode 第 293 场周赛
61 0
|
21天前
|
Go
golang力扣leetcode 第 292 场周赛
golang力扣leetcode 第 292 场周赛
42 0
|
21天前
|
存储
Leetcode第382场周赛
```markdown 给定字符串`s`,计算按键变更次数,即使用不同键的次数,不考虑大小写差异。例如,`&quot;aAbBcC&quot;`变更了2次。函数`countKeyChanges`实现此功能。另外,求满足特定模式子集最大元素数,`maximumLength`函数使用`TreeMap`统计次数,枚举并构建子集,返回最大长度。最后,Alice和Bob玩鲜花游戏,Alice要赢需满足鲜花总数奇数、顺时针在[1,n]、逆时针在[1,m],返回满足条件的(x, y)对数,可通过奇偶性分类讨论求解。 ```
15 1
|
21天前
|
存储
Leetcode第383场周赛
在LeetCode第383场周赛中,选手完成了3道题目。第一题是关于边界上的蚂蚁,蚂蚁根据非零整数数组nums的值移动,返回蚂蚁返回边界上的次数。解题方法是计算数组累加和为0的次数。第二题涉及计算网格的区域平均强度,给定一个灰度图像和阈值,返回每个像素所属区域的平均强度。解题关键在于理解相邻像素和区域定义,并计算平均强度。第三题是恢复单词初始状态的最短时间问题,通过移除前k个字符并添加k个字符,求恢复原词所需的最短时间。解题策略是检查去除前k个字符后的子串是否能作为原词的前缀。
13 1
Leetcode第383场周赛
|
21天前
|
算法
【力扣】55.跳跃游戏
【力扣】55.跳跃游戏
|
21天前
leetcode:292. Nim 游戏(数学推理)
leetcode:292. Nim 游戏(数学推理)
21 0
|
21天前
|
算法 Java 测试技术
[Java·算法·中等] LeetCode 45. 跳跃游戏 II 详细解读
[Java·算法·中等] LeetCode 45. 跳跃游戏 II 详细解读
39 0
|
21天前
leetcode-529:扫雷游戏
leetcode-529:扫雷游戏
23 0