代码随想录算法训练营第二天 | 977. 有序数组的平方、209. 长度最小的子数组、904. 水果成篮、76. 最小覆盖子串、59. 螺旋矩阵 II、54. 螺旋矩阵

代码随想录算法训练营第二天 | 977. 有序数组的平方、209. 长度最小的子数组、904. 水果成篮、76. 最小覆盖子串、59. 螺旋矩阵 II、54. 螺旋矩阵

    • 977. 有序数组的平方
    • 209. 长度最小的子数组
    • 904. 水果成篮
    • 76. 最小覆盖子串
    • 59. 螺旋矩阵 II
    • 54. 螺旋矩阵

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

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

209. 长度最小的子数组

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

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

904. 水果成篮

题目链接:https://leetcode.cn/problems/fruit-into-baskets/

class Solution {
public:
    int totalFruit(vector<int>& fruits) {
        int last;
        int ans = 0;
        int i = 0, j;
        unordered_set<int> type; // 篮子水果种类
        for (j = 0; j < fruits.size(); j++) {
            if (type.size() == 2 && type.find(fruits[j]) == type.end()) { // 第三种水果出现
                i = j - 1;
                last = fruits[i--];
                while (fruits[i] == last) // 查找左窗口边界
                    i--;
                type.erase(fruits[i++]);
            }
            type.insert(fruits[j]);
            ans = max(j - i + 1, ans);
        }
        return ans;
    }
};

扩展:map 可以记录种类和个数

76. 最小覆盖子串

题目链接:https://leetcode.cn/problems/minimum-window-substring/

class Solution {
public:
    string minWindow(string s, string t) {
        int i = 0, correct = 0;
        string res = s + "max";
        unordered_map<char, int> smap, tmap;
        for (auto c : t) 
            tmap[c]++;
        for (int j = 0; j < s.size(); j++) {
            smap[s[j]]++; // 记录字符数
            if (tmap[s[j]] >= smap[s[j]]) // 记录有效字符数
                correct++;
            while (i < j && tmap[s[i]] < smap[s[i]]) // 删除左窗口冗余字符
                smap[s[i++]]--;
            if (correct == t.size() && j - i + 1 < res.size())
                res = s.substr(i, j - i + 1); 
        }
        return res == s + "max" ? "" : res;
    }
};

关键在于如何记录有效字符串和删除冗余字符

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

class Solution {
public:
    vector<vector<int>> generateMatrix(int n) {
        vector<vector<int>> matrix(n, vector<int>(n, 0));
        int i, j;
        int startx = 0, starty = 0;
        int loop = n / 2;
        int count = 1;
        int offset = 1;
        while (loop--) {
            i = startx, j = starty;
            while (j < n - offset) 
                matrix[i][j++] = count++;
            while (i < n - offset) 
                matrix[i++][j] = count++;
            while (j > starty) 
                matrix[i][j--] = count++;
            while (i > startx) 
                matrix[i--][j] = count++;
            startx++, starty++;
            offset++;
        }
        if (n % 2)
            matrix[startx][starty] = count;
        return matrix;
    }
};

关键在于找规律,坚持循环不变量原则

54. 螺旋矩阵

题目链接:https://leetcode.cn/problems/spiral-matrix

class Solution {
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix) {
        int count = 0;
        int row = matrix.size(), col = matrix[0].size();
        int startx = 0, starty = 0;
        int i, j;
        int loop = min(row / 2, col / 2);
        int offset = 1;
        vector<int> ans(row * col, 0);
        while (loop--) {
            i = startx, j = starty;
            while (j < col - offset)
                ans[count++] = matrix[i][j++];
            while (i < row - offset)
                ans[count++] = matrix[i++][j];
            while (j > starty)
                ans[count++] = matrix[i][j--];
            while (i > startx)
                ans[count++] = matrix[i--][j];
            startx++, starty++;
            offset++;
        }
        if (row <= col) {
            if (row % 2) {
                for (i = startx, j = starty; j <= col - offset; j++)
                    ans[count++] = matrix[i][j];
            }
        } else {
            if (col % 2) {
                for (i = startx, j = starty; i <= row - offset; i++)
                    ans[count++] = matrix[i][j];
            }
        }
        return ans;
    }
};

你可能感兴趣的:(算法,矩阵,线性代数)