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

首先是第一题,977.有序数组的平方:
暴力解法:

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

看看题解,确实想不到这样的题可以用双指针

class Solution {
    public int[] sortedSquares(int[] nums) {
        int n =nums.length;
        int[] ans = new int[n];
        int l =0;
        int r = n-1;
        int j =n-1;
        while(l<=r){
            if(nums[l]*nums[l]>nums[r]*nums[r]){
                ans[j] = nums[l]*nums[l];
                l++;
            }else{
                ans[j] = nums[r]*nums[r];
                r--;
            }
            j--;
        }
        return ans;
    }
}

20.长度最小的子数组
先自己想:
超时写法(19/20)

import java.util.*;
class Solution {
    public int minSubArrayLen(int target, int[] nums) {
            int n = nums.length;
            int all = nums[n-1];
            int ans = Integer.MAX_VALUE;
            if(nums[n-1]>=target)return 1;
            for(int i =0;i<n-1;i++){
                int tar = nums[i];
                all += nums[i];
                if(tar>=target){
                    ans = 1;
                    break;
                }
                for(int j = i+1;j<n;j++ ){
                    tar += nums[j];
                    if(tar>=target){
                        ans = Math.min(ans,j+1-i);
                        break;
                    }
                }
            }
            if(all<target)ans=0;
            
            return ans;
    }
}

双指针

class Solution {
    public int minSubArrayLen(int target, int[] nums) {
        int n = nums.length;
        int l =0;
        int result = Integer.MAX_VALUE;
        int sum =0;

        for(int i = 0;i<n;i++){
                sum += nums[i];

                while(sum>=target){
                    result = result <i-l+1  ?result:i-l+1;
                    sum -= nums[l];
                    l++;
                }
        }

        return result == Integer.MAX_VALUE?0:result;
    }
}

59.螺旋矩阵
大模拟,不熟练,偶尔还得回头看看(回头重刷)

class Solution {
    public int[][] generateMatrix(int n) {
        int[][] ans = new int[n][n];
        int num =1;
        int left = 0;
        int right =n-1;
        int top =0;
        int bottom =n-1;
        while(left<=right&&top<=bottom){
            for(int column = left;column<=right;column++){
                ans[top][column] = num;
                num++;
            }
            for(int row = top+1;row<=bottom;row++){
                ans[row][right]=num;
                num++;
            }
            if(left<right&& top<bottom){
                for(int column = right-1;column>left;column--){
                   ans[bottom][column] = num;
                   num++;
                }
                for(int row = bottom;row>top;row--){
                    ans[row][left]=num;
                    num++;
                }
            }
            left++;
            right--;
            top++;
            bottom--;
        }
        return ans;


    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值