day2-数组-算法训练营

一、题目

  • 给你一个按 非递减顺序 排序的整数数组 nums,返回 每个数字的平方 组成的新数组,要求也按 非递减顺序 排序。
  • 找出该数组中满足其和 ≥ target 的长度最小的 连续子数组 [numsl, numsl+1, …, numsr-1, numsr] ,并返回其长度。如果不存在符合条件的子数组,返回0。
  • 给你一个正整数 n ,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix 。

二、答案

1. 题1

public class Day2_1_Square_of_ordered_array {
    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. 题2

public class Day2_2_Subarray_with_the_smallest_length {
    public int minSubArrayLen(int s, 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>= s){
                result = Math.min(result , right-left+1);
                sum -= nums[left++];
            }
        }
        return result == Integer.MAX_VALUE ? 0 :result;
    }
}

3. 题3

public class Day2_3_Spiral_matrix {
    public int[][] Spiral(int n){
                int[][] res = new int[n][n];
                int startx = 0, starty = 0; //坐标
                int loop = n/2 ;            //旋转次数(n为单数时向下取证)
                int mid  = n/2 ;            //矩阵中心(n为单数时)
                int offset = 1;             //需要控制每一条边遍历的长度,每次循环右边界收缩一位
                int count = 1;              //用来给矩阵每一个空格赋值
                int i,j;
                while (loop-- >= 0 ){
                    i = startx;
                    j = starty;

                    for(j = starty; j < n-offset ; j++ ){
                        res[startx][j] = count++;
                    }
                    for (i = startx; i < n-offset; i++ ){
                        res[i][j] = count++;
                    }
                    for ( ; j > starty ; j--){
                        res[i][j] = count++;
                    }
                    for ( ; i > startx ; i-- ){
                        res[i][j] = count++;
                    }
                    startx++;
                    starty++;
                    offset += 1;
                }
                if ( n%2 == 1){
                    res[mid][mid] = count;
                }
                return res;
    }
}

三、思想总结

  1. 双指针
    在这里插入图片描述

  2. 滑动窗口
    在这里插入图片描述

  3. 左闭右开,循环执行
    在这里插入图片描述

以上图片引用自:https://programmercarl.com

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值