今日任务
常用华东窗口模版,前缀和
209.长度最小的子数组
题目链接:. - 力扣(LeetCode)
本题的模版就是处理滑动窗口最好的模版,其他的方法对于判断边界值啥的,非常复杂。
class Solution {
public int minSubArrayLen(int target, int[] nums) {
int start = 0;
int res = Integer.MAX_VALUE;
int sum = 0;
for (int i = 0; i <nums.length; i++) {
sum += nums[i];
while(sum >= target){
res = Math.min(res, i - start + 1);
sum -= nums[start++];
}
}
return res == Integer.MAX_VALUE ? 0 : res;
}
}
59.螺旋矩阵II
题目链接:. - 力扣(LeetCode)
本题的难度在于两点
1.边界值选择
2. 代码书写时边界值的处理
class Solution {
public int[][] generateMatrix(int n) {
int startX = 0;
int startY = 0;
int loop = 0;
int offset = 1;
int count = 1;
int[][] result = new int[n][n];
int i,j;
while (loop < n/2) {
//从左到右
for (j = startY; j < n - offset; j++) {
result[startX][j] = count++;
}
//从上到下
for (i = startX; i < n -offset; i++){
result[i][j] = count++;
}
//从右到左
for (; j > startY; j--) {
result[i][j] = count++;
}
//从下到上
for(; i > startX; i--){
result[i][j] = count++;
}
startX++;
startY++;
loop++;
offset++;
}
if ( n % 2 == 1 ) {
result[startX][startY] = count;
}
return result;
}
}
前缀和:
前缀和题目,因为在公司内无法访问,因此没有具体代码书写。
任务布置中的2个题目都看了,根据题目意思理解前缀和就是将数组前n个数据累加的值组成的一个数组。,比如preSum[k]表示数组前k个值(0-k)累加的值。
1823

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



