Leetcode动态规划 不同路径
生活随笔
收集整理的這篇文章主要介紹了
Leetcode动态规划 不同路径
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the diagram below).
How many possible unique paths are there?
狀態轉移方程
dp[i][j] = dp[i-1][j] + dp[i][j-1]
邊界條件
dp[i][0] = 1
dp[0][j] = 1
代碼
class Solution { public:int uniquePaths(int m, int n) {int dp[105][105] = {0};for(int i = 0; i < m; i++)dp[i][0] = 1;for(int j = 0; j < n; j++)dp[0][j] = 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];} }; 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的Leetcode动态规划 不同路径的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Leetcode贪心 验证回文字符串
- 下一篇: 余秋雨简介及作品介绍 余秋雨的作品有哪些