「程式碼隨想錄演算法訓練營」第二天 | 陣列 part2

云雀AC了一整天發表於2024-07-04

977.有序陣列的平方

題目建議: 本題關鍵在於理解雙指標思想
題目連結:https://leetcode.cn/problems/squares-of-a-sorted-array/
題目難度:簡單
文章講解:https://programmercarl.com/0977.有序陣列的平方.html
影片講解: https://www.bilibili.com/video/BV1QB4y1D7ep
題目狀態:透過

個人思路:

迴圈平方後使用 sort 函式,具體程式碼如下:

class Solution {
public:
    vector<int> sortedSquares(vector<int>& nums) {
        int n = nums.size();
        vector<int> res(n);
        for(int i = 0; i < n; ++i)
        {
            res[i] = nums[i] * nums[i];
        }
        sort(res.begin(), res.end());
        return res;
    }
};

卡哥的思路(雙指標):

根據卡哥的思路寫的關於雙指標的程式碼:

```cpp
class Solution {
public:
    vector<int> sortedSquares(vector<int>& nums) {
        int n = nums.size();
        vector<int> res(n);
        int i = 0, j = n - 1, k = n - 1;
        while(i <= j)
        {
            if(nums[i] * nums[i] > nums[j] * nums[j])
            {
                res[k] = nums[i] * nums[i];
                i++;
            }
            else
            {
                res[k] = nums[j] * nums[j];
                j--;
            }
            k--;
        }
        return res;
    }
};

209.長度最小的子陣列

題目建議: 滑動視窗
題目連結:https://leetcode.cn/problems/minimum-size-subarray-sum/
題目難度:中等
文章講解:https://programmercarl.com/0209.長度最小的子陣列.html
影片講解: https://www.bilibili.com/video/BV1tZ4y1q7XE
題目狀態:有思路但出錯,經ChatGPT修正後透過

個人思路:

建立兩個指向陣列頭部的指標leftright,判斷在這兩個指標中間的子陣列之和是否大於target,並建立一個變數res = INT_MAX,用於儲存子陣列大於等於target的最小個數。

  • 若大於等於,res = min(res, right - left + 1),並且left++,重新計運算元陣列之和;
  • 若小於,right++,並重新計運算元陣列之和

程式碼如下:

class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        int res = INT_MAX;
        int left = 0;
        int sum = 0;
        for(int right = 0; right < nums.size(); ++right)
        {
            sum += nums[right];
            while(sum >= target)
            {
                res = min(res, right - left + 1);
                sum -= nums[left];
                left++;
            }
        }
        return res == INT_MAX ? 0 : res;
    }
};

59.螺旋矩陣II

題目建議: 本題關鍵還是在轉圈的邏輯,在二分搜尋中提到的區間定義,在這裡又用上了。
題目連結:https://leetcode.cn/problems/spiral-matrix-ii/
題目難度:中等
文章講解:https://programmercarl.com/0059.螺旋矩陣II.html
影片講解:https://www.bilibili.com/video/BV1SL4y1N7mV/
題目狀態:沒思路,直接看卡哥思路

程式碼如下:

class Solution {
public:
    vector<vector<int>> generateMatrix(int n) {
        vector<vector<int>> res(n, vector<int>(n, 0));
        int startX = 0, startY = 0;
        int offset = 1;
        int i, j;
        int mid = n/2;
        int loop = n/2;
        int count = 1;

        while(loop--)
        {
            i = startX;
            j = startY;
            for(j; j < n - offset; ++j)
            {
                res[i][j] = count++;
            }
            for(i; i < n - offset; ++i)
            {
                res[i][j] = count++;
            }
            for(j; j > startY; --j)
            {
                res[i][j] = count++;
            }
            for(i; i > startX; --i)
            {
                res[i][j] = count++;
            }
            startX++;
            startY++;
            offset++;
        }
        if(n % 2)
        {
            res[mid][mid] = count;
        }
        return res;
    }
};

思路:

主要是判斷好遍歷在什麼時候拐彎。

  1. 計算螺旋矩陣的圈數loop;計算矩陣的中心位置count(用於奇偶);定義每圈在哪個位置拐彎(偏移量offset);定義每圈遍歷的開始位置(水平從startY開始遍歷,垂直從startX開始遍歷);
  2. 明確什麼時候開始拐彎(左閉右開);
  3. 每遍歷一圈,下圈的開始位置(startXstartY)加1,每圈的偏移量加1。

相關文章