leetcode
来源:互联网 发布:珠海软件测试培训机构 编辑:程序博客网 时间:2024/06/15 09:34
62. Unique Paths
A robot is located at thetop-left corner of a m x n grid (marked 'Start' in the diagrambelow).
The robot can only move either down or right at any point intime. The robot is trying to reach the bottom-right corner of the grid (marked'Finish' in the diagram below).
Above is a 3 x 7 grid. How many possible unique paths are there?
算法思想
一、
第一次做这个题目的时候想利用数学规律直接计算,因为要走到终点,那么必须要走两次下,和六次右(此例题)。然后就有了很清晰的表达,就是在8步中选择2步,然后就能得出结果。然后进行测试,发现了一个问题,测试20X20的时候还可以保持正确的输出,但是超过了就不行了,因为数字太大(100X100)得出的正确都是负数。。。这个就不忍直视了,但是也没办法,__int64也用不了,然后老老实实地利用动态规划吧。
二、
动态规划:
主要就是找到状态转移方程,通过分解子问题,可以得到这样的一个方程
c[I , j ] = c[ I – 1, j] + c[ I , j – 1]
至于为什么有这个方程,因为要到达位置I , j 只能通过位置I – 1, j 和位置I , j – 1 处到达,所以位置i,j拥有的选择就是这两个位置之和。
class Solution {public: int uniquePaths(int m, int n) { int** a = new int*[m]; for(int i = 0 ; i < m ; i++) a[i] = new int[n]; for(int i = 0 ; i < m ; i++){ a[i][0] = 1; }//for i for(int i = 0 ; i < n ; i++){ a[0][i] = 1; }//for i for(int i = 1 ; i < m ; i++){ for(int j = 1; j < n ; j++){ a[i][j] = a[i-1][j] + a[i][j-1]; }//for j }//for i return a[m-1][n-1]; }};
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode
- leetcode:
- leetcode:
- LeetCode
- leetcode
- LEETCODE
- leetcode
- leetCode
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode:
- leetcode
- Java的封装类
- 计算几何模板总结(一)
- A股-入门-如何巧用集合竞价规避风险
- Ubuntu16.10偶遇linux0.11
- Docker入门教程(二)——基本命令
- leetcode
- poj1811(pollard-rho求一个大数(64位)的最小因子)
- 搭建Docker私有仓库--自签名方式
- 八大排序算法之冒泡排序
- 点击菜单跳转页面给点击的菜单添加新的样式
- Myeclipse 的优化
- 更改matlab默认工作路径
- ES6——Day2(跨模块常量+全局对象属性)
- 【面试题】剑指offer 19