首页 > 技术文章 > LeetCode 62

ZJPaang 2020-04-17 21:57 原文

https://leetcode-cn.com/problems/unique-paths/

说到这个题我就是真的憨批

一开始用了回溯法,发现T了

点开标签 写着dp,才发现自己想法完全是错了。

这个dp很容易,我是倒过来想的,解答区有正着做的,他们的要简洁一点。

最牛逼的还是排列组合数!!!!!

class Solution {
    public int uniquePaths(int m, int n) {
        int[][] result = new int[n][m];
        result[n-1][m-1] = 1;
        for(int i = n-1; i >=0;i--){
            for(int j = m-1; j >=0;j--){
                if(i == n-1 && j == m-1){
                    continue;
                }
                if(i+1>=n){
                    result[i][j] += result[i][j+1];
                }
                if(j+1>=m){
                    result[i][j] += result[i+1][j];
                }
                if(i+1<n && j+1<m){
                    result[i][j] = result[i][j] + result[i][j+1] + result[i+1][j];
                }
            }
        }
        return result[0][0];
    }
}

 

推荐阅读