标签:leetcode
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
原文地址:http://7371901.blog.51cto.com/7361901/1598846