C#LeetCode刷题之#62-不同路径(Unique Paths)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3680 访问。
一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” )。
机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。
问总共有多少条不同的路径?
例如,上图是一个7 x 3 的网格。有多少可能的路径?
说明:m 和 n 的值均不超过 100。
输入: m = 3, n = 2
输出: 3
解释:
从左上角开始,总共有 3 条路径可以到达右下角。
- 向右 -> 向右 -> 向下
- 向右 -> 向下 -> 向右
- 向下 -> 向右 -> 向右
输入: m = 7, n = 3
输出: 28
A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the diagram below).
How many possible unique paths are there?
Above is a 7 x 3 grid. How many possible unique paths are there?
Note: m and n will be at most 100.
Input: m = 3, n = 2
Output: 3
Explanation:
From the top-left corner,there are a total of 3 ways to reach the bottom-right corner:
- Right -> Right -> Down
- Right -> Down -> Right
- Down -> Right -> Right
Input: m = 7, n = 3
Output: 28
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3680 访问。
public class Program {
public static void Main(string[] args) {
var m = 7;
var n = 3;
var res = UniquePaths(m, n);
Console.WriteLine(res);
Console.ReadKey();
}
private static int UniquePaths(int m, int n) {
if(m == 0 || n == 0) return 0;
var dp = new int[m, n];
for(var i = 0; i < m; i++) {
for(var j = 0; j < n; j++) {
dp[i, j] = 1;
}
}
for(var i = 1; i < m; i++) {
for(var j = 1; j < n; j++) {
dp[i, j] = dp[i, j - 1] + dp[i - 1, j];
}
}
return dp[m - 1, n - 1];
}
}
以上给出1种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3680 访问。
28
分析
显而易见, 以上算法的时间复杂度为:O(m∗n)O(m*n)O(m∗n) 。
C#LeetCode刷题之#62-不同路径(Unique Paths)的更多相关文章
- C#LeetCode刷题之#63-不同路径 II(Unique Paths II)
目录 问题 示例 分析 问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3682 访问. 一个机器人位于一个 m x ...
- C#LeetCode刷题-数组
数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...
- C#LeetCode刷题-动态规划
动态规划篇 # 题名 刷题 通过率 难度 5 最长回文子串 22.4% 中等 10 正则表达式匹配 18.8% 困难 32 最长有效括号 23.3% 困难 44 通配符匹配 17.7% ...
- C#LeetCode刷题-字符串
字符串篇 # 题名 刷题 通过率 难度 3 无重复字符的最长子串 24.6% 中等 5 最长回文子串 22.4% 中等 6 Z字形变换 35.8% 中等 8 字符串转整数 (atoi) ...
- LeetCode刷题总结-数组篇(中)
本文接着上一篇文章<LeetCode刷题总结-数组篇(上)>,继续讲第二个常考问题:矩阵问题. 矩阵也可以称为二维数组.在LeetCode相关习题中,作者总结发现主要考点有:矩阵元素的遍历 ...
- LeetCode刷题总结-树篇(上)
引子:刷题的过程可能是枯燥的,但程序员们的日常确不乏趣味.分享一则LeetCode上名为<打家劫舍 |||>题目的评论: 如有兴趣可以从此题为起点,去LeetCode开启刷题之 ...
- C#LeetCode刷题-树
树篇 # 题名 刷题 通过率 难度 94 二叉树的中序遍历 61.6% 中等 95 不同的二叉搜索树 II 43.4% 中等 96 不同的二叉搜索树 51.6% 中等 98 验证二叉搜索树 ...
- C#LeetCode刷题-栈
栈篇 # 题名 刷题 通过率 难度 20 有效的括号 C#LeetCode刷题之#20-有效的括号(Valid Parentheses) 33.0% 简单 42 接雨水 35.6% 困难 71 简 ...
- C#LeetCode刷题-双指针
双指针篇 # 题名 刷题 通过率 难度 3 无重复字符的最长子串 24.5% 中等 11 盛最多水的容器 43.5% 中等 15 三数之和 16.1% 中等 16 最接近的三数之和 3 ...
随机推荐
- Ethical Hacking - GAINING ACCESS(11)
CLIENT SIDE ATTACKS - Listening for connections 1. Run Metasploit Move the backdoor file to the webs ...
- Python numpy 浮点数精度问题
Python numpy 浮点数精度问题 在复现FP(fictitious play, Iterative solution of games by fictitious play-page393)算 ...
- Shell基本语法---函数
函数 函数定义 function 函数名 () { 指令... return n } 函数调用及参数传递 function func() { echo "第零个参数:" $ #脚本 ...
- 微信小程序开发(一)基础知识学习
1.特点: ①无DOM对象(虚拟DOM),一切基于组件化(复用.解耦) ②四个重要文件: *.js.*.wxml --> html..wxss --> css.*.json ③无需下载 ...
- Python对列表去重的各种方法
一.循环去重 二.用 set() 去重 1.set()对list去重 2.list 是有序的,用 sort() 把顺序改回来 三.利用 dict 的属性来去重 1.用 dict 的 fromke ...
- vue学习(十一) v-for使用的注意事项:2.2.0+之后的版本里,当在组件中使用v-for时,key是必须的,它是用来表示唯一身份的
//html <div id="app"> <div> <label>id <input type="text" v- ...
- Android多线程--AsyncTask
常见的多线程方法有: 继承Thread类 实现Runnable接口 Handler AsyncTask HandlerThread 1.定义 一个Android已经封装好的轻量级异步类 属于抽象类,即 ...
- arcgis for js 如何用contains过滤数据
添加全部数据 // 构建map容器 var view = new MapView({ container: 'mapId', map: map }); /******************** * ...
- PHP preg_split() 函数
preg_replace 函数通过一个正则表达式分隔字符串.高佣联盟 www.cgewang.com 语法 array preg_split ( string $pattern , string $s ...
- PHP xml_set_element_handler() 函数
定义和用法 xml_set_element_handler() 函数规定在 XML 文档中元素的起始和终止调用的函数. 如果成功,该函数则返回 TRUE.如果失败,则返回 FALSE.高佣联盟 www ...