Rotate Image & Binary Tree Postorder Traversal & Minimum Path Sum
来源:互联网 发布:怎么找网络水军 编辑:程序博客网 时间:2024/05/01 10:31
(1) Rotate Image
根据[1]:
class Solution {public: void rotate(vector<vector<int> > &matrix) { int n=matrix.size(); for(int i=0;i<n;i++) for(int j=0;j<n-i;j++) swap(matrix[i][j],matrix[n-1-j][n-1-i]); for(int i=0;i<n/2;i++) for(int j=0;j<n;j++) swap(matrix[i][j],matrix[n-1-i][j]); }};
(2) Binary Tree Postorder Traversal
class Solution {public: vector<int> postorderTraversal(TreeNode *root) { vector<int> ret; stack<TreeNode *> stk; TreeNode *visited; if(!root) return ret; while(root || !stk.empty()) { while(root) { stk.push(root); root=root->left; } root=stk.top(); if(!root->right || visited==root->right) { ret.push_back(root->val); visited=root; stk.pop(); root=NULL; } else root=root->right; } return ret; }};更详细有关二叉树的遍历参考[2]。
(3) Minimum Path Sum
典型的动态规划问题:
class Solution {public: int minPathSum(vector<vector<int> > &grid) { int n=grid[0].size(), m=grid.size(); int tmp[m][n]; tmp[0][0]=grid[0][0]; if(m>1) for(int i=1;i<m;i++) tmp[i][0]=tmp[i-1][0]+grid[i][0]; if(n>1) for(int j=1;j<n;j++) tmp[0][j]=tmp[0][j-1]+grid[0][j]; if(m>1 && n>1) for(int i=1;i<m;i++) for(int j=1;j<n;j++) tmp[i][j]=min(tmp[i-1][j]+grid[i][j],tmp[i][j-1]+grid[i][j]); return tmp[m-1][n-1]; }};
参考:
[1] http://www.2cto.com/kf/201401/274473.html
[2] http://blog.csdn.net/wzy_1988/article/details/8450952
0 0
- Rotate Image & Binary Tree Postorder Traversal & Minimum Path Sum
- 124. Binary Tree Maximum Path Sum&145.Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Android中ListView下拉刷新的实现
- 读书笔记:视觉测量的方法
- 关于JDBC下使用SQLite DB问题总结(Windows下和Linux下使用)
- FreeMarker整合Spring 3
- Spring DispatcherServlet 工作机制
- Rotate Image & Binary Tree Postorder Traversal & Minimum Path Sum
- 文件类型声明的理解来源
- 【android开发】使用PopupWindow实现页面点击顶部弹出下拉菜单
- Drupal 7 建站学习手记(三):Nivo Slider模块报错的问题
- ubuntu+haproxy+keepalived主从负载
- MDI窗体(多文档界面)
- java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive
- 云环境下面向能耗降低的资源负载均衡方法
- SQLite入门