209 长度最小的子数组
方法一:暴力破解
方法二:滑动窗口
滑动窗口重要代码
for (int i = 0; i < n; i++) {
sum += nums[i];
// 窗口滑动
while (sum >= target) {
ans = Math.min(ans, i - left +1);
sum -= nums[left++];
}
}
方法三:前缀和
59.螺旋矩阵II
模拟法、循环遵循左闭右开的原则
public int[][] generateMatrix(int n) {
int[][] res = new int[n][n];
int x = 0, y =0;
int offset = 1;
int count = 1;
int loop = 1;
int row,col;
while (loop <= n / 2) {
// 从左往右
for (col = y ; col < n - offset; col++) {
res[x][col] = count++;
}
// 从上往下
for (row = x; row < n -offset; row++) {
res[row][col] = count++;
}
//从右往左
for (;col > y;col--) {
res[row][col] = count++;
}
// 从下往上
for (;row > x;row--) {
res[row][col] = count++;
}
x++;
y++;
loop++;
offset++;
}
if (n%2 == 1) {
res[x][y] = count;
}
return res;
}
1054

被折叠的 条评论
为什么被折叠?



