代码随想录算法训练营第二天|977.有序数组的平方 ,209.长度最小的子数组 ,59.螺旋矩阵II

一、参考资料

有序数组的平方

题目链接: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

长度最小的子数组

题目链接:https://leetcode.cn/problems/minimum-size-subarray-sum/

文章讲解:https://programmercarl.com/0209.%E9%95%BF%E5%BA%A6%E6%9C%80%E5%B0%8F%E7%9A%84%E5%AD%90%E6%95%B0%E7%BB%84.html

视频讲解:https://www.bilibili.com/video/BV1tZ4y1q7XE

螺旋矩阵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/

数组专题总结

文章链接:https://programmercarl.com/%E6%95%B0%E7%BB%84%E6%80%BB%E7%BB%93%E7%AF%87.html

二、LeetCode977-有序数组的平方

我的代码(过于暴力)
  1. class Solution {
  2. public:
  3. vector<int> sortedSquares(vector<int>& nums) {
  4. vector<int> res;
  5. for (int i = 0; i < nums.size(); i++){
  6. nums[i] = nums[i] * nums[i];
  7. }
  8. sort(nums.begin(), nums.end());
  9. return nums;
  10. }
  11. };

接下来学习一下双指针的代码实现:

双指针实现
  1. class Solution {
  2. public:
  3. vector<int> sortedSquares(vector<int>& nums) {
  4. // 这里定义一个结果集,长度为原数组的大小
  5. vector<int> res(nums.size(), 0);
  6. // 定义首尾指针
  7. int firstp = 0;
  8. int lastp = nums.size() - 1;
  9. int k = lastp; // k表示结果集的指针,从后向前写入vector
  10. while (firstp <= lastp){
  11. if (nums[firstp] * nums[firstp] < nums[lastp] * nums[lastp]){
  12. res[k--] = nums[lastp] * nums[lastp];
  13. lastp--;
  14. }
  15. else{
  16. res[k--] = nums[firstp] * nums[firstp];
  17. firstp++;
  18. }
  19. }
  20. return res;
  21. }
  22. };

三、LeetCode209-长度最小的子数组

最大的收获是学习了滑动窗口的写法!视频讲解很详细

滑动窗口-代码实现
  1. class Solution {
  2. public:
  3. int minSubArrayLen(int target, vector<int>& nums) {
  4. int result = INT32_MAX;
  5. int sum = 0;
  6. // 滑动窗口的起始位置
  7. int begin = 0;
  8. // 滑动窗口的长度
  9. int subLen = 0;
  10. for (int i = 0; i < nums.size(); i++){
  11. sum += nums[i];
  12. while (sum >= target){
  13. subLen = (i - begin + 1);
  14. result = result < subLen ? result : subLen;
  15. sum -= nums[begin];
  16. begin++;
  17. }
  18. }
  19. if (result == INT32_MAX){
  20. return 0;
  21. }
  22. return result;
  23. }
  24. };

问题:

INT32_MAX是什么?它表示数组中一个很大的数,进一步,它是在limits.h下面的一个宏。

https://blog.csdn.net/wangshuqian1314/article/details/122657716

卡哥讲解!

滑动窗口,就是不断的调节子序列的起始位置和终止位置,从而得出我们要想的结果。

滑动窗口的精妙之处在于根据当前子序列和大小的情况,不断调节子序列的起始位置。从而将O(n^2)暴力解法降为O(n)。

时间复杂度O(n),空间复杂度O(1)

  1. class Solution {
  2. public:
  3. int minSubArrayLen(int s, vector<int>& nums) {
  4. int result = INT32_MAX;
  5. int sum = 0; // 滑动窗口数值之和
  6. int i = 0; // 滑动窗口起始位置
  7. int subLength = 0; // 滑动窗口的长度
  8. for (int j = 0; j < nums.size(); j++) {
  9. sum += nums[j];
  10. // 注意这里使用while,每次更新 i(起始位置),并不断比较子序列是否符合条件
  11. while (sum >= s) {
  12. subLength = (j - i + 1); // 取子序列的长度
  13. result = result < subLength ? result : subLength;
  14. sum -= nums[i++]; // 这里体现出滑动窗口的精髓之处,不断变更i(子序列的起始位置)
  15. }
  16. }
  17. // 如果result没有被赋值的话,就返回0,说明没有符合条件的子序列
  18. return result == INT32_MAX ? 0 : result;
  19. }
  20. };

四、LeetCode59-螺旋矩阵II

这个题以前是做过的,先用学会的模拟实现一下

我的代码(数组模拟)
  1. class Solution {
  2. public:
  3. // 这题之前也写过,但还是不太熟练呐
  4. vector<vector<int>> generateMatrix(int n) {
  5. vector<vector<int>> res(n, vector<int>(n)); // 初始化二维数组
  6. // 定义上下左右四个方向的索引
  7. int t = 0; // top
  8. int b = n - 1; // bottom
  9. int l = 0; // left
  10. int r = n - 1; // right
  11. // 螺旋矩阵的计数(即填入矩阵的数值)
  12. int k = 1;
  13. while (k <= n * n){
  14. for(int i = l; i <= r; i++, k++){
  15. res[t][i] = k;
  16. }
  17. t++;
  18. for(int i = t; i <= b; i++, k++){
  19. res[i][r] = k;
  20. }
  21. r--;
  22. for(int i = r; i >= l; i--, k++){
  23. res[b][i] = k;
  24. }
  25. b--;
  26. for(int i = b; i >= t; i--, k++){
  27. res[i][l] = k;
  28. }
  29. l++;
  30. }
  31. return res;
  32. }
  33. };
卡哥讲解!

求解本题依然是要坚持循环不变量原则【处理的原则:统一的左闭右开】

  1. class Solution {
  2. public:
  3. vector<vector<int>> generateMatrix(int n) {
  4. vector<vector<int>> res(n, vector<int>(n, 0)); // 使用vector定义一个二维数组
  5. int startx = 0, starty = 0; // 定义每循环一个圈的起始位置
  6. int loop = n / 2; // 每个圈循环几次,例如n为奇数3,那么loop = 1 只是循环一圈,矩阵中间的值需要单独处理
  7. int mid = n / 2; // 矩阵中间的位置,例如:n为3, 中间的位置就是(1,1),n为5,中间位置为(2, 2)
  8. int count = 1; // 用来给矩阵中每一个空格赋值
  9. int offset = 1; // 需要控制每一条边遍历的长度,每次循环右边界收缩一位
  10. int i,j;
  11. while (loop --) {
  12. i = startx;
  13. j = starty;
  14. // 下面开始的四个for就是模拟转了一圈
  15. // 模拟填充上行从左到右(左闭右开)
  16. for (j = starty; j < n - offset; j++) {
  17. res[startx][j] = count++;
  18. }
  19. // 模拟填充右列从上到下(左闭右开)
  20. for (i = startx; i < n - offset; i++) {
  21. res[i][j] = count++;
  22. }
  23. // 模拟填充下行从右到左(左闭右开)
  24. for (; j > starty; j--) {
  25. res[i][j] = count++;
  26. }
  27. // 模拟填充左列从下到上(左闭右开)
  28. for (; i > startx; i--) {
  29. res[i][j] = count++;
  30. }
  31. // 第二圈开始的时候,起始位置要各自加1, 例如:第一圈起始位置是(0, 0),第二圈起始位置是(1, 1)
  32. startx++;
  33. starty++;
  34. // offset 控制每一圈里每一条边遍历的长度
  35. offset += 1;
  36. }
  37. // 如果n为奇数的话,需要单独给矩阵最中间的位置赋值
  38. if (n % 2) {
  39. res[mid][mid] = count;
  40. }
  41. return res;
  42. }
  43. };

Day02总结:

  1. 最大的收获是学习了滑动窗口,写代码的熟练度还需要加强

  1. 数组模拟注意边界问题,主要是想明白怎么轮转的,将清楚的逻辑转化为代码实现

  1. 双指针还需要多多练习,多看题目和多思考~

五、数组专题总结

posted @ 2023-01-12 22:52  ZYM-爱吃柚子的程序媛  阅读(2182)  评论(0)    收藏  举报