代码随想录算法训练营第二天|数组理论基础,Leetcode 977.有序数组的平方 | 209.长度最小的子数组| 59.螺旋矩阵II

文章介绍了两道LeetCode上的问题:一是如何在有序数组中平方所有元素并保持排序,可以使用直接平方后再排序或双指针的方法;二是寻找数组中满足特定条件的最小子数组长度,采用双指针从结束位置开始移动。另外还涉及了构建螺旋矩阵的算法,强调了循环不变量的重要性。
摘要由CSDN通过智能技术生成

 977.有序数组的平方 Squares of a Sorted Array - LeetCode

非双指针做法,直接把array里的数字平方,然后sort

time O(NlogN)

Space On

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

双指针做法:

1.开一个新的数组存储答案

2.因为有负数,所以平方后大的数字肯定是在两头
 

class Solution {
    public int[] sortedSquares(int[] nums) {
        int left = 0;
        int right = nums.length - 1;
        int[] res = new int[nums.length];
        int idx = nums.length - 1;

        while (left <= right) {
            if (nums[left] * nums[left] < nums[right] * nums[right]) {
                res[idx--] = nums[right] * nums[right];
                right--;
            } else {
                res[idx--] = nums[left] * nums[left];
                left++;
            }
        }
        return res;
    }
}

Leetcode209 sliding window题 Minimum Size Subarray Sum - LeetCode

双指针思路:for循环表示的是起始位置还是终止位置,假设是起始位置,终止位置要把后面全部循环一遍,和暴力解法没区别。 所以一定是终止位置。

 起始位置用什么时候移动,

res = 最大值

i = 0;

for (j = 0; j < nums.length; j++)

        sum += nums[j]

              (sum >= s){// if 还是while?1111,100 需要持续移动来更新,所以用while

                        subL = j - i + 1;

                        res = min(res, subL);

                        sum = sum - nums[i];

                        i++;

                }

class Solution {
    public int minSubArrayLen(int target, int[] nums) {
        int res = Integer.MAX_VALUE;
        int sum = 0;
        int i = 0;

        for (int j = 0; j < nums.length; j++) {
            sum += nums[j];
            while (sum >= target) {
                int subLen = j - i + 1;
                res = Math.min(res, subLen);
                sum = sum - nums[i];
                i++;
            }
        }
        return res == Integer.MAX_VALUE ? 0 : res;
    }
}

Leetcode 59 Spiral Matrix II - LeetCode

要明确循环不变量,每一条边的规则要统一,每条边的最后一个数留给下一条边来处理(就是左闭右开原则)

startX = 0;

startY = 0;

offSet = 1;

count = 1;

class Solution {
    public int[][] generateMatrix(int n) {
        int start = 0;
        //int startY = 0;
        int offSet = 1;
        int count = 1;
        int loop = n / 2;
        int i, j;
        int[][] res = new int[n][n];

        while (loop-- > 0) {
            for ( j = start; j < n - offSet; j++) {
                res[start][j] = count++;
            }
            for ( i = start; i < n - offSet; i++) {
                res[i][j] = count++;
            }
            for (; j > start; j--) {
                res[i][j] = count++;
            }
            for (; i > start; i--) {
                res[i][j] = count++;
            }
            start++;
          //  startY++;
            offSet += 1;
        }
        if (n % 2 == 1) {
            res[start][start] = count;
        }
        return res;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值