Posts

Showing posts with the label DFS

570. Find the Missing Number II

class Solution { public : /** * @param n: An integer * @param str: a string with number from 1-n in random order and miss one number * @return: An integer */ int findMissing2 ( int n, string &str) { // write your code here //DFS 分隔符放在一个char还是两个 if (n <= 1 ){ return -1 ; } vector < int > visited(n, 0 ); return dfs(n, str, 0 , visited); } int dfs ( int n, string &str, int start, vector < int > &visited) { //出口 if (start >= str.size()){ vector < int > missed; for ( int i = 1 ; i <= n; i++){ if (!visited[i - 1 ]){ missed.push_back(i); } } if (missed.size() == 1 ){ return missed[ 0 ]; } else { return -1 ; } } // body if (...

426. Restore IP Addresses

Code ( Language :C++) ( Judger :cloudjudge-cluster-5) Edit class Solution { public : /** * @param s: the IP string * @return: All possible valid IP addresses */ vector < string > restoreIpAddresses( string &s) { // write your code here vector < string > res; const int size = s.size(); if (size < 4 || size > 12 ){ return res; } vector < string > resElem; dfs(s, res, resElem, 0 ); return res; } void dfs ( string &s, vector < string > &res, vector < string > &resElem, int start) { int size = s.size(); if (start == s.size()){ if (resElem.size() == 4 ){ res.push_back(resElem[ 0 ] + "." + resElem[ 1 ] + "." + resElem[ 2 ] + "." + resElem[ 3 ]); } return ; } if (resElem.size() > 4 ){ ...

836. Partition to K Equal Sum Subsets

class Solution { public : /** * @param nums: a list of integer * @param k: an integer * @return: return a boolean, denote whether the array can be divided into k non-empty subsets whose sums are all equal */ bool partitiontoEqualSumSubsets ( vector < int > &nums, int k) { // write your code here const int size = nums.size(); if (size < k){ return false ; } if (k == 0 ){ return false ; } int sum = 0 ; for ( auto i : nums){ sum += i; } if (sum % k){ return false ; } vector < int > visited(size, 0 ); return dfs(nums, k, 0 , sum / k, 0 , visited); } bool dfs ( vector < int > &nums, int k, int sum, int target, int start, vector < int > &visited) { if (k == 1 ){ return true ; } if (sum > target){ ...

123. Word Search

Code ( Language :C++) ( Judger :cloudjudge-cluster-5) Edit class Solution { public : /** * @param board: A list of lists of character * @param word: A string * @return: A boolean */ const vector < int > dx = { -1 , 1 , 0 , 0 }; const vector < int > dy = { 0 , 0 , -1 , 1 }; const int dir = 4 ; bool exist ( vector < vector < char >> &board, string &word) { // write your code here const int m = board.size(); if (m == 0 ){ return false ; } const int size = word.size(); if (size == 0 ){ return false ; } const int n = board[ 0 ].size(); for ( int i = 0 ; i < m; i++){ for ( int j = 0 ; j < n; j++){ if (board[i][j] == word[ 0 ]){ vector < vector < int >> visited(m, vector < int >(n, 0 )); //visited...

33. N-Queens

//超时了,花了1个小时吧。最后一个bug查了好久。 class Solution { public : /* * @param n: The number of queens * @return: All distinct solutions */ bool checkValid ( vector < int > pre, int cur) { for ( int i = 0 ; i < pre.size(); i++){ if (pre[i] == cur || pre[i] == cur - (pre.size() - i) || pre[i] == cur + (pre.size() - i)){ return false ; } } return true ; } void dfs ( int n, int rowCnt, vector < vector < int >> &res, vector < int > &resElem) { if (rowCnt == n){ res.push_back(resElem); return ; } for ( int i = 0 ; i < n; i++){ //resElem.push_back(i); 这里不能这样写啊,只有判断valid了,才能放入啊,脑残了! //以后,记住,无论什么题目,先判断valid,再加element。 if (checkValid(resElem, i)){ resElem.push_back(i); dfs(n, rowCnt + 1 , res, resElem); resElem.pop_bac...