首页 > 代码库 > [LeetCode]62 Unique Paths
[LeetCode]62 Unique Paths
https://oj.leetcode.com/problems/unique-paths/
http://blog.csdn.net/linhuanmars/article/details/22126357
public class Solution { public int uniquePaths(int m, int n) { if (m < 0 || n < 0) return -1; // Invalid input // Start 0, 0 // End m, n // Use a matrix[m][n]. // Each element is all possible unique paths to that point. int[][] paths = new int [m][n]; for (int i = 0 ; i < m ; i ++) { for (int j = 0 ; j < n ; j ++) { if (i == 0 && j == 0) { paths[i][j] = 1; // Only one path to start point. } else { int fromleft = j > 0 ? paths[i][j - 1] : 0; int fromup = i > 0 ? paths[i - 1][j] : 0; paths[i][j] = fromleft + fromup; } } } return paths[m - 1][n - 1]; } }
[LeetCode]62 Unique Paths
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。