Unique Paths
2015-12-22 23:29
447 查看
package cn.edu.xidian.sselab.array; /** * * @author zhiyong wang * title: Unique Paths * content: * 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? * */ public class UniquePaths { //第一反应应该是递归, //递归要有约束条件,所以从终点向前进行走,(1,1)到(m,n)的路径和等于(1,1)到(m-1,n)与(m,n-1)路径之和 //总结起来,自己递归的意识还是不够强 //但是这种思路超时了 public int uniquePaths(int m,int n){ if(m==1 || n==1) return 1; else{ return uniquePaths(m-1,n) + uniquePaths(m,n-1); } } //用组合数学的求解方法,从(1,1)到(m,n)的总步数是一定的m + n - 2; //因为只能向右,与向下走,所以向右走的步数也是确定的n-1,向下走的步数也是确定的m-1; //路径总数为:C(m+n-2) (m-1)即(m+n-2)!/(m-1)!(n-1)! public int uniquePath(int m,int n){ int all = n + m - 2; int down = m - 1; double total = 1;//注意这个地方一开始定义是int,会把中间结果处理严重,导致结果不正确 for(int i=1;i<=down;i++){ total = total * (all-down+i) / i; } return (int)total; } } |
相关文章推荐
- toolbar easyui
- android124 zhihuibeijing 新闻中心-组图
- IOS基础-UINAVIGATIONCONTROLLER-不写代码实现分层显示
- UIView中的坐标转换
- Ceph 多节点quick部署
- UIView常用的一些方法小记之setNeedsDisplay和setNeedsLayout
- PHP中include和require的区别详解
- 如何有效的遍历django的QuerySet
- CF# Educational Codeforces Round 3 F. Frogs and mosquitoes
- UEditor1.4.3在SSH框架项目中上传图片的设置
- Xcode7 UI自动化测试详解 带demo UITests
- 简单地响应式布局----CSS3中的Media Query(媒介查询)
- Leetcode204: N-Queens II
- 例题5-6 UVA 540 Team Queue团体队列
- Leetcode203: N-Queens
- 关于 IOS7下 UITextview的contentsize.height不能准确判断高度
- UILabel和UIFont开发技巧
- UITextField技巧
- iOS 给UIView添加xib
- Android BlueTooth开发