LeetCode-Solutions-in-Cpp17

Solutions to high-frequency interview questions of LeetCode in C++17, taking into account both efficiency and comprehensibility.


Project maintained by downdemo Hosted on GitHub Pages — Theme by mattgraham
$$$
$$b
$a@

到达 @ 之前,必然先到达 a  b
因此到达 @ 的路径数 = 到达 a 的路径数 + 到达 b 的路径数
@@@    111    111    111
@$$ => 1$$ => 123 => 123
@$$    1$$    1$$    136

结果为6
class Solution {
 public:
  int uniquePaths(int m, int n) {
    vector<vector<int>> dp(m, vector<int>(n));
    for (int i = 0; i < n; ++i) {
      dp[0][i] = 1;
    }
    for (int i = 0; i < m; ++i) {
      dp[i][0] = 1;
    }
    for (int i = 1; i < m; ++i) {
      for (int j = 1; j < n; ++j) {
        dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
      }
    }
    return dp[m - 1][n - 1];
  }
};