算法刷题 Day 2 | 977.有序数组的平方 & 209.长度最小的子数组 & 59.螺旋矩阵II
977.有序数组的平方
题目建议: 本题关键在于理解双指针思想
题目链接:https://leetcode.cn/problems/squares-of-a-sorted-array/
Tips:这道题没有空间复杂度要求,最开始以为是要在原数组上修改,后来发现是自己想复杂了。
我的题解:
class Solution {
public int[] sortedSquares(int[] nums) {
int pLeft = 0;
int pRight = nums.length - 1;
int[] result = new int[nums.length];
int pos = nums.length - 1;
while(pLeft <= pRight){
int squareLeft = nums[pLeft] * nums[pLeft];
int squareRight = nums[pRight] * nums[pRight];
if(squareLeft >= squareRight){
result[pos--] = squareLeft;
pLeft++;
}
else{
result[pos--] = squareRight;
pRight--;
}
}
return result;
}
}
文章讲解:https://programmercarl.com/0977.%E6%9C%89%E5%BA%8F%E6%95%B0%E7%BB%84%E7%9A%84%E5%B9%B3%E6%96%B9.html
视频讲解: https://www.bilibili.com/video/BV1QB4y1D7ep
209.长度最小的子数组
题目建议: 本题关键在于理解滑动窗口,这个滑动窗口看文字讲解 还挺难理解的,建议大家先看视频讲解。 拓展题目可以先不做。
题目链接:https://leetcode.cn/problems/minimum-size-subarray-sum/
Tips:这一题有很多内容需要注意
1.滑动窗口的循环结束条件以右端点到头为准,然后在最后一次循环内部再尽可能地往右移动左端点,直到不满足题设条件。
2.Java中int的最大值可以通过Integer.MAX_VALUE
获得。
3.取最小值的函数是Math.min(a,b)
我的题解:
class Solution {
public int minSubArrayLen(int target, int[] nums) {
int pLeft = 0;
int pRight = 0;
int sum = 0;
int result = Integer.MAX_VALUE;
for(;pRight < nums.length;pRight++){
sum += nums[pRight];
while(sum>=target){
int length = pRight - pLeft + 1;
result = Math.min(length,result);
sum -= nums[pLeft++];
}
}
if(result == Integer.MAX_VALUE){
return 0;
}
else{
return result;
}
}
}
视频讲解:https://www.bilibili.com/video/BV1tZ4y1q7XE
59.螺旋矩阵II
题目建议: 本题关键还是在转圈的逻辑,在二分搜索中提到的区间定义,在这里又用上了。
题目链接:https://leetcode.cn/problems/spiral-matrix-ii/
Tips:就像贪吃蛇一样,一共四种移动方式,每次只要撞到墙或者撞到已经去过的地方,就调转到下一个方向即可,直到走完所有的格子。
我的题解:
class Solution {
public int[][] generateMatrix(int n) {
int[][] matrix = new int[n][n];
int[][] history = new int[n][n];
int row = 0;
int column = 0;
int counter = 0;
int movingPattern = 0; //分为0、1、2、3一共四种方向
while(counter<n*n){
matrix[row][column] = ++counter;
history[row][column] = 1;
if(movingPattern == 0){
if(column+1 < n && history[row][column+1]==0){
column++;
}
else{
row++;
movingPattern = 1;
}
}
else if(movingPattern == 1){
if(row+1 < n && history[row+1][column]==0){
row++;
}
else{
column--;
movingPattern = 2;
}
}
else if(movingPattern == 2){
if(column-1 >=0 && history[row][column-1]==0){
column--;
}
else{
row--;
movingPattern = 3;
}
}
else if(movingPattern == 3){
if(row-1 >=0 && history[row-1][column]==0){
row--;
}
else{
column++;
movingPattern = 0;
}
}
}
return matrix;
}
}
文章讲解:https://programmercarl.com/0059.%E8%9E%BA%E6%97%8B%E7%9F%A9%E9%98%B5II.html
视频讲解:https://www.bilibili.com/video/BV1SL4y1N7mV/
数组总结
文章链接:https://programmercarl.com/%E6%95%B0%E7%BB%84%E6%80%BB%E7%BB%93%E7%AF%87.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构