LeetCode062 Unique Paths
来源:互联网 发布:淘宝开直播需要交钱吗 编辑:程序博客网 时间:2024/06/11 22:40
详细见:leetcode.com/problems/unique-paths
Java Solution: github
package leetcode;import java.util.Arrays;public class P062_UniquePaths {public static void main(String[] args) {System.out.println(new Solution().uniquePaths(3, 3));}/* * 先写一份空间O(m * n),时间O(m * n)的版本 * 1 ms * 5.22% */static class Solution { public int uniquePaths(int m, int n) { if (m < 1 || n < 1) return 0; int[][] route = new int[m][n]; Arrays.fill(route[0], 1); for (int i = 1; i < m; i ++) route[i][0] = 1; for (int i = 1; i < m; i ++) { for (int j = 1; j < n; j ++) { route[i][j] = route[i - 1][j] + route[i][ j - 1]; } } return route[m - 1][n - 1]; }}/* * 1 ms * 5.22% */static class Solution2 { public int uniquePaths(int m, int n) { if (m < 1 || n < 1) return 0; if (m > n) return uniquePaths(n, m); int[] route = new int[m]; Arrays.fill(route, 1); for (int i = 1; i < n; i ++) for (int j = 1; j < m; j ++) route[j] += route[j - 1]; return route[m - 1]; }}}
C Solution: github
/* url: leetcode.com/problems/unique-paths AC 3ms 2.33%*/int uniquePaths(int m, int n) { int* dp = (int*) malloc(sizeof(int) * n); int i = 0, j = 0; for (i = n-1; i > -1; i --) { dp[i] = 1; } for (j = m-2; j > -1; j --) { for (i = n-2; i > -1; i --) { dp[i] += dp[i+1]; } } i = dp[0]; free(dp); return i;}int main() {}
Python Solution: github
#coding=utf-8''' url: leetcode.com/problems/unique-paths @author: zxwtry @email: zxwtry@qq.com @date: 2017年4月13日 @details: Solution: 79ms 10.36%'''class Solution(object): def uniquePaths(self, m, n): """ :type m: int :type n: int :rtype: int """ dp = [1] * n for j in range(m-2, -1, -1): for i in range(n-2, -1, -1): dp[i] += dp[i+1] return dp[0]
0 0
- LeetCode062 Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Java 包(package)
- 4.learningTF
- 大数运算学习总结
- setsockopt()用法(参数详细说明)
- Tomcat安装和配置
- LeetCode062 Unique Paths
- 第五部分 高效并发
- WPF
- NYOJ1072我想回家_最短路径
- 类和面向对象习题
- LeetCode063 Unique Paths II
- C# EmguCV学习笔记(一)---图像加减法操作
- Tomcat简介
- bzoj 4817: [Sdoi2017]树点涂色 link cut tree+线段树+树链剖分