Day 2 - LeetCode #977, #209, #59

文章介绍了如何利用两指针策略和滑动窗口思想解决LeetCode上的两个问题:有序数组的平方排序和长度最小的子数组。通过比较和调整指针位置,找到满足条件的最小子数组和每个数的平方值。
摘要由CSDN通过智能技术生成

#977, Squares of a sorted Array 

Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order.

Input: nums = [-4,-1,0,3,10]
Output: [0,1,9,16,100]
Explanation: After squaring, the array becomes [16,1,0,9,100].
After sorting, it becomes [0,1,9,16,100].

Notes:

loop from the highest index to lowest using two pointers, while comparing the abs value of first index and last index. 

Highlight :

  1. int index = nums.length - 1 ; then index-- in while loop
  2. when two points meet (left==right), fill in the last(middle) element 

#209 

 209.长度最小的子数组

题目建议: 本题关键在于理解滑动窗口,这个滑动窗口看文字讲解 还挺难理解的,建议大家先看视频讲解。  拓展题目可以先不做。 

题目链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

文章讲解:代码随想录

视频讲解:拿下滑动窗口! | LeetCode 209 长度最小的子数组_哔哩哔哩_bilibili

Note: 
sliding windows, two pointers - left, right. In for loop, move right pointer and in parallel calculate the sum, while sum >= target,
1. move left pointer to the right
2. modify the current sum - minus the previous left pointer value
3. record the index length, do a Math.min of current result

  public int minSubArrayLen(int target, int[] nums) {

        int res = Integer.MAX_VALUE;

        int left = 0;

        int sum = 0;

        for (int right = 0; right <= nums.length - 1; right++) {

            sum += nums[right];

            while(sum >= target) {

                res = Math.min(res, right - left + 1);

                sum -= nums[left];

                left++;

            }

        }    

        if(res == Integer.MAX_VALUE) return 0;

        return res;    

    }

 59.螺旋矩阵II

题目建议:  本题关键还是在转圈的逻辑,在二分搜索中提到的区间定义,在这里又用上了。 

题目链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

文章讲解:代码随想录

视频讲解:一入循环深似海 | LeetCode:59.螺旋矩阵II_哔哩哔哩_bilibili

Note : 

traverse in four different directions, each move do check the boundary.  In other words:

when move from left to right, make sure it within top and bottom boundaries

when move from top to bottom, make sure it is within left to right boundaries.

12/31 not sure why, but it is required to make the code work.

int m = matrix.length, n = matrix[0].length;
    int upper_bound = 0, lower_bound = m - 1;
    int left_bound = 0, right_bound = n - 1;
    List<Integer> res = new LinkedList<>();
    // res.size() == m * n 则遍历完整个数组
    while (res.size() < m * n) {
        if (upper_bound <= lower_bound) {
            // 在顶部从左向右遍历
            for (int j = left_bound; j <= right_bound; j++) {
                res.add(matrix[upper_bound][j]);
            }
            // 上边界下移
            upper_bound++;
        }
        
        if (left_bound <= right_bound) {
            // 在右侧从上向下遍历
            for (int i = upper_bound; i <= lower_bound; i++) {
                res.add(matrix[i][right_bound]);
            }
            // 右边界左移
            right_bound--;
        }
        
        if (upper_bound <= lower_bound) {
            // 在底部从右向左遍历
            for (int j = right_bound; j >= left_bound; j--) {
                res.add(matrix[lower_bound][j]);
            }
            // 下边界上移
            lower_bound--;
        }
        
        if (left_bound <= right_bound) {
            // 在左侧从下向上遍历
            for (int i = lower_bound; i >= upper_bound; i--) {
                res.add(matrix[i][left_bound]);
            }
            // 左边界右移
            left_bound++;
        }
    }
    return res;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值