第二天| 977.有序数组的平方,209.长度最小的子数组,59.螺旋矩阵II

 # 977_题目考察点:双指针

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

### 代码实现

package LeetCode;

public class YangSibo_977 {
    public static void main(String args []) {
        int [] nums = {-4,-1,0,3,10};
        int [] res1 = new int[nums.length];
        int [] res2 = new int[nums.length];
        YangSibo_977_1 demo1 = new YangSibo_977_1();
        YangSibo_977_2 demo2 = new YangSibo_977_2();
        res1 = demo1.sortedSquares(nums);
        res2 = demo1.sortedSquares(nums);
        for (int i = 0; i < nums.length; i++) {
            System.out.print(res1[i]);
            System.out.print(",");
        }
        System.out.println();
        for (int i = 0; i < nums.length; i++) {
            System.out.print(res2[i]);
            System.out.print(",");
        }
    }
}

class YangSibo_977_1 {
    public int[] sortedSquares(int[] nums) {
        int length = nums.length;
        int [] res = new int[length];
        if (length == 0) {
            return res;
        }
        if (length == 1) {
            int temp = nums[0] * nums[0];
            nums[0] = temp;
            return nums;
        }
        int [] squaresdata = new int[length];
        for(int i = 0; i < length; i++) {
            squaresdata[i]= nums[i] * nums[i];
        }
        for(int i = 0; i < length -1 ; i++) {
            for(int j = 0; j < length - i -1; j++) {
                if (squaresdata [j+1] < squaresdata[j]) {
                    int temp = squaresdata[j+1];
                    squaresdata[j+1] = squaresdata[j];
                    squaresdata[j] = temp;
                }
            }
        }
        return squaresdata;
    }
}
class YangSibo_977_2 {
    public int[] sortedSquares(int[] nums) {
        int length = nums.length;
        int [] res = new int[length];
        if (length == 0) {
            return res;
        }
        if (length == 1) {
            int temp = nums[0] * nums[0];
            nums[0] = temp;
            return nums;
        }
        int right = 0;
        int left = length - 1;
        int label = length - 1;
        while (right <= left) {
            int rightval = nums[right] * nums[right];
            int leftval = nums[left] * nums[left];
            if( rightval > leftval ) {
                res[label] = rightval;
                right++;
            }else {
                res[label] = leftval;
                left--;
            }
            label--;
        }
        return res;
    }
}

### 解题注意事项
1、因为数组事非递减的,平方和大的数据一定在两端,通过左右指针快速找到符合要求的值
2、也可以通过冒泡排序对获取的平方进行排序

# 209_题目考察点:双指针

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

### 代码实现

package LeetCode;

public class YangSibo_209 {
    public  static void main(String args []) {
        int [] nums = {2,3,1,2,4,3};
        int target = 7;
        YangSibo_209_1 demo1 = new YangSibo_209_1();
        YangSibo_209_2 demo2 = new YangSibo_209_2();
        System.out.println("demo1:" + demo1.minSubArrayLen(target,nums));
        System.out.println("demo2:" + demo2.minSubArrayLen(target,nums));
    }
}
//暴力破解
class YangSibo_209_1 {
    public int minSubArrayLen(int target, int[] nums) {
        int length = nums.length;
        if (length == 0 || (length == 1 && nums[0] < target)) {
            return 0;
        }
        if (length == 1 && nums[0] >= target) {
            return 1;
        }
        int minres = length;
        int flag = 0;
        for (int i = 0; i < length; i++) {
            for(int j = 0 ; j < length; j++) {
                int step = j - i + 1;
                if (step < 1) {
                    continue;
                }
                int stepsum = 0;
                for (int k = i; k <= j; k++){
                    stepsum = stepsum + nums[k];
                }
                if (stepsum >= target ) {
                    flag = 1;
                    if ( step < minres ){
                        minres = step;
                    }
                }
            }
        }
        if ( flag != 0) {
            return minres;
        }else {
            return 0;
        }
    }
}

class YangSibo_209_2 {
    public int minSubArrayLen(int target, int[] nums) {
        int length = nums.length;
        if (length == 0 || (length == 1 && nums[0] < target)) {
            return 0;
        }
        if (length == 1 && nums[0] >= target) {
            return 1;
        }
        int stepsum = 0;
        int minres = length;
        int flag = 0;
        for (int i = 0, j = 0; j < length; j++) {
            stepsum = stepsum + nums[j];
            while (stepsum >= target) {
                flag = 1;
                int step = j - i + 1;
                minres = Math.min(step,minres);
                stepsum = stepsum - nums[i];
                i = i + 1;
            }
        }
        if ( flag != 0) {
            return minres;
        }else {
            return 0;
        }
    }
}

### 解题注意事项
1、暴力解法就是遍历所有存在的数组区间,记得求的数据和
2、滑动窗口的方法是通过窗口的终点这一个变量,控制数据的走向,通过逐步挪动窗口的起点找寻更优的结果

# 59_题目考察点:二分法的区间定义

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

### 代码实现

package LeetCode;

public class YangSibo_59 {
    public static void main (String args []) {
    int n = 4;
    YangSibo_59_1 demo1 = new YangSibo_59_1();
    int demo1res [][];
    demo1res = demo1.generateMatrix(n);
    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++ ){
            System.out.println(demo1res[i][j]);
        }
    }
    }
}

class YangSibo_59_1 {
    public int[][] generateMatrix(int n) {
        int res[][] = new int[n][n];
        if (n == 1) {
            res[0][0] = 1;
            return res;
        }
        int larggestnum = n * n;
        int count = 1;
        int loop = 0;
        if (n % 2 != 0) {
            int midnum = n / 2;
            res[midnum][midnum] = larggestnum;
        }
        int i = 0, j = 0;

        while (count < larggestnum) {
            for (; j < n - loop- 1; j++) {
                res[i][j] = count;
                count++;
            }
            for (; i < n - loop- 1; i++) {
                res[i][j] = count;
                count++;
            }
            if(count > larggestnum){
                break;
            }
            for (; j > loop; j--) {
                res[i][j] = count;
                count++;
            }
            for (; i > loop; i--) {
                res[i][j] = count;
                count++;
            }
            i++;
            j++;
            loop ++;
        }
        return  res;
    }
}

### 解题注意事项
1、注意奇数和偶数结束的位置不相同,且每次都是向内循环的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值