leetcode62-Unique Paths

2024-06-04 07:36
文章标签 paths unique leetcode62

本文主要是介绍leetcode62-Unique Paths,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目

一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为 “Start” )。
机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为 “Finish” )。
问总共有多少条不同的路径?
示例 1:
输入:m = 3, n = 7
输出:28

分析

每次只能向下或者向右,我们可以用dp[i][j]表示走到当前的路径,那么dp公式就可以为dp[i][j]=dp[i-1][j]+dp[i][j-1] ,最后注意起始条件,即第一行和第一列的路径数只能是1

public class uniquePaths {public static void main(String[] args) {System.out.println(getUniquePath(3,7));}public static int getUniquePath(int m,int n) {int[][] dp = new int[m][n];for(int i = 0;i<m;i++) {dp[i][0] = 1;}for(int i = 0;i<n;i++) {dp[0][i] = 1;}for(int i = 1;i<m;i++) {for(int j = 1;j<n;j++) {dp[i][j] = dp[i-1][j]+dp[i][j-1];}}return dp[m-1][n-1];}
}

这篇关于leetcode62-Unique Paths的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1029457

相关文章

[leetcode] 257. Binary Tree Paths

* Binary Tree Paths* 描述 Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / \ 2 3 \ 5 All root-to-leaf paths are: [“1->2->5”, “1->3”] 我的代码

动态规划02(Leetcode62、63、343、96)

参考资料: https://programmercarl.com/0062.%E4%B8%8D%E5%90%8C%E8%B7%AF%E5%BE%84.html 62. 不同路径 题目描述: 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为 “Start” )。 机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为 “Finish” )。

C++ unique_ptr

深刻理解一个原理的方法之一就是去自我实现一个,上代码: 1 自定义unique_ptr #include <iostream>#include <utility>template<typename T>class unique_ptr {private:T * ptr_resource = nullptr;public:explicit unique_ptr(T* raw_resourc

weka打开csv提示attribute names are not unique! Cause:

谢邀,人在实验室,没有中文补丁我要死了,希望weka没事 如题,初学weka,自己设了个CSV,想要试试weka的转换格式功能,没想到出现了提示attribute names are not unique! Cause:‘’ 这里的 Causes:‘85’,意思是自动识别首行的列名时,识别到了纯数字(数据如下图,首行有个 “85”),而纯数字不能作为列名* 加上列名后再导入就可以了

Binary Tree Paths问题及解法

问题分析: Given a binary tree, return all root-to-leaf paths. 示例: given the following binary tree: 1/ \2 3\5 All root-to-leaf paths are: ["1->2->5", "1->3"] 问题分析: 这里涉及到将整数转换为字符串的问题,还好新

Unique Binary Search Trees II问题及解法

问题描述: Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1...n. 示例: Given n = 3, your program should return all 5 unique BST's shown below. 1

Unique Binary Search Trees问题及解法

问题描述: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? 示例: Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1\

numpy.unique()函数

该函数的调用方法: numpy.unique(ar, return_index=False, return_inverse=False, return_counts=False, axis=None) 该函数作用:找出数组中独一无二的元素值。 各个参数意义: ar:输入数组,除非设定了下面介绍的axis参数,否则输入数组均会被自动扁平化成一个一维数组。 return_index:(可选参数

LeetCode contest 193 5437. 不同整数的最少数目 Least Number of Unique Integers after K Removals

Table of Contents 一、中文版 二、英文版 三、My answer 四、解题报告 一、中文版 给你一个整数数组 arr 和一个整数 k 。现需要从数组中恰好移除 k 个元素,请找出移除后数组中不同整数的最少数目。 示例 1: 输入:arr = [5,5,4], k = 1输出:1解释:移除 1 个 4 ,数组中只剩下 5 一种整数。 示例 2: 输入:

IO高级 -- 文件操作(Path、Paths、Files)

一、基础:File 1.1 构造方法: 1、 public File(String pathname) :通过给定的路径来创建新的 File实例。2、 public File(String parent, String child) :从父路径(字符串)和子路径创建新的 File实例。3、 public File(File parent, String child) :从父路径(File)和