模拟

59. 螺旋矩阵 II

https://leetcode.cn/problems/spiral-matrix-ii/

给你一个正整数 n ,生成一个包含 1n2 所有元素,且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix

模拟

class Solution {
public:
    vector<vector<int>> generateMatrix(int n) {
        vector<vector<int>> res(n, vector<int>(n, 0));
        int sx = 0, sy = 0, offset = 1;
        int loop = n / 2, count = 1;
        while(loop --){
            int i = sx, j = sy;

            for(; j < n - offset; j++)
                res[i][j] = count++;
            for(; i < n - offset; i++)
                res[i][j] = count++;
            for(; j > sx; j--)
                res[i][j] = count++;
            for(; i > sy; i--)
                res[i][j] = count++;

            sx++, sy++, offset++;
        }
        if(n % 2 != 0) res[n / 2][n / 2] = count;
        return res;
    }
};

模拟即可,注意每次要更新 x, y 的起点与边界值 offset 。

偏移量

模拟狗都不写。

54. 螺旋矩阵

https://leetcode.cn/problems/spiral-matrix/

给你一个 mn 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素。

使用偏移量来做,模拟?狗都不写!

415. 字符串相加

https://leetcode.cn/problems/add-strings/

给定两个字符串形式的非负整数 num1 和 num2 ,计算它们的和并同样以字符串形式返回。

你不能使用任何內建的用于处理大整数的库(比如 BigInteger), 也不能直接将输入的字符串转换为整数形式。

最后更新于