zoukankan      html  css  js  c++  java
  • Leetcode 62. Unique Paths

    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?

    Above is a 3 x 7 grid. How many possible unique paths are there?

    Note: m and n will be at most 100.

           思路:这是一道典型的动态规划问题,使用一个二维数组dp记忆到达每一点可行的走法总数。首先将左边界点和上边界点初始化为1,因为机器人起始于(1,1),左边界点和上边界点的走法只有1种。接下来的每一点(x,y),可以由(x-1,y)向下走或是(x,y-1)向右走来到达,因此在(x,y)这一点可到达的方法有dp[x-1][y]+dp[x][y-1]种,到达终点的方法则是dp最后一个点的数据。

     1 class Solution {
     2 public:
     3     int uniquePaths(int m, int n) {
     4         if(m == 0 || n == 0){
     5             return 0;
     6         }
     7             
     8         vector<vector<int>> dp(m+1, vector<int>(n+1,0));
     9         int i, j;
    10         //int dp[101][101], i, j;
    11         for(i = 1; i <= m; i++){
    12             dp[i][1] = 1;
    13         }
    14             
    15         for(j = 1; j <= n; j++){
    16             dp[1][j] = 1; 
    17         }
    18            
    19         for(i = 2; i <= m; i++){
    20             for(j = 2; j <= n; j++){
    21                 dp[i][j] = dp[i-1][j] + dp[i][j-1];
    22             }
    23         }
    24         return dp[m][n];
    25     }
    26 };
  • 相关阅读:
    Charpter5 软件测试总结
    Charpter3 关于闰年测试
    Charpter2 新的测试用例
    Charpter1 等价类划分方法分析与应用
    软件测试第一周学习总结
    关于软件测试学习的心得
    软件测试-同行评审
    白盒测试
    黑盒测试
    Java实现的闰年测试程序
  • 原文地址:https://www.cnblogs.com/qinduanyinghua/p/5733092.html
Copyright © 2011-2022 走看看