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

66 阅读1分钟

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

977.有序数组的平方

题目链接:977.有序数组的平方

  • 因为是有序数组,可以考虑一个头指针和尾指针,头指针往后遍历,尾指针往前
  • 这样可以把问题转化为合并两个有序数组/链表
  • 时间复杂度O(n)
  • 空间复杂度O(1)

209.长度最小的子数组

题目链接:209.长度最小的子数组

  • 暴力法重复过多,时间复杂度O(N^2)
  • 在子集合中所有元素和大于等于target的时候进行while循环(不是if!!!),循环中动态调整起始位置
  • 滑动窗口的模板
  •  /* 滑动窗口算法框架 */
     void slidingWindow(string s) {
         unordered_map<char, int> window;
         
         int left = 0, right = 0;
         while (right < s.size()) {
             // c 是将移入窗口的字符
             char c = s[right];
             // 增大窗口
             right++;
             // 进行窗口内数据的一系列更新
             ...
     ​
             /*** debug 输出的位置 ***/
             // 注意在最终的解法代码中不要 print
             // 因为 IO 操作很耗时,可能导致超时
             printf("window: [%d, %d)\n", left, right);
             /********************/
             
             // 判断左侧窗口是否要收缩
             while (window needs shrink) {
                 // d 是将移出窗口的字符
                 char d = s[left];
                 // 缩小窗口
                 left++;
                 // 进行窗口内数据的一系列更新
                 ...
             }
         }
     }
    

59.螺旋矩阵II

题目链接:59.螺旋矩阵II

  • 分享一个比较直观的代码
  • 不用考虑corner case
 class Solution {
     public int[][] generateMatrix(int n) {
         int left = 0, right = n - 1;
         int top = 0, bottom = n - 1;
         int counter = 1;
         int [][] maxtrix = new int[n][n];
         while(counter !=  n * n + 1 ){
             for(int i = left; i <= right; i++){
                 maxtrix[top][i] = counter++;
             }
             top++;
             for(int i = top; i <= bottom; i++){
                 maxtrix[i][right] = counter++;
             }
             right--;
             for(int i = right; i >= left; i--){
                 maxtrix[bottom][i] = counter++;
             }
             bottom--;
             for(int i = bottom; i >= top; i--){
                 maxtrix[i][left] = counter++;
             }
             left++;
         }
         return maxtrix;
     }
 }
 class Solution {
 public:
     vector<vector<int>> generateMatrix(int n) {
         int left = 0;
         int right = n;
         int bottom = n;
         int top = 0;
         vector<vector<int>> matrix(n, vector<int>(n));
         int count = 0;
         while(count != n * n) {
             
             for(int col = left; col < right; col++) {
                 matrix[top][col] = ++count;
             }
             top++;
 ​
             for(int row = top; row < bottom; ++row) {
                 matrix[row][right - 1] = ++count;
             }
             right--;
 ​
             for(int col = right - 1; col >=left; col--) {
                 matrix[bottom - 1][col] = ++count;
             }
             bottom--;
 ​
 ​
             for(int row = bottom - 1; row >= top; --row) {
                 matrix[row][left] = ++count;
             }
             left++;
         }
 ​
 ​
         return matrix;
     }
 };