代码训练第二天|数组part2

文章讲述了在解决LeetCode编程问题时,如何运用双指针技巧(如有序数组平方问题),以及逆向思维处理数组操作,同时介绍了Java中`&&`和`&`运算符的区别,以及如何使用滑动窗口(如最小子数组和问题)和动态规划(如螺旋矩阵II)。作者强调了循环不变量和边界条件的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

977. 有序数组的平方

题目链接:LeetCode - The World's Leading Online Programming Learning Platform

思路:知道用双指针做题,也做出来了。但是先找到的中间值再去从中间从小到大找到了两边。多了很多判断条件并且跑了三次才跑通。

总结

要学会逆向思维,从两边往中间,从大到小来得到数组

Java中&& 和& 的区别:

&& 得到了结果就会停止执行operands

&执行完全部operands

执行方向:从左向右

边界条件应该写在&&的第一个避免之后的代码执行时out of index

class Solution {
    public int[] sortedSquares(int[] nums) {
        int[] res = new int[nums.length];
    //     int start = 0;
    //     while(start<nums.length&&nums[start]<0){ // || stop excute if get the result | will excute all the operands
    //         start++;
    //     }
    //     int left = start-1, right = start;
    //     int index = 0;
    //     while(index<nums.length){
    //         if(right==nums.length){
    //             res[index++]=(int)Math.pow(nums[left--],2);
    //             continue;
    //         }
    //         if(left==-1){
    //             res[index++]=(int)Math.pow(nums[right++],2);
    //             continue;
    //         }
    //         if(Math.abs(nums[left])<nums[right])res[index++]=(int)Math.pow(nums[left--],2);
    //         else res[index++]=(int)Math.pow(nums[right++],2);
    //     }
    //     return res;

        int left = 0, right = nums.length-1;
        int index = nums.length-1;
        while(left<=right){
            if(Math.abs(nums[left])>Math.abs(nums[right])){
                res[index]=(int)Math.pow(nums[left++],2);
            }else{
                res[index]=(int)Math.pow(nums[right--],2);
            }

            index--;
        }
        return res;
    }
}

 209.长度最小的子数组

题目链接:https://leetcode.com/problems/minimum-size-subarray-sum/

文章讲解:https://programmercarl.com/0209.%E9%95%BF%E5%BA%A6%E6%9C%80%E5%B0%8F%E7%9A%84%E5%AD%90%E6%95%B0%E7%BB%84.html

第一想法是动态规划,有个类似的子数组的题目,但是后来看了下全部是正数,好像也没那么麻烦。用滑动窗口解决就好。滑动窗口的框架不是很熟悉,这个题目相对简单,还是能写,需要复习。

暴力算法外层循环是循环的起始位置

滑动窗口其实是通过在外层循环来遍历终止位置,再根据窗口条件来移动起始位置

class Solution {
    public int minSubArrayLen(int target, int[] nums) {
        int left = 0, right = 0;
        int sum = 0;
        int res = Integer.MAX_VALUE;
        while(right<nums.length){ //[left,right)
            sum+=nums[right];//add right num into window first
            right++;//shift the right index;
            while(left<=right&&sum>=target){
                res = Math.min(res,right-left);//right 先动在算,left先算再动;所以[left,right)
                sum-=nums[left];
                left++;
            }

        }
        return res==Integer.MAX_VALUE?0:res;//read the question
    }
}

 59.螺旋矩阵II

循环不变量原则,避免发生错误

坚持了每条边左闭右开的原则

class Solution {
    public int[][] generateMatrix(int n) {
        int loop = 0;  // 控制循环次数
        int[][] res = new int[n][n];
        int start = 0;  // 每次循环的开始点(start, start)
        int count = 1;  // 定义填充数字
        int i, j;

        while (loop++ < n / 2) { // 判断边界后,loop从1开始
            // 模拟上侧从左到右
            for (j = start; j < n - loop; j++) {
                res[start][j] = count++;
            }

            // 模拟右侧从上到下
            for (i = start; i < n - loop; i++) {
                res[i][j] = count++;
            }

            // 模拟下侧从右到左
            for (; j >= loop; j--) {
                res[i][j] = count++;
            }

            // 模拟左侧从下到上
            for (; i >= loop; i--) {
                res[i][j] = count++;
            }
            start++;
        }

        if (n % 2 == 1) {
            res[start][start] = count;
        }

        return res;
    }
}

//取巧办法,用四个bond
class Solution {
    public int[][] generateMatrix(int n) {
        int upperbound = 0, leftbound = 0, lowerbound = n-1, rightbound = n-1;
        int count = 1;
        int[][] res = new int[n][n];
        while(count<=n*n){
            for(int i=leftbound;i<=rightbound;i++){
                res[upperbound][i]=count++;
            }
            upperbound++;
            for(int i = upperbound;i<=lowerbound;i++){
                res[i][rightbound] = count++;
            }
            rightbound--;
            for(int i=rightbound;i>=leftbound;i--){
                res[lowerbound][i]=count++;
            }
            lowerbound--;
            for(int i=lowerbound;i>=upperbound;i--){
                res[i][leftbound]=count++;
            }
            leftbound++;
        }
        return res;
    }
}

loop控制边界(第几圈)

start 是起点; i j 需要是全局变量

中心点通过奇偶讨论

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值