记录2022.8.19的4399笔试编程题

532 阅读1分钟

1. LeetCode 633. 平方数之和

class Solution {
public:
    bool judgeSquareSum(int c) {
        int m = sqrt(c);
        for (long a = 0; a <= m; ++a)
        {
            long b = round(sqrt(c - a*a));
            if (a*a + b*b == c) return true;
        }
        return false;
    }
};

要点

  1. sqrt()返回的是浮点数,为了避免浮点数精度问题,使用round()取整(四舍五入)。

  2. a和b为long型,因为int*int相乘结果显示溢出(round()导致的)。

2. LeetCode 200. 岛屿数量

题解:dsf

class Solution {
public:
    int numIslands(vector<vector<char>>& grid) {
        if (grid.empty()) return 0;
        int m = grid.size();
        int n = grid[0].size();
        int ans = 0;
        for (int y = 0; y < m; ++y)
        {
            for (int x = 0; x < n; ++x)
            {
                ans += grid[y][x] - '0';
                dfs(grid, x, y, m, n);
            }
        }
        return ans;
    }
private:
    void dfs(vector<vector<char>>& grid, int x, int y, int m, int n)
    {
        if (x < 0 || y < 0 || x >= n || y >= m || grid[y][x] == '0')
            return;
        grid[y][x] = '0';
        dfs(grid, x + 1, y, m, n);
        dfs(grid, x - 1, y, m, n);
        dfs(grid, x, y + 1, m, n);
        dfs(grid, x, y - 1, m, n);
    }
};

3. LeetCode 85. 最大矩形

题解:栈,结合LeetCode 84. 柱状图中最大的矩形

class Solution {
public:
    int maximalRectangle(vector<vector<char>>& matrix) {
        if (matrix.size() == 0) return 0;
        int row = matrix.size();
        int col = matrix[0].size();
        vector<int> heights(col, 0);
        int maxx = 0;
        for (int i = 0; i < row; ++i)
        {
            for (int j = 0; j < col; ++j)
            {
                if (matrix[i][j] == '1')
                {
                    ++heights[j];
                }
                else
                {
                    heights[j] = 0;
                }
            }
            int area = largestRectangleArea(heights);
            maxx = max(maxx, area);
        }
        return maxx;
    }
private:
    int largestRectangleArea(vector<int>& heights) {
        if (heights.size() == 0) return 0;
        int maxx = 0;
        stack<int> st;
        for (int curr = 0; curr < heights.size(); ++curr)
        {
            if (st.empty() || heights[curr] >= heights[st.top()])
            {
                st.push(curr);
            }
            else
            {
                int right = curr;
                int idx = st.top();
                st.pop();
                while (!st.empty() && heights[idx] == heights[st.top()])
                {
                    idx = st.top();
                    st.pop();
                }
                int leftMost = st.empty() ? -1 : st.top();
                maxx = max(maxx, (right - leftMost - 1)*heights[idx]);
                --curr;
            }
        }
        int rightMost = st.top() + 1;
        while (!st.empty())
        {
            int idx = st.top();
            st.pop();
            int left = st.empty() ? -1 : st.top();
            maxx = max(maxx, (rightMost - left - 1)*heights[idx]);
        }
        return maxx;
    }
};

和第3题类似的还有LeetCode 221. 最大正方形

题解:动态规划

class Solution {
public:
    int maximalSquare(vector<vector<char>>& matrix) {
        if (matrix.size() == 0) return 0;
        int row = matrix.size();
        int col = matrix[0].size();
        int res = 0;

        vector<vector<int>> dp(row + 1, vector<int>(col + 1, 0));

        for (int i = 1; i <= row; i++)
        {
            for (int j = 1; j <= col; j++)
            {
                if (matrix[i - 1][j - 1] == '1')
                {
                    dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j - 1]), dp[i - 1][j]) + 1;
                    res = max(res, dp[i][j]);
                }
            }
        }
        return res * res;
    }
};

要点: dp数组要多出一维用来处理‘1’只在矩阵下边界和右边界的特殊情况(此时返回1)。