LintCode_114 不同的路徑

來源:互聯網
上載者:User

標籤:

題目

有一個機器人的位於一個M×N個網格左上方(中標記為‘Start‘)。

機器人每一時刻只能向下或者向右移動一步。機器人試圖達到網格的右下角(中標記為‘Finish‘)。問有多少條不同的路徑?

 

 注意事項

n和m均不超過100

範例

1,1 1,2 1,3 1,4 1,5 1,6 1,7
2,1            
3,1           3,7

以上3 x 7的網格中,有多少條不同的路徑?

思路

dp的思路來解決

dp[0][j] = 1;

dp[i][0] = 1;

dp[i][j] = dp[i-1][j] + dp[i][j-1];

空間複雜度可以降低到O(n)

dp[i] = dp[i] + dp[i-1];

C++代碼

 1 int uniquePaths(int m, int n) { 2         // wirte your code here 3         vector<int> dp(n,1); 4         for(int i = 1; i < m; ++i) 5         { 6             for(int j = 1; j < n; ++j) 7             { 8                 dp[j] += dp[j - 1]; 9             }10         }11         return dp[n - 1];12     }
View Code

 

LintCode_114 不同的路徑

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.