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 ...
随机推荐
- Shell循环处理
date=`echo $1 | tr -d '-'` date1=`echo $1` date_end=`get_date $2 +1 | sed 's/-//g'` while [ 1 ] do d ...
- HDU_2040——判断亲和数
Problem Description 古希腊数学家毕达哥拉斯在自然数研究中发现,220的所有真约数(即不是自身的约数)之和为: 1+2+4+5+10+11+20+22+44+55+110=284. ...
- HDU-2059龟兔赛跑(基础方程DP-遍历之前的所有状态)
Problem Description 据说在很久很久以前,可怜的兔子经历了人生中最大的打击——赛跑输给乌龟后,心中郁闷,发誓要报仇雪恨,于是躲进了杭州下沙某农业园卧薪尝胆潜心修炼,终于练成了绝技,能 ...
- HDOJ 3622 - Bomb Game 2-sat+二分....细心...
题意: 有N个炸弹..每个炸弹有两个位置可以选择..把炸弹放到其中一个地方去...炸弹的爆炸范围是其为圆心的圆...两个炸弹不能有攻击范围上的重合..问要满足条件..炸弹爆炸范围的半径最长能是多少.. ...
- JAVA的四种引用,强弱软虚用到的场景
1.强引用 最常用的引用类型,如Object object = new Object(),只要强引用存在,GC必定 不回收,即使当前内存空间不足,jAVA虚拟机宁愿抛出OutofMemoryError ...
- 【转】MVC5中的区域(Areas)
MVC本身提倡的就是关注点分离.但是当项目本身的业务逻辑足够复杂,如果所有的业务逻辑都写个Controller文件夹下面的时候,你会看到非常庞大的各种命名的Controller,这个时候区域的作用就非 ...
- (转)Linux整合apache和tomcat构建Web服务器
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://wenzhongxiang.blog.51cto.com/6370734/1285 ...
- openfire连接登陆优化方案
client登陆openfire,大概总共须要9个来回才完毕登录. 在2G情况下.就表现为client登录特别慢,所以,为解决问题,对openfire进行了例如以下优化 openfire的连接.登陆过 ...
- JDK版本错误:Unsupported major.minor version 51.0
错误原因 有时候把项目从本机编译文件部署到服务器,或者发给别人使用时,会报如下异常: java.lang.UnsupportedClassVersionError: test_hello_world ...
- Cycling Label
Cycling Label 来源: github/BBCyclingLabel Licence: Apache 2.0 作者: Bruno de Carvalho 分类: 标签(Label) 平台: ...