【062-Unique Paths(唯一路径)】


【LeetCode-面试算法经典-Java实现】【全部题目文件夹索引】

原题

  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 3 x 7 grid. How many possible unique paths are there?

  Note: m and n will be at most 100.

题目大意

  一个机器人在一个m*n的方格的左上角。

  机器人仅仅能向右或都向下走一个方格,机器人要到达右下角的方格。

  请问一共同拥有多少种唯一的路径。

  注意:m和n最大不超100。

解题思路

  典型的动态规划问题,对问题使用动态规划的方法进行求解。

  用一个m*n的组数A保存结果。

  对于A数组中的元素有。

  1、当x=0或者y=0时有A[x][y] = 1;

  2、当x>=1而且y>=1时有A[\x][\y] = A[x-1][y]+A[\x][y-1]。

  3、所求的结点就是A[m-1][n-1]。

代码实现

算法实现类

public class Solution {
public int uniquePaths(int m, int n) {
int[][] result = new int[m][n]; // 第一列的解
for (int i = 0; i < m; i++) {
result[i][0] = 1;
} // 第一行的解
for (int i = 1; i < n; i++) {
result[0][i] = 1;
} // 其他位置的解
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
result[i][j] = result[i - 1][j] + result[i][j - 1];
}
} // 所求的解
return result[m - 1][n - 1];
}
}

评測结果

  点击图片,鼠标不释放。拖动一段位置,释放后在新的窗体中查看完整图片。

特别说明

欢迎转载。转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47182719

【LeetCode-面试算法经典-Java实现】【062-Unique Paths(唯一路径)】的更多相关文章

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

  2. 062 Unique Paths 不同路径

    机器人位于一个 m x n 网格的左上角, 在下图中标记为“Start” (开始).机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角,在下图中标记为“Finish”(结束).问有多少条不 ...

  3. 【LeetCode-面试算法经典-Java实现】【015-3 Sum(三个数的和)】

    [015-3 Sum(三个数的和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an array S of n integers, are there ...

  4. 【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】

    [139-Word Break(单词拆分)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s and a dictionary of w ...

  5. 【LeetCode-面试算法经典-Java实现】【063-Unique Paths II(唯一路径问题II)】

    [063-Unique Paths II(唯一路径问题II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Follow up for "Unique Pa ...

  6. 【LeetCode-面试算法经典-Java实现】【096-Unique Binary Search Trees(唯一二叉搜索树)】

    [096-Unique Binary Search Trees(唯一二叉搜索树)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given n, how many s ...

  7. 【LeetCode-面试算法经典-Java实现】【053-Maximum Subarray(最大子数组和)】

    [053-Maximum Subarray(最大子数组和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Find the contiguous subarray w ...

  8. 【LeetCode-面试算法经典-Java实现】【059-Spiral Matrix II(螺旋矩阵II)】

    [059-Spiral Matrix II(螺旋矩阵II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an integer n, generate a ...

  9. 【LeetCode-面试算法经典-Java实现】【136-Single Number(仅仅出现一次的数字)】

    [136-Single Number(仅仅出现一次的数字)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an array of integers, ev ...

随机推荐

  1. 097实战 关于ETL的几种运行方式

    一:代码部分 1.新建maven项目 2.添加需要的java代码 3.书写mapper类 4.书写runner类 二:运行方式 1.本地运行 2.集群运行 3.本地提交集群运行 三:本地运行方式 1. ...

  2. Emit学习笔记

    1,给字段设置值,并返回 static void Main(string[] args) { //给字段设置值,并返回 AssemblyName assemblyName = new Assembly ...

  3. C# DataGridView转DataTable

    public static DataTable ToDataTable(this DataGridView dataGridView, string tableName = null) { DataG ...

  4. BZOJ 4405 [wc2016]挑战NPC 带花树 一般图最大匹配

    https://www.lydsy.com/JudgeOnline/problem.php?id=4405 这道题大概就是考场上想不出来,想出来也调不出来的题. 把每个桶拆成三个互相有边的点,每个球向 ...

  5. AGC 015C.Nuske vs Phantom Thnook(思路 前缀和)

    题目链接 闻本题有格子,且何谓格子也 \(Description\) 给定\(n*m\)的蓝白矩阵,保证蓝格子形成的的同一连通块内,某蓝格子到达另一个蓝格子的路径唯一. \(Q\)次询问.每次询问一个 ...

  6. C++ 类模板基础知识

    类模板与模板类 为什么要引入类模板:类模板是对一批仅仅成员数据类型不同的类的抽象,程序员只要为这一批类所组成的整个类家族创建一个类模板,给出一套程序代码,就可以用来生成多种具体的类,(这类可以看作是类 ...

  7. Problem E: 用链表实现约瑟夫环

    Description 你听说过约瑟夫问题吗?问题大致如下:首先n个人围成一个圈,标记为1到n号.接着,从1号开始报数(从1开始),然后2号报数,然后3号...当有人报到到m时,这个人就要踢出比赛,然 ...

  8. Git Windows 安装

    环境 Windows版本:Windows 7 旗舰版 处理器:Inte i5 系统类型:64 位操作系统 下载 Git Windows https://github.com/git-for-windo ...

  9. js常用事件

    为了便于使读者更好地运用js事件,就把常用事件大致分为以下几种: a. 表单元素事件,在表单元素中生效 onfocus  ------获取焦点 onblur -------失去焦点 onsubmit ...

  10. Hadoop化繁为简(三)—探索Mapreduce简要原理与实践

    目录-探索mapreduce 1.Mapreduce的模型简介与特性?Yarn的作用? 2.mapreduce的工作原理是怎样的? 3.配置Yarn与Mapreduce.演示Mapreduce例子程序 ...