代码随想录第二天|209.长度最小的子数组(滑动窗口)
209.长度最小的子数组
class Solution { public int minSubArrayLen(int target, int[] nums) { int left = 0; int sum = 0; //int result = Integer.MAX_VALUE;无法编译,改成长度+1 int result = nums.length + 1; 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 == nums.length + 1 ? 0 : result; } }
59.螺旋矩阵II
class Solution { public int[][] generateMatrix(int n) { int[][] nums = new int[n][n]; //start piont every loop; int startX = 0; int startY = 0; //set num every loop; int flag = 1; //fill the maxtrix; int count = 1; //count loop number; int loop = 1; int i, j; while(loop <= n/2){ //the top row for(j = startY; j < n - flag; j++){ nums[startX][j] = count++; } //right column for(i = startX; i < n - flag; i++){ nums[i][j] = count++; } //bottom row for(;j > startY; j--){ nums[i][j] = count++; } //left column for(; i > startX; i--){ nums[i][j] = count++; } startX++; startY++; flag++; loop++; } if(n % 2 == 1){ nums[startX][startY] = count; } return nums; } }
区间和 以后看
总结: