solution.md 4.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
# 不同路径
<p>一个机器人位于一个 <code>m x n</code><em> </em>网格的左上角 (起始点在下图中标记为 “Start” )。</p>
<p>机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为 “Finish” )。</p>
<p>问总共有多少条不同的路径?</p>
<p> </p>
<p><strong>示例 1:</strong></p><img
    src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0062.Unique%20Paths/images/robot_maze.png" />
<pre><strong>输入:</strong>m = 3, n = 7<strong><br />输出:</strong>28</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入:</strong>m = 3, n = 2<strong><br />输出:</strong>3<strong><br />解释:</strong>从左上角开始,总共有 3 条路径可以到达右下角。<br />1. 向右 -> 向下 -> 向下<br />2. 向下 -> 向下 -> 向右<br />3. 向下 -> 向右 -> 向下</pre>
<p><strong>示例 3:</strong></p>
<pre><strong>输入:</strong>m = 7, n = 3<strong><br />输出:</strong>28</pre>
<p><strong>示例 4:</strong></p>
<pre><strong>输入:</strong>m = 3, n = 3<strong><br />输出:</strong>6</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
    <li><code>1 <= m, n <= 100</code></li>
    <li>题目数据保证答案小于等于 <code>2 * 10<sup>9</sup></code></li>
</ul>
<p>以下错误的选项是?</p>
F
fix bug  
feilong 已提交
22

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
    Solution sol;
    int m = 3;
    int n = 7;
    int res;

    res = sol.uniquePaths(m, n);
    cout << res;
    return 0;
}
```

## 答案
```cpp

class Solution
{
public:
    int uniquePaths(int m, int n)
    {
        vector<vector<int>> path(m, vector<int>(n, 0));
        for (int i = 0; i < n; i++)
            path[0][i] = 1;
        for (int i = 0; i < m; i++)
            path[i][0] = 1;
        for (int i = 1; i < n; i++)
            for (int j = 1; j < m; j++)
                path[j][i] = path[j - 1][i + 1] + path[j + 1][i - 1];
        return path[m - 1][n - 1];
    }
};
```
## 选项

### A
```cpp
typedef vector<int> BigInt;
class Solution
{
public:
    int uniquePaths(int m, int n)
    {
        if (m == 0 || n == 0)
            return 0;
        if (m == 1 || n == 1)
            return 1;
        int m_ = m - 1 + n - 1;
        int n_ = n - 1;
        BigInt a = fac(m_);
        int result = 0;
        for (int i = n_; i >= 1; i--)
            a = div(a, i);
        for (int i = m_ - n_; i >= 1; i--)
            a = div(a, i);
        int k = a.size() - 1;
        while (a[k] == 0)
            k--;
        for (int i = k; i >= 0; i--)
            result = result * 10 + a[i];

        return result;
    }

    BigInt fac(int n)
    {
        BigInt result;
        result.push_back(1);
        for (int factor = 1; factor <= n; ++factor)
        {
            long long carry = 0;
            for (auto &item : result)
            {
                long long product = item * factor + carry;
                item = product % 10;
                carry = product / 10;
            }
            if (carry > 0)
            {
                while (carry > 0)
                {
                    result.push_back(carry % 10);
                    carry /= 10;
                }
            }
        }
        return result;
    }
    BigInt div(BigInt a, int d)
    {
        int b = 0;
        BigInt result;
        int len = a.size();
        for (int i = len - 1; i >= 0; i--)
        {
            b = b * 10 + a[i];
            result.insert(result.begin(), b / d);
            b = b % d;
        }
        return result;
    }
};
```

### B
```cpp
class Solution
{
public:
    int uniquePaths(int m, int n)
    {
        if (m <= 0 || n <= 0)
        {
            return 0;
        }

        vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
        for (int i = 0; i < m; i++)
        {
            dp[i][0] = 1;
        }
        for (int i = 0; i < n; i++)
        {
            dp[0][i] = 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];
    }
};
```

### C
```cpp
class Solution
{
public:
    int uniquePaths(int m, int n)
    {
        int N = m + n - 2;
        int M = m < n ? m - 1 : n - 1;

        long ans = 1;
        for (int i = 1; i <= M; i++)
            ans = ans * (N - i + 1) / i;
        return ans;
    }
};
```