Valid Sudoku & Subsets I&&II & Flatten Binary Tree to Linked List & Jump Game I&&II
来源:互联网 发布:淘宝怎么弄淘口令 编辑:程序博客网 时间:2024/04/30 11:01
(1) Valid Sudoku
就是每一行,每一列,每一个方块的check,还有一种简化版[1]:
class Solution {public: bool isValidSudoku(vector<vector<char> > &board) { int row[9],col[9],area[9]; for(int i=0;i<9;i++) { memset(row,0,9*sizeof(int)); memset(col,0,9*sizeof(int)); for(int j=0;j<9;j++) { if(board[i][j]!='.') { if(row[board[i][j]-'1']>0) return false; else row[board[i][j]-'1']++; } if(board[j][i]!='.') { if(col[board[j][i]-'1']>0) return false; else col[board[j][i]-'1']++; } } } for(int i=0;i<9;i+=3) for(int j=0;j<9;j+=3) { memset(area,0,9*sizeof(int)); for(int k=0;k<3;k++) for(int l=0;l<3;l++) if(board[i+k][j+l]!='.') { if(area[board[i+k][j+l]-'1']>0) return false; else area[board[i+k][j+l]-'1']++; } } return true; }};
(2) Subsets
DFS每次返回当前解,当解深度大于最大深度时返回:
class Solution {private: void solve(int dep, int maxDep, vector<int> tmp,vector<int> S,vector<vector<int>> &ret, int begin){ ret.push_back(tmp); if(dep==maxDep) return; for(int i=begin;i<S.size();i++) { vector<int> a(tmp); a.push_back(S[i]); solve(dep+1,maxDep,a,S,ret,i+1); } }public: vector<vector<int> > subsets(vector<int> &S) { vector<vector<int>> ret; vector<int> tmp; tmp.clear(); sort(S.begin(),S.end()); solve(0,S.size(),tmp,S,ret,0); return ret; }};
(3) Subset II
比(2)多一个检查是否重复的判断:
class Solution {private: void solve(int dep, int maxDep, vector<int> tmp,vector<int> S,vector<vector<int>> &ret, int begin){ ret.push_back(tmp); if(dep==maxDep) return; for(int i=begin;i<S.size();i++) { vector<int> a(tmp); if(i>begin) { if(S[i]>S[i-1]) { a.push_back(S[i]); solve(dep+1,maxDep,a,S,ret,i+1); } } else { a.push_back(S[i]); solve(dep+1,maxDep,a,S,ret,i+1); } } }public: vector<vector<int> > subsetsWithDup(vector<int> &S) { vector<vector<int>> ret; vector<int> tmp; tmp.clear(); sort(S.begin(),S.end()); solve(0,S.size(),tmp,S,ret,0); return ret; }};
(4) Flatten Binary Tree to Linked List
非递归后序遍历:
class Solution {public: void flatten(TreeNode *root) { if(!root) return; stack<TreeNode *> stk; TreeNode *preNode=NULL,*curNode=NULL; stk.push(root); while(!stk.empty()) { curNode=stk.top(); stk.pop(); if(preNode) { preNode->left=NULL; preNode->right=curNode; } preNode=curNode; if(curNode->right) stk.push(curNode->right); if(curNode->left) stk.push(curNode->left); } }};
(5) Jump Game
只用关心最远能跳到什么地方[2]:
class Solution {public: bool canJump(int A[], int n) { if(n==0) return false; int maxPos=A[0]; for(int i=0;i<=min(maxPos,n-1);i++) maxPos=max(maxPos,i+A[i]); return maxPos>=n-1; }};
(6) Jump Game II
根据[3]:
class Solution {public: int jump(int A[], int n) { int jump[n]; int maxJump=0; memset(jump,0,n*sizeof(int)); for(int i=0;i<n;i++) { for(int j=maxJump-i+1;j<=A[i] && i+j<n;j++) jump[i+j]=jump[i]+1; maxJump=max(maxJump,i+A[i]); } return jump[n-1]; }};
参考:
[1]http://blog.csdn.net/doc_sgl/article/details/13002461
[2]http://blog.csdn.net/pickless/article/details/9855891
[3]http://blog.csdn.net/pickless/article/details/9776661
0 0
- Valid Sudoku & Subsets I&&II & Flatten Binary Tree to Linked List & Jump Game I&&II
- leetcode -day17 Path Sum I II & Flatten Binary Tree to Linked List & Minimum Depth of Binary Tree
- Jump Game I , II
- jump game I // II
- Jump Game I,II
- [leetcode]Jump Game I II
- LeetCode:Jump Game I && II
- Jump Game I,II 贪心
- Linked List Cycle I & II
- Linked List Cycle I&&II
- Linked List Cycle I / II
- Linked List Cycle I II
- Reverse Linked List I,II
- Linked List Cycle I&II
- LeetCode: Flatten Binary Tree to Linked List
- LeetCode Flatten Binary Tree to Linked List
- LeetCode: Flatten Binary Tree to Linked List
- [Leetcode] Flatten Binary Tree to Linked List
- c++ 单例
- Android菜鸟在成长-对话选项框2单选和复选
- latex 图表 figure and table 混排
- php bom去除工具_php批量去除bom的代码[交流qq 158393237] 微信管家不显示验证码 微信管家无法显示验证码
- [ios]关于CoreData的一个工具Mogenerator的使用
- Valid Sudoku & Subsets I&&II & Flatten Binary Tree to Linked List & Jump Game I&&II
- Java学习笔记_16_JavaBean
- SQL 列转行/list agg 列超长处理方法
- 漫谈惯性、视觉暂留、潜意识、混合动力发动机、先入为主 - 纯粹的无厘头?
- android json解析及简单例子
- Java 8的元空间
- leetcode 第一刷_Remove Nth Node From End of List
- 微信管家不显示验证码的解决方式
- C指针理解