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

Tips:机器人从左上一直走到右下,(只能走右与下)直到走到FInish的位置。

package medium;

import java.util.Arrays;

public class L62UniquePaths {
public int uniquePaths(int m, int n) {
int[][] visited = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
visited[i][j] = -1;
System.out.println(i + "," + j + ">" + visited[i][j]);
}
}
int count = movingCount(m, n, 0, 0, visited);
return count;
}
public int movingCount(int m, int n, int row, int col, int[][] visited) {
int count = 0;
if (row < 0 || col < 0 || row >= m || col >= n)
return 0;
if (row == m - 1 && col == n - 1)
return 1;
if (visited[row][col] != -1)
return visited[row][col];
count = movingCount(m, n, row + 1, col, visited) + movingCount(m, n, row, col + 1, visited);
visited[row][col] = count;
return count;
} //另外一种很快地方法。当前状态依赖于前一种状态
public int Solution2(int m, int n) {
int[] row = new int[n];
Arrays.fill(row,1);
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
row[j]+=row[j-1];
}
}
return row[n-1];
} public static void main(String[] args) {
L62UniquePaths cc = new L62UniquePaths();
int count = cc.uniquePaths(3, 4);
System.out.println(count);
}
}

 63. Unique Paths II

Follow up for "Unique Paths":

Now consider if some obstacles are added to the grids. How many unique paths would there be?

An obstacle and empty space is marked as 1 and 0 respectively in the grid.

For example,

There is one obstacle in the middle of a 3x3 grid as illustrated below.

[
[0,0,0],
[0,1,0],
[0,0,0]
]

The total number of unique paths is 2.

Note: m and n will be at most 100.

Tips:本题目是根据62题,稍作改变得来的,数组中1的位置不能走。

package medium;

public class L63UniquePaths2 {

	public int uniquePathsWithObstacles(int[][] obstacleGrid) {
if (obstacleGrid == null)
return 0;
int m = obstacleGrid.length;
int n = obstacleGrid[0].length;
int[][] visited = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
visited[i][j] = -1;
System.out.println(i + "," + j + ">" + visited[i][j]);
}
}
int count = movingCount(m, n, 0, 0, visited, obstacleGrid);
return count; } public int movingCount(int m, int n, int row, int col, int[][] visited, int[][] obstacleGrid) {
int count = 0;
if (row < 0 || col < 0 || row >= m || col >= n)
return 0;
if (obstacleGrid[row][col] == 0) {
if (row == m - 1 && col == n - 1)
return 1;
if (visited[row][col] != -1)
return visited[row][col];
count = movingCount(m, n, row + 1, col, visited, obstacleGrid)
+ movingCount(m, n, row, col + 1, visited, obstacleGrid);
visited[row][col] = count;
} return count;
} public static void main(String[] args) {
L63UniquePaths2 l63 = new L63UniquePaths2();
int[][] obstacleGrid = { { 0, 0, 0 }, { 0, 1, 0 }, { 0, 0, 0 } };
int[][] aa = { { 1 } };
int count = l63.uniquePathsWithObstacles(aa);
System.out.println(count); }
}

【leetcode】62.63 Unique Paths的更多相关文章

  1. &lt;LeetCode OJ&gt; 62. / 63. Unique Paths(I / II)

    62. Unique Paths My Submissions Question Total Accepted: 75227 Total Submissions: 214539 Difficulty: ...

  2. 【LeetCode】62. Unique Paths 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...

  3. 【LeetCode】62. Unique Paths

    Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagra ...

  4. 【leetcode】62. Uniqe Paths

    题目 https://oj.leetcode.com/problems/unique-paths/   A robot is located at the top-left corner of a m ...

  5. 【LeetCode】387. First Unique Character in a String

    Difficulty:easy  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/first-unique-cha ...

  6. 62. 63. Unique Paths 64. Minimum Path Sum

    1. A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...

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

  8. leetcode@ [62/63] Unique Paths II

    class Solution { public: int uniquePathsWithObstacles(vector<vector<int>>& obstacleG ...

  9. 【LeetCode】257. Binary Tree Paths 解题报告(java & python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leet ...

随机推荐

  1. 双端队列 ADT接口 链表实现

    Deque ADT接口 DEQUEUE.h: #include <stdlib.h> #include "Item.h" typedef struct DEQUEUEn ...

  2. Vue2.5入门-2

    todolist功能开发 代码 <!DOCTYPE html> <html> <head> <title>vue 入门</title> &l ...

  3. python-对于mysql数据库的操作

    python操作mysql数据库 问题:DDL,DCL,DML的区别? 语言与框架:jdbc/odbc操作数据库 java(ibatis/hibernate/jpa)操作数据库 客户端工具:navic ...

  4. linux编程实现pwd命令

    linux编程实现pwd命令 在linux中,一切皆文件.目录其实也是一种文件,只不过这种文件比较特殊,它里面存储的是一张对应表,即文件名和i节点的对应关系表,而i节点才是记录此文件详细信息的结构,如 ...

  5. 再论WPF中的UseLayoutRounding和SnapsToDevicePixels

    原文:再论WPF中的UseLayoutRounding和SnapsToDevicePixels 版权声明:.net/web/医疗技术的木子纵横的个人分享 https://blog.csdn.net/m ...

  6. 【JUC源码解析】ConcurrentHashMap

    简介 支持并发的哈希表.其中包括红黑树,扩容,分槽计数等知识点. 源码分析 常量 private static final int MAXIMUM_CAPACITY = 1 << 30; ...

  7. 查询数据库所有表和字段及其注释(mysql)

    #查询某个库所有表 select * from information_schema.TABLES where table_schema = '数据库' #查询某个库所有表的字段 select * f ...

  8. tomcat 部署项目到服务器

    参考博客,我选了一种最简单的方法来部署项目. 在tomcat 目录下 的  conf\Catalina\localhost 目录中,新建一个   ' 项目名.xml '   文件,名字用项目名表示, ...

  9. java计算两个日期之间的天数,排除节假日和周末

    如题所说,计算两个日期之前的天数,排除节假日和周末.这里天数的类型为double,因为该功能实现的是请假天数的计算,有请一上午假的为0.5天. 不够很坑的是每个日期都要查询数据库,感觉很浪费时间. 原 ...

  10. SQL Server临时表漫谈

    SQL Server是微软的关系型数据库,对于刚入门的我是一个非常友好的开发工具.可视化界面的安装与操作,非常适合刚入门的我. 其实大家要找这方面的资料,在网上一搜一大堆,这里我就不赘述那些了,基本都 ...