Saturday, November 14, 2015

[leetcode] Unique Paths

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
public class Solution {
    public int uniquePaths(int m, int n) {
        if (m <= 0 || n <= 0) return 0;
        
        int rollingWays[] = new int[n];
        for (int i = 0; i < n; i++){
            rollingWays[i] = 1;
        }
        
        for (int i = m-2; i >= 0; i--){
            for (int j = n-2; j >= 0; j--){
                rollingWays[j] = rollingWays[j]+rollingWays[j+1];
            }
        }
        
        return rollingWays[0];
    }
}

No comments:

Post a Comment