LeetCode062——不同路径

我的LeetCode代码仓:https://github.com/617076674/LeetCode

原题链接:https://leetcode-cn.com/problems/unique-paths/description/

题目描述:

知识点:动态规划

思路:动态规划

状态定义:f(x, y) ---------- 到达坐标(x, y)的路径数

状态转移

(1)如果x == 0或者y == 0,f(x, y) = 1。

(2)否则,f(x, y) = f(x - 1, y) + f(x, y - 1)。

时间复杂度和空间复杂度均是O(m * n)。

JAVA代码:

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

LeetCode解题报告:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值