今日学习的文章链接,或者视频链接
977.有序数组的平方
题目建议: 本题关键在于理解双指针思想
题目链接:https://leetcode.cn/problems/squares-of-a-sorted-array/
文章讲解: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/
视频讲解:https://www.bilibili.com/video/BV1tZ4y1q7XE
59.螺旋矩阵II
题目建议: 本题关键还是在转圈的逻辑,在二分搜索中提到的区间定义,在这里又用上了。
题目链接:https://leetcode.cn/problems/spiral-matrix-ii/
文章讲解:https://programmercarl.com/0059.%E8%9E%BA%E6%97%8B%E7%9F%A9%E9%98%B5II.html
视频讲解:https://www.bilibili.com/video/BV1SL4y1N7mV/
自己看到题目的第一想法
- 有序数组的平方
已经有了顺序数组(含负数),简单想就是数组平方后再次排序,时间复杂度会很高
绝对值以后再排序,其实和平方以后排序没有什么区别
提示了双指针也没有什么思路
- 长度最小的子数组
试着用双指针,慢指针用于遍历数组,快指针用于计算
- 螺旋矩阵
没思路
看完代码随想录之后的想法
- 有序数组的平方
起始一开始差不多也想到了类似的思路,但是习惯性不动脑子想着抄答案,得改
2. 长度最小的子数组
滑动窗口其实也是双指针的思想,用一个 for 循环完成两个 for 循环的任务,所以 for 循环条件是数组的终止位置
3. 螺旋矩阵 II
自己做的时候最好画个图,还是用左开右闭确认边界条件的方式做
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define min(x,y) ((x>y)?(y):(x))
/*
977. 有序数组的平方,根据题目,平方后的最大值一定会出现在源数组的最左端或者最右端
时间复杂度:O(n),空间复杂度:O(n)
input:
array:数组
size:数组大小
*/
void sqrtNum(int *oldArray, int *newArray, int size){
int left = 0, right = size-1;
int newRight = right;
while (left<=right){
if (oldArray[left]*oldArray[left] < oldArray[right]*oldArray[right]){
newArray[newRight--] = oldArray[right]*oldArray[right];
right--;
}else{
newArray[newRight--] = oldArray[left]*oldArray[left];
left++;
}
}
return ;
}
void showArray(int *array, int size){
for (size_t i = 0; i < size; i++){
printf("%d ", *(array+i));
}
printf("\n");
return ;
}
/* 209.长度最小的子数组 暴力破解,时间复杂度:O(n^2),空间复杂度:O(1)
int target = 7;
int nums[6] = {2,3,1,2,4,3};
*/
int minSubArrayLen_violence(int target, int* nums, int numsSize){
// 空数组判断
if (0 == numsSize){
return 0;
}
int tmpSum = 0;
int subLength = 0;
int result = __INT_MAX__;
for (size_t i = 0; i < numsSize; i++){ // 子序列起点位置
tmpSum = 0;
for (size_t j = i; j < numsSize; j++){ // 子序列终点位置
tmpSum += nums[j];
if (tmpSum >= target){ // 当子序列之和大于target时,此时不需要再去加之后的数字
subLength = j-i+1;
result = (subLength < result ? subLength : result);
break; // c语言中break
}
}
}
return __INT_MAX__ == result ? 0 : result;
}
/* 209.长度最小的子数组 滑动窗口,时间复杂度:O(n)
int target = 7;
int nums[6] = {2,3,1,2,4,3};
*/
int minSubArrayLen(int target, int* nums, int numsSize){
// 空数组判断
if (0 == numsSize){
return 0;
}
int sum = 0, sublength = 0;
int j = 0, result = __INT_MAX__; // j控制窗口头部
for (size_t i = 0; i < numsSize; i++ ) { // for循环控制窗口尾部
sum += nums[i];
while (sum >= target){
sublength = i - j + 1;
result = min(result, sublength);
sum -= nums[j++]; // sum中减去left值,窗口向前移动
}
}
return result == __INT_MAX__ ? 0 : sublength;
}
/*
59. 螺旋矩阵 II,有错误,思路是没问题的
*/
int** generateMatrix(int n, int* returnSize, int** returnColumnSizes){
if (n < 1 || n > 20){
return NULL;
}
int** matrix = malloc(sizeof(int*) * n);
*returnSize = n;
*returnColumnSizes = malloc(sizeof(int) * n);
for (int i = 0; i < n; i++) {
matrix[i] = malloc(sizeof(int) * n);
memset(matrix[i], 0, sizeof(int) * n);
(*returnColumnSizes)[i] = n;
}
int count = 1;
int startX = 0, startY = 0; // 起始位置
int offset = 1; // 偏移,左闭右开
int loop = n/2;
int mid = n/2;
size_t i, j;
while (loop--){
i = startX, j = startY;
// 都是以左闭右开的规则处理的
for ( ; j < n-offset; j++){
matrix[startX][j] = count++;
}
for ( ; i < n-offset; i++){
matrix[i][j] = count++;
}
for ( ; i > startX; i--){
matrix[i][j] = count++;
}
for ( ; j > startY; j--){
matrix[i][j] = count++;
}
startX++, startY++, offset++;
}
if (n%2){
returnColumnSizes[mid][mid] = count;
}
return returnColumnSizes;
}
int main(int argc, char const *argv[])
{
int n = 5;
int returnSize = 0;
generateMatrix(n, &returnSize, NULL);
return 0;
}