LeetCode_Unique Paths
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?
方法一: DFS 大数据超时
class Solution {
public:
void DFS(int i, int j, int m, int n){
if( i== m && j == n){
result++;
}
if(j+ <= n) DFS(i, j+,m,n);
if(i+ <= m) DFS(i+,j,m,n);
}
int uniquePaths(int m, int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
result = ;
DFS(,,m,n);
return result;
}
private:
int result;
};
方法二:动态规划。 grid[i][j] = grid[i-1][j]+grid[i][j-1]
class Solution {
public:
int uniquePaths(int m, int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<vector<int>> grid(m,vector<int>(n,));
for(int i = ; i< n;i++) grid[][i] = ;
for(int i = ; i< m;i++) grid[i][] = ; for(int i = ; i< m; i++)
for(int j = ; j < n; j++)
grid[i][j] = grid[i-][j] + grid[i][j-];
return grid[m-][n-] ; }
};
LeetCode_Unique Paths的更多相关文章
- LeetCode_Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [LeetCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- [LeetCode] Unique Paths II 不同的路径之二
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [LeetCode] Unique Paths 不同的路径
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- leetcode : Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- UVA 10564 Paths through the Hourglass[DP 打印]
UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...
- LeetCode-62-Unique Paths
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- Leetcode Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- POJ 3177 Redundant Paths(边双连通的构造)
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13717 Accepted: 5824 ...
随机推荐
- 【转】android:DDMS查看Threads--不错
原文网址:http://www.cnblogs.com/mybkn/archive/2012/05/27/2520335.html 有时候程序运行出现死锁或者信号量卡死是很纠结的问题,单看代码很难分析 ...
- #翻译# 深入JavaScript的Unicode难题(上)
退一步说, JavaScript处理Unicode时有些怪异. 这篇文章会说明JS在Unicode上令人痛苦的部分, 然后提供解决方案, 并说明在未来的ECMAScript6中是如何改善这些问题的. ...
- Android输入法界面管理(打开/关闭/状态获取)
最近做一个带发表情的聊天界面,需要管理系统输入法的状态, 一.打开输入法窗口: InputMethodManager inputMethodManager = (InputMethodManager) ...
- Java中的自动装箱与拆箱
自动装箱和拆箱从Java 1.5开始引入,目的是将原始类型值转自动地转换成对应的对象.自动装箱与拆箱的机制可以让我们在Java的变量赋值或者是方法调用等情况下使用原始类型或者对象类型更加简单直接. 如 ...
- Hello,world,l'm coming!
#include<studio.h> int main() { printf("Hello,Word!" l'm coming\n"); return0; }
- Spring的StringUtils工具类
本文是转载文章,感觉比较好,如有侵权,请联系本人,我将及时删除. 原文网址:<Spring的StringUtils工具类> org.springframework.util.StringU ...
- XML的DOM、SAX、DEMO4J及DEMO4J整合Path的代码例子
1.DMO解析 package cn.itcast.xml.dom; import java.io.File; import javax.xml.parsers.DocumentBuilder; im ...
- js获得url的参数
网上找的一段代码,非常好用,现在难以找到原作者,但是非常感谢!/** * 获取当前URL参数值 * @param name 参数名称 * @return 参数值 */ function ...
- RxJava漫谈-RxAndroid使用
RxJava在github上的地址:https://github.com/ReactiveX/RxJava RxAndroid在github上的地址:https://github.com/Reacti ...
- iOS字体 UIFont 字体名字大全
打印所有的字体 NSArray *familyNames = [UIFont familyNames];//所有的家族名称 for(NSString *familyName in familyName ...