class Solution {
public int minSubArrayLen(int target, int[] nums) {
int left = 0;int right = 0;
int sum = 0;
int len = Integer.MAX_VALUE;
while(right<nums.length){
sum += nums[right];
while(sum>=target){
len = len>(right - left +1)?(right - left +1):len;
sum -= nums[left++];
}
right++;
}
return len==Integer.MAX_VALUE?0:len;
}
}
版本一,最后一边有问题。
class Solution {
public int[][] generateMatrix(int n) {
int[][] nums = new int[n][n];
int startX = 0, startY = 0, offset = 0, count = 1;
while (offset < n / 2) {
for (int i = startX; i < n - 1 - offset; i++) {
nums[startY][i] = count++;
}
for (int i = startY; i < n - 1 - offset; i++) {
nums[i][n - 1 - offset] = count;
count++;
}
startY++;
for (int i = n - 1 - offset; i >0; i--) {
nums[n - 1 - offset][i] = count;
count++;
}
for (int i = n - 1 - offset; i > 0; i--) {
nums[i][startX] = count;
count++;
}
startX++;
offset++;
}
if (n % 2 != 0) {
nums[n / 2][n / 2] = count;
}
return nums;
}
}
版本二,GPT给的。
class Solution {
public int[][] generateMatrix(int n) {
int[][] nums = new int[n][n];
int startX = 0, startY = 0, endX = n - 1, endY = n - 1, count = 1;
while (startX <= endX && startY <= endY) {
for (int i = startY; i <= endY; i++) {
nums[startX][i] = count++;
}
startX++;
for (int i = startX; i <= endX; i++) {
nums[i][endY] = count++;
}
endY--;
if (startX <= endX) {
for (int i = endY; i >= startY; i--) {
nums[endX][i] = count++;
}
endX--;
}
if (startY <= endY) {
for (int i = endX; i >= startX; i--) {
nums[i][startY] = count++;
}
startY++;
}
}
return nums;
}
}
属于是螺旋矩阵没螺明白。