代码随想录训练营第二天|977.有序数组的平方、209.长度最小的子数组、59.螺旋矩阵

文章探讨了如何使用双指针优化算法来解决LeetCode上的两个问题:有序数组的平方和长度最小的子数组。对于有序数组的平方问题,指出暴力循环虽然可行但效率低,正确方法是使用双指针和额外数组。在子数组问题中,通过滑动窗口的双指针法寻找满足条件的最小子数组长度。
摘要由CSDN通过智能技术生成

LeetCode 977 有序数组的平方
题目链接:有序数组的平方

思路:最简单的思路,暴力循环,先实现一下。

package Array;

import java.util.Arrays;
import java.util.List;

/**
 * @author rpstart
 * @create 2023-06-29 19:01
 */
public class sortedSquares977 {
    public static void main(String[] args) {
        int[] nums = {-4,-1,0,3,10};
        int[] res = sortedSquares(nums);
        System.out.println(Arrays.toString(res));
    }

    public static int[] sortedSquares(int[] nums) {
        for (int i = 0; i < nums.length; i++) {
            nums[i] = nums[i]*nums[i];
        }
        Arrays.sort(nums);
        return nums;
    }
}

暴力循环可以在力扣上通过,但是失去了本题考察的意义,接下来思考一下怎么能降低时间复杂度。一般来说双层循环的降维可以通过双指针来实现,因为双指针在数组中可以通过调整前后指针的遍历顺序,从而不使用双层循环中的内循环。

错误答案:

尝试思路1:通过指定双指针比较数组两端的平方后的数字大小,将大的数字放到数组的后面,但是这种情况忽略了全是负数等情况,并不能完全通过示例。

package Array;

import java.util.Arrays;
import java.util.List;

/**
 * @author rpstart
 * @create 2023-06-29 19:01
 */
public class sortedSquares977 {
    public static void main(String[] args) {
        int[] nums = {-4,-1,0,3,10};
        int[] res = sortedSquares(nums);
        System.out.println(Arrays.toString(res));
    }

    private static int[] sortedSquares(int[] nums) {
        int left = 0;
        int right = nums.length - 1;
        while (left <= right) {
            if (nums[right]*nums[right] > nums[left]*nums[left]) {
                nums[right] = nums[right]*nums[right];
                right--;
            } else {
                int temp = nums[left];
                nums[left] = nums[right];
                nums[right] = temp*temp;
                right--;
            }
        }
        return nums;
    }
    
}

正确思路:参考代码随想录。奥奥,原来思路确实是双指针,在本身的数组上实现覆盖不容易,可以重新定义数组来进行实现啊,懂了这个思路,实现代码如下。

   private static int[] sortedSquares(int[] nums) {
        int[] res = new int[nums.length];
        int index = nums.length - 1;
        for (int i = 0; i < nums.length; i++) {
            nums[i] = nums[i]*nums[i];
        }
        int left = 0;
        int right = nums.length - 1;
        while (left <= right) {
            if (nums[right] > nums[left]){
                res[index--] = nums[right];
                right--;
            } else {
                res[index--] = nums[left];
                left++;
            }
        }
        return res;
    }

反思:此题目还是考察双指针的运用,昨天的移除元素说明了数组元素不能被删除,只能被覆盖,但不是所有的题目都需要在原数组中进行处理,要进行灵活变通。

LeetCode 209 长度最小的子数组
题目链接:长度最小的子数组

初始思路:判断通过滑动窗口来进行操作,而滑动窗口本身就是对双指针的利用,第一层循环为双指针找到第一个符合条件的窗口,然后通过内层进行移动窗口,注意,第二层的窗口并不会增加时间复杂度。

package Array;

/**
 * @author rpstart
 * @create 2023-06-29 21:16
 */
public class minSubArrayLen209 {
    public static void main(String[] args) {
        int[] nums = {2,3,1,2,4,3};
        int target = 7;
        int res = minSubArrayLen(target, nums);
        System.out.println(res);
    }

    public static int minSubArrayLen(int target, int[] nums) {
        int left = 0;
        int sum = 0;
        int res = Integer.MAX_VALUE;
        for (int right = 0; right < nums.length; right++) {
            sum += nums[right];
            while (sum >= target) {
                res = Math.min(res, right - left + 1);
                sum -= nums[left];
                left++;
            }
        }
        return res == Integer.MAX_VALUE ? 0 : res;
    }
}

LeetCode 59 螺旋矩阵
题目链接:螺旋矩阵

思路:控制区间。

package Array;

import java.util.Arrays;

/**
 * @author rpstart
 * @create 2023-06-29 22:59
 */
public class generateMatrix59 {
    public static void main(String[] args) {

        int[][] res = generateMatrix(4);
        for (int i = 0; i < res.length; i++) {
            System.out.println(Arrays.toString(res[i]));
        }
        
    }

    public static int[][] generateMatrix(int n) {
        int loop = 0;
        int[][] res = new int[n][n];
        int start = 0;
        int num = 1;
        int i = 0;
        int j = 0;

        while (loop < n / 2) {
            loop++;
            for (j = start; j < n - loop; j++) {
                res[start][j] = num++;
            }
            for (i = start; i < n - loop; i++) {
                res[i][j] = num++;
            }
            for (; j > start; j--) {
                res[i][j] = num++;
            }
            for (; i > start; i--) {
                res[i][j] = num++;
            }
            start++;
        }

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值