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

977.有序数组的平方

https://leetcode.cn/problems/squares-of-a-sorted-array/

暴力求解

时间复杂度: O(n + nlogn),O(nlogn)

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;
    }
}

双指针

时间复杂度:O(n)

class Solution {
    public int[] sortedSquares(int[] nums) {
        // 双指针方法
        int len = nums.length;
        int left = 0, right = nums.length - 1;
        int[] result = new int[len];
        for(int p = len -1; p >=0 ; p--){
            if(Math.abs(nums[left]) < Math.abs(nums[right])){
                result[p] = nums[right] * nums[right];
                right -- ;
            }
            else{
                result[p] = nums[left] * nums[left];
                left ++;
            }
        }
        return result;
    }
}

209.长度最小的子数组

暴力解法

class Solution {
    public int minSubArrayLen(int target, int[] nums) {
        int len = nums.length;
        int result = Integer.MAX_VALUE;
        for(int i = 0; i < len; i ++){
            int sum = 0; // 这里不能把 num[i]加上,如果加上会导致只有一个大于目标的元素不会被考虑进去
            for(int j = i; j < len; j++){
                sum += nums[j];
                if(sum >= target){
                    if( (j - i + 1)  <  result ){
                        result = j - i + 1;
                    }else{
                        break;
                    }
                }
            }
        }
        if(result == Integer.MAX_VALUE){
            return 0;
        }else{
            return result;
        }
    }
}

滑动窗口解法

class Solution {
    public int minSubArrayLen(int target, int[] nums) {
        // 滑动窗口
        int len = nums.length, sum = 0, i = 0 ;
        int result = Integer.MAX_VALUE;
        int subString = 0;
        for(int j = 0; j < len; j ++){
            sum += nums[j];
            while(sum >= target){ // 这里必须是while,因为可能左指针滑动之后
            //sum还是大于target
                subString = j - i + 1;
                result = result > subString ? subString : result;
                sum -= nums[i];
                i ++;
            }
        }
        return  result == Integer.MAX_VALUE?0:result;
    }
}

59. 螺旋矩阵 II

class Solution {
    public int[][] generateMatrix(int n) {
        int[][] res = new int[n][n];
        int loop = 0;
        int start = 0;
        int offset = 1;
        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 > start; j--){
                res[i][j] = count ++;
            }

            for(;i > start; i--){
                res[i][j] = count ++;
            }

            start ++;
        }

        if(n % 2 != 0){
            res[start][start] = count ++;
        }
        return res;
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值