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的更多相关文章

  1. LeetCode_Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  2. [LeetCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  3. [LeetCode] Unique Paths II 不同的路径之二

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  4. [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 ...

  5. leetcode : Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  6. UVA 10564 Paths through the Hourglass[DP 打印]

    UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...

  7. 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 ...

  8. Leetcode Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  9. POJ 3177 Redundant Paths(边双连通的构造)

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13717   Accepted: 5824 ...

随机推荐

  1. COJ 0024 N皇后问题

    N皇后问题 难度级别:B: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述     在N*N的方格棋盘放置N个皇,使得它们不相互攻击(即任意2个 ...

  2. bzoj3995[SDOI2015]道路修建

    http://www.lydsy.com/JudgeOnline/problem.php?id=3995 线段树维护连通性. 我们发现,对于一个区间[L,R],我们只需要知道(1,L),(2,L),( ...

  3. HDU_2026——将单词的首字母变大写

    Problem Description 输入一个英文句子,将每个单词的第一个字母改成大写字母.   Input 输入数据包含多个测试实例,每个测试实例是一个长度不超过100的英文句子,占一行.   O ...

  4. java与数据结构(8)---java实现链队列

    链队列 实际上就是单链表,只是规定了删除在队头进行,添加在队尾进行. 链队列代码结构 package list.queue; public interface Queuable<T>; p ...

  5. sicily 4433 DAG?

    题意:输入一个有向图,判断该图是否是有向无环图(Directed Acyclic Graph). 解法:还是深搜 #include<iostream> #include<memory ...

  6. 微软在线测试题String reorder

    问题描述: Time Limit: 10000msCase Time Limit: 1000msMemory Limit: 256MB DescriptionFor this question, yo ...

  7. java foreach记录

    实现原理解释: http://blog.csdn.net/a596620989/article/details/6930479 http://stackoverflow.com/questions/8 ...

  8. Highcharts 带有数据标签曲线图表

    <html> <head> <meta charset="UTF-8" /> <title>Highcharts</title ...

  9. asp.net 分页类

    PaginatedList.cs using System;using System.Collections.Generic;using System.Linq;using System.Web; n ...

  10. js判断上传文件大小

    下面提供三款网页特效判断上传文件大小哦,这三种方法是现在限制文件上传大小比较好的方法,可以在客户上传文件时限制上传文件大小判断处理<!doctype html public "-//w ...