第 167 场周赛

第 167 场周赛

在B站关注了一个up主,每周会更新Leetcode的周赛视频,所以也打算学习一下,起码每周都有算法练习,希望能解锁难度为困难的算法题目。还有两题还没写,下周继续。

5283. 二进制链表转整数

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public int getDecimalValue(ListNode head) {
        int res =0;
        ListNode curr = head;
        while(curr!=null){
            int temp = curr.val;
            res = res*2+temp;
            curr = curr.next;
        }
        return res;
    }
}

5124. 顺次数

class Solution {
    public List<Integer> sequentialDigits(int low, int high) {
        List<Integer> lists = new ArrayList<Integer>();
        for(int i=2;i<=10;i++){
            for(int j=1;j<=10-i;j++){
                int temp=0;
                for(int k=0;k<i;k++){
                    int currValue = j+k;
                    if(currValue>9){
                        break;
                    }
                    temp = temp*10 +currValue; 
                }
                if(temp>=low && temp<=high){
                    lists.add(temp);
                }
            }
        }
        Collections.sort(lists);
        return lists;
    }
}

1292. 元素和小于等于阈值的正方形的最大边长

class Solution {
    int m ;
    int n ;
    int [][] dp;
    public int maxSideLength(int[][] mat, int threshold) {
        m = mat.length;
        n = mat[0].length;
        dp = new int[m+1][n+1];
        for(int i = 1;i<=m;i++){
            for(int j=1;j<=n;j++){
                dp[i][j] = mat[i-1][j-1]+dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1];
            }
        }
        int res = 0;
        int l=0,h = Math.min(m,n);
        while(l<=h){
            if(l==h || h-l==1){
                break;
            }
            int mid = l+(h-l)/2;
            if(compare(mid,threshold)){
                l=mid;
            }else{
                h=mid-1;
            }  
        }
        if(compare(h,threshold)){
            return h;
        }else{
            return l;
        }
    }
    public boolean compare(int k, int threshold) {
        for(int i=1;i<=m;i++){
            for(int j=1;j<=n;j++){
                if(i-k<0 || j-k<0){
                    continue;
                }
                if(dp[i][j]-dp[i-k][j]-dp[i][j-k]+dp[i-k][j-k]<=threshold){
                    return true;
                }
            }
        }
        return false;
    }

    
}
发布了21 篇原创文章 · 获赞 10 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/Tracy_Yuan2014/article/details/103554834