77. Combinations
题目
分析:求给定数字n,k的组合数,方法是采用深度搜索算法,代码如下(copy网上代码)
1 class Solution { 2 public: 3 void dfs77(vector<vector<int > > &ans, vector<int> subans, int start, int n, int k) 4 { 5 if (subans.size() == k) 6 { 7 ans.push_back(subans); return ; 8 } 9 for (int i = start; i <= n; i++) 10 { 11 subans.push_back(i); 12 dfs77(ans, subans, i + 1, n, k); 13 subans.pop_back(); // 满足一个条件或者该分支递归完后要删除最后一个 14 } 15 } 16 vector<vector<int> > combine(int n, int k) { 17 vector<vector<int > > ans; 18 if (n < k || k == 0) return ans; 19 vector<int> subans; 20 dfs77(ans, subans, 1, n, k); 21 return ans; 22 } 23 };
--------------------------------------------------------------------------------分割线------------------------------------------------------------------
78. Subsets
题目
分析:求一个集合的所有子集,代码如下(copy网上代码)
1 class Solution { 2 public: 3 vector<vector<int>>res; 4 vector<int>ans; 5 vector<vector<int>> subsets(vector<int>& nums) { 6 if(nums.empty()) return res; 7 sort(nums.begin(), nums.end()); 8 dfs(0, ans, nums); 9 return res; 10 11 } 12 void dfs(int k, vector<int>ans, vector<int> nums){ 13 res.push_back(ans); 14 for(int i = k; i < nums.size(); i++){ 15 ans.push_back(nums[i]); 16 dfs(i + 1, ans, nums); 17 ans.pop_back(); 18 } 19 } 20 };
--------------------------------------------------------------------------------------分割线------------------------------------------------------------
79. Word Search
题目
分析:看到这类题目,首先就应该想到“回溯法”,代码如下,具体思路见注释:
1 class Solution { 2 public: 3 int m,n; 4 public: 5 bool exist(vector<vector<char>>& board, string word) { 6 if(""==word) 7 return true; 8 m=board.size(); 9 n=board[0].size(); 10 vector<vector<bool>> flag(m,vector<bool>(n,false)); 11 for(int i=0;i<m;i++) 12 { 13 for(int j=0;j<n;j++) 14 { 15 if(board[i][j]==word[0]) 16 { 17 flag[i][j]=true; 18 if(HuiSu(board,i,j,word,0,flag)) 19 return true; 20 else 21 flag[i][j]=false; 22 23 24 25 } 26 } 27 } 28 return false; 29 } 30 31 bool HuiSu(const vector<vector<char>>& board,int i,int j,const string& word,int index,vector<vector<bool>>& flag) 32 { 33 if(word[index+1] == '\0') 34 return true; 35 36 int ii,jj; 37 ii=i; 38 jj=j+1; 39 bool temp; 40 if(isTrue(ii,jj))//向右 41 { 42 if(flag[ii][jj] == false && board[ii][jj] == word[index+1]) 43 { 44 flag[ii][jj] = true; 45 temp = HuiSu(board,ii,jj,word,index+1,flag); 46 if(temp) 47 return true; 48 flag[ii][jj] = false;//这一步很重要 49 } 50 } 51 52 ii=i; 53 jj=j-1; 54 if(isTrue(ii,jj))//向左 55 { 56 if(flag[ii][jj] == false && board[ii][jj] == word[index+1]) 57 { 58 flag[ii][jj] = true; 59 temp = HuiSu(board,ii,jj,word,index+1,flag); 60 if(temp) 61 return true; 62 flag[ii][jj] = false; 63 } 64 } 65 66 ii=i-1; 67 jj=j; 68 if(isTrue(ii,jj))//向上 69 { 70 if(flag[ii][jj] == false && board[ii][jj] == word[index+1]) 71 { 72 flag[ii][jj] = true; 73 temp = HuiSu(board,ii,jj,word,index+1,flag); 74 if(temp) 75 return true; 76 flag[ii][jj] = false; 77 } 78 } 79 80 ii=i+1; 81 jj=j; 82 if(isTrue(ii,jj))//向下 83 { 84 if(flag[ii][jj] == false && board[ii][jj] == word[index+1]) 85 { 86 flag[ii][jj] = true; 87 temp = HuiSu(board,ii,jj,word,index+1,flag); 88 if(temp) 89 return true; 90 flag[ii][jj] = false; 91 } 92 } 93 94 95 return false; 96 } 97 98 bool isTrue(int i,int j) 99 { 100 if(i<0) 101 return false; 102 else if(i>=m) 103 return false; 104 else if(j<0) 105 return false; 106 else if(j>=n) 107 return false; 108 109 return true; 110 } 111 };