Backtracking - 46. 47.有问题 77. 89. 131. Stack - 103. 144. 150. 155. 173

46Permutations

Given a collection of distinct integers, return all possible permutations.

Example:

Input: [1,2,3]
Output:
[
  [1,2,3],
  [1,3,2],
  [2,1,3],
  [2,3,1],
  [3,1,2],
  [3,2,1]
]

提示:当pos = 0时,i遍历nums与pos上的元素交换,为固定这一元素。

答案:

class Solution {
public:
    vector<vector<int>> permute(vector<int>& nums) {
        vector<vector<int>> result;
        dfs(nums, 0, result);
        return result;
    }
private:
     void dfs(vector<int> &nums, int pos, vector<vector<int>> &result){
        if(pos == nums.size() - 1){
            result.push_back(nums);
        }else{
                for(int i=pos; i<nums.size(); i++){
                    swap(nums[i], nums[pos]);
                    dfs(nums, pos+1, result);
                    swap(nums[i], nums[pos]);
                }
        }
     }
};

47Permutations II

Given a collection of numbers that might contain duplicates, return all possible unique permutations.

Example:

Input: [1,1,2]
Output:
[
  [1,1,2],
  [1,2,1],
  [2,1,1]
]

提示:我的答案有问题但不知道为什么,将dfs函数的& 与 第二个swap去掉,修改if语句为

if (i != k && num[i] == num[k]) continue;

就可以通过了

答案:

class Solution {
public:
    vector<vector<int>> permuteUnique(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        vector<vector<int>> result;
        dfs(nums, 0, result);
        sort(result.begin(), result.end());
        return result;
    }
private:
    void dfs(vector<int> &nums, int pos, vector<vector<int>> &result){
        if(pos >= nums.size() - 1){
            result.push_back(nums);
        }else{
                for(int i = pos; i < nums.size(); i++){
                    if(i != pos && (nums[i] == nums[pos] || nums[i] == nums[i - 1]))continue;
                    swap(nums[i], nums[pos]);
                    dfs(nums, pos+1, result);
                    swap(nums[i], nums[pos]);
                }
        }
     }
};

77Combinations

Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.

Example:

Input: n = 4, k = 2
Output:
[
  [2,4],
  [3,4],
  [2,3],
  [1,2],
  [1,3],
  [1,4],
]

提示:迭代法n=5,k=4  1235->1236,1246,1244->1245

          回溯法

答案:

class Solution {
public:
    vector<vector<int>> combine(int n, int k) {
        vector<vector<int>> result;
		int i = 0;
		vector<int> p(k, 0);
		while (i >= 0) {
			p[i]++;
			if (p[i] > n) --i; //最后一位数越界
			else if (i == k - 1) result.push_back(p); //i指向最后一位数push
			else {     
			    ++i;
			    p[i] = p[i - 1];
			}
		}
		return result;
    }
};
class Solution {
public:
    void myCombine(vector<vector<int>>& res, vector<int>& cur, int pos, int count, int k, int n) {
        if (k == 0) {
            res.push_back(cur);
            return;
        }
        for (int i = pos; i <= n; i++) {
            cur[count] = i;
            myCombine(res, cur, i + 1, count + 1, k - 1, n);
        }
    }
    vector<vector<int>> combine(int n, int k) {
        vector<vector<int>> res;
        vector<int> cur(k, 0);
        myCombine(res, cur, 1, 0, k, n);
        return res;
    }
};

89Gray Code

The gray code is a binary numeral system where two successive values differ in only one bit.

Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0.

For example, given n = 2, return [0,1,3,2]. Its gray code sequence is:

00 - 0
01 - 1
11 - 3
10 - 2

Note:
For a given n, a gray code sequence is not uniquely defined.

For example, [0,2,3,1] is also a valid gray code sequence according to the above definition.

For now, the judge is able to judge based on one instance of gray code sequence. Sorry about that.

提示:The idea is simple. G(i) = i^ (i/2).

答案:

class Solution {
public:
    vector<int> grayCode(int n) {
        vector<int> result;
        for(int i = 0; i < 1<<n; i++) result.push_back(i ^ i>>1);
        return result;
    }
};

131Palindrome Partitioning

Given a string s, partition s such that every substring of the partition is a palindrome.

Return all possible palindrome partitioning of s.

Example:

Input: "aab"
Output:
[
  ["aa","b"],
  ["a","a","b"]
]

提示:

答案:

class Solution {
public:
    vector<vector<string>> partition(string s) {
        vector<vector<string>> res;
        vector<string> tmp;
        getPartition(s, 0, tmp, res);
        return res;
    }
private: 
    void getPartition(string& s, int idx, vector<string>& tmp, vector<vector<string>>& res) {
        if (idx == s.length()) {
            res.push_back(tmp);
            return;
        }
        for (int i = idx, n = s.length(); i < n; i++) {
            int l = idx, r = i;
            while (l < r && s[l] == s[r]) l++, r--;
            if (l >= r) {
                tmp.push_back(s.substr(idx, i - idx + 1));
                getPartition(s, i + 1, tmp, res);
                tmp.pop_back();
            }
        }
    }
};

提示:

答案:

提示:

答案:

提示:

答案:

提示:

答案:

提示:

答案:

提示:

答案:

提示:

答案:

提示:

答案:

提示:

答案:

提示:

答案:

提示:

答案:

提示:

答案:


猜你喜欢

转载自blog.csdn.net/qq_27012963/article/details/80446271