第五天噜——
加油加油!
一. 最小覆盖子串
题目链接:力扣
思路:
因为刷的还是滑动窗口相关的题,所以这道题还是用滑动窗口去做。
滑动窗口的思想也很简单,先使用 i,j将该数组完全包含起来,通过不断地改变 i j,来扩展和收缩滑动窗口,可以想象为一个滑动窗口在数组上游走,直到寻找到题目要求的“包含字符串T的所有元素”,记录下该长度 j-i+1,那么这些记录中的最小值就是要求的结果。
基本步骤:
步骤一
不断增加j使滑动窗口增大,直到窗口包含了T的所有元素
步骤二
不断增加i使滑动窗口缩小,因为是要求最小字串,所以将不必要的元素排除在外,使长度减小,直到碰到一个必须包含的元素,这个时候不能再扔了,再扔就不满足条件了,记录此时滑动窗口的长度,并保存最小值
步骤三
让i再增加一个位置,这个时候滑动窗口肯定不满足条件了,那么继续从步骤一开始执行,寻找新的满足条件的滑动窗口,如此反复,直到j超出了字符串S范围。
代码:保姆级注释
class Solution {
public String minWindow(String s, String t) {
//首先创建的是need数组表示每个字符在t中需要的数量,用ASCII码来保存
//加入need[76] = 2,表明ASCII码为76的这个字符在目标字符串t中需要两个,如果是负数表明当前字符串在窗口中是多余的,需要过滤掉
int[] need = new int[128];
//按照字符串t的内容向need中添加元素
for (int i = 0; i < t.length(); i++) {
need[t.charAt(i)]++;
}
/*
l: 滑动窗口左边界
r: 滑动窗口右边界
size: 窗口的长度
count: 当次遍历中还需要几个字符才能够满足包含t中所有字符的条件,最大也就是t的长度
start: 如果有效更新滑动窗口,记录这个窗口的起始位置,方便后续找子串用
*/
int l = 0, r = 0, size = Integer.MAX_VALUE, count = t.length(), start = 0;
//循环条件右边界不超过s的长度
while (r < s.length()) {
char c = s.charAt(r);
//表示t中包含当前遍历到的这个c字符,更新目前所需要的count数大小,应该减少一个
if (need[c] > 0) {
count--;
}
//无论这个字符是否包含在t中,need[]数组中对应那个字符的计数都减少1,利用正负区分这个字符是多余的还是有用的
need[c]--;
//count==0说明当前的窗口已经满足了包含t所需所有字符的条件
if (count == 0) {
//如果左边界这个字符对应的值在need[]数组中小于0,说明他是一个多余元素,不包含在t内
while (l < r && need[s.charAt(l)] < 0) {
//在need[]数组中维护更新这个值,增加1
need[s.charAt(l)]++;
//左边界向右移,过滤掉这个元素
l++;
}
//如果当前的这个窗口值比之前维护的窗口值更小,需要进行更新
if (r - l + 1 < size) {
//更新窗口值
size = r - l + 1;
//更新窗口起始位置,方便之后找到这个位置返回结果
start = l;
}
//先将l位置的字符计数重新加1
need[s.charAt(l)]++;
//重新维护左边界值和当前所需字符的值count
l++;
count++;
}
//右移边界,开始下一次循环
r++;
}
return size == Integer.MAX_VALUE ? "" : s.substring(start, start + size);
}
}
二. 螺旋矩阵 II
题目链接:力扣
思路:没啥思路,最基本的模拟,模拟着敲出来出来就完事,需要注意的就是起始和结束下标。
非常考察对二维数组的理解!
代码:
class Solution {
public int[][] generateMatrix(int n) {
int maxNum = n * n;
int curNum = 1;
int[][] matrix = new int[n][n];
int row = 0, column = 0;
int[][] directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; // 右下左上
int directionIndex = 0;
while (curNum <= maxNum) {
matrix[row][column] = curNum;
curNum++;
int nextRow = row + directions[directionIndex][0], nextColumn = column + directions[directionIndex][1];
if (nextRow < 0 || nextRow >= n || nextColumn < 0 || nextColumn >= n || matrix[nextRow][nextColumn] != 0) {
directionIndex = (directionIndex + 1) % 4; // 顺时针旋转至下一个方向
}
row = row + directions[directionIndex][0];
column = column + directions[directionIndex][1];
}
return matrix;
}
}
三. 螺旋矩阵
题目链接:力扣
思路:
同上,一圈一圈的模拟,大体上都是很类似的,只要能做出来上一题,这道题不成问题!
代码:
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
LinkedList<Integer> result = new LinkedList<>();
if(matrix==null||matrix.length==0) return result;
int left = 0;
int right = matrix[0].length - 1;
int top = 0;
int bottom = matrix.length - 1;
int numEle = matrix.length * matrix[0].length;
while (numEle >= 1) {
for (int i = left; i <= right && numEle >= 1; i++) {
result.add(matrix[top][i]);
numEle--;
}
top++;
for (int i = top; i <= bottom && numEle >= 1; i++) {
result.add(matrix[i][right]);
numEle--;
}
right--;
for (int i = right; i >= left && numEle >= 1; i--) {
result.add(matrix[bottom][i]);
numEle--;
}
bottom--;
for (int i = bottom; i >= top && numEle >= 1; i--) {
result.add(matrix[i][left]);
numEle--;
}
left++;
}
return result;
}
}