两道题都做过
209
没注意是大于等于,改了一下马上通过了。
class Solution {
public int minSubArrayLen(int target, int[] nums) {
int l = 0, r = 0, len = nums.length;
int count = 0, ans = len + 1, now = 0;
while(r < len){
count += nums[r++];//r是下一个要加的
while(count >= target){//缩小l至小于,不可能等于,可能等于吗,可能的,直到小于
now = r - l;//不用加一
ans = Math.min(now, ans);
count -= nums[l++];
}
}
if(ans == len + 1){
return 0;
}
return ans;
}
}
59
59题曾经是我的噩梦,之前彻底弄懂之后发现这次一下就做出来了,夸夸自己。
class Solution {
public int[][] generateMatrix(int n) {
int[][] ans = new int[n][n];
int top = n-1, low = 0, left = 0, right = n-1;
int num = n * n, index = 1;
while(index <= num){
for(int i = left; i <= right; i++){
ans[low][i] = index++;
}
low++;
for(int i = low; i <= top; i++){
ans[i][right] = index++;
}
right --;
for(int i = right; i >= left; i--){
ans[top][i] = index++;
}
top--;
for(int i = top; i >= low; i--){
ans[i][left] = index++;
}
left++;
}
return ans;
}
}