代码随想录Day2 数组 part 02

977. Squares of a Sorted Array

C++解法

class Solution {
public:
    vector<int> sortedSquares(vector<int>& A) {
        for (int i = 0; i < A.size(); i++){
            A[i] *= A[i];
        }
        sort(A.begin(), A.end());   //sorting 
        return A;
    }
};


后面转到了Java

解法1:

class Solution {
    public int[] sortedSquares(int[] nums) {
        int right = nums.length - 1;
        int left = 0;
        int[] result = new int [nums.length];
        int index = result.length - 1;
        while (left <= right){
            if (nums[left] * nums[left] > nums[right] * nums[right]){
                result[index--] = nums[left] * nums[left];
                ++left;
            } else {
                result[index--] = nums[right] * nums[right];
                --right;
            }
        }
        return result;
    }
}

解法2: 

class Solution {
    public int[] sortedSquares(int[] nums) {

        int l = 0;
        int r = nums.length - 1;
        int[] res = new int[nums.length];
        int j = nums.length - 1;
        while (l <= r){
            if(nums[l] * nums[l] > nums[r] * nums[r]){
                res[j--] = nums[l] * nums[l++];
            } else {
                res[j--] = nums[r] * nums[r--];
            }
        }
        return res;
        
    }
}

两种解法都是O(n)complexity,解法大致相同。

209. Minimum Size Subarray Sum

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

59. Spiral Matrix II

59.螺旋矩阵II

题目链接:. - 力扣(LeetCode)

文章讲解:代码随想录

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

class Solution {
    public int[][] generateMatrix(int n) {
        int[][] nums = new int[n][n];
        int startX = 0, startY = 0;
        int offset = 1;
        int count = 1;
        int loop = 1;
        int i, j;

        while (loop <= n/2){
            for (j = startY; j < n - offset; j++){
                nums[startX][j] = count++;
            }

            for (i = startX; i < n - offset; i++){
                nums[i][j] = count++;
            }

            for(; j > startY; j--){
                nums[i][j] = count++;
            }

            for (; i > startX; i--){
                nums[i][j] = count++;
            }

            startX++;
            startY++;
            offset++;
            loop++;
        }
        if (n % 2 == 1) {
            nums[startX][startY] = count;
        }
        return nums;
    }
}

做这一道开始觉得有意思了起来,接下来也觉得补起前面的打卡更有动力了一些呢。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值