LintCode brush questions - different paths

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/kingslave1/article/details/78073943

description:

A robot located in a  m  ×  n-  th upper left corner of the grid.

Each time the robot can only move one step down or to the right. Robot trying to reach the bottom right corner of the grid.

Ask how many different paths there?

Example:

Gives m =  3 and = n-  3, returns  6.
Given m =  4 and = n-  5, return  35.


Problem solving:

public class Solution {
    /**
     * @param n, m: positive integer (1 <= n ,m <= 100)
     * @return an integer
     */
    public int uniquePaths(int m, int n) {
        if(m==0||n==0) {
        	return 1;
        }
		int [][]dp=new int[m][n];
        dp[0][0]=1;
        for(int i=1;i<m;i++) {
        	dp[i][0]=1;
        }
        for(int j=1;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];
    }
}


Guess you like

Origin blog.csdn.net/kingslave1/article/details/78073943