LeetCode之Unique Paths
2015-09-27 16:36
330 查看
/*备忘录法。*/ class Solution { public: int uniquePaths(int m, int n) { if(m < 1 || n < 1) return 0; vector<vector<int> > path(m+1, vector<int>(n+1, 0)); for(int i = 1; i <= m; ++i){ for(int j = 1; j <= n; ++j){ path[i][j] = dfs(i, j, path); } } return path[m] ; } int dfs(int row, int col, vector<vector<int> > &path){ if(row == 1 || col == 1) return 1; else return get(row-1, col, path) + get(row, col-1, path); } int get(int row, int col, vector<vector<int> > &path){ if(row == 1 || col == 1) return 1; if(path[row][col] > 0) return path[row][col]; else return dfs(row, col, path); } }; /*动态规划法。*/ class Solution { public: int uniquePaths(int m, int n) { if(m < 1 || n < 1) return 0; vector<int> path(n+1, 0); path[1] = 1; for(int i = 1; i <= m; ++i){ for(int j = 2; j <= n; ++j){ path[j] = path[j] + path[j-1]; } } return path ; } };
相关文章推荐
- UISlider滑块
- 【UIKit-110-3】#import <UIKit/UITableView.h>增删改移
- Scala深入浅出进阶经典 第70讲:Scala界面GUI编程实战详解
- android四种更新UI的方法
- 辨析String、StringBuffer和StringBuilder
- win7系统桌面自动生成“我的Bluetooth位置”图标的解决方法
- iOS之UI随笔-UIButton与UIImageView
- php array_push 与 $arr[]=$value 性能比较
- UI:数据的解析XML与JSON
- Access restriction: The type TaskTopicResolver is not accessible due to restrict
- iOS_覆盖uitabbarcontroller全屏的view
- 'sessionFactory' or 'hibernateTemplate' is required
- [LeetCode] Unique Binary Search Trees(!!DP)
- 【UIKit-110-2】#import <UIKit/UITableView.h>获取数据与滚动
- IOS UI画线
- 检测到有潜在危险的 Request.Form 值
- iOS开发UI篇—xib的简单使用
- 【UIKit-110-1】#import <UIKit/UITableView.h> 基础创建
- Auto Layout界面自动布局 (Interface Builder布局)
- Longest Consecutive Sequence