Algorithm camp day6

A path whose neutralization is a certain value in a binary tree

Title description
Input the root node of a binary tree and an integer, and print out all paths in which the sum of the node values ​​in the binary tree is the input integer in lexicographic order. The path is defined as a path from the root node of the tree and going down to the nodes passed by the leaf nodes.
Idea:
DFS backtracking algorithm. The essence of the backtracking algorithm is an exhaustive process based on DFS

  1. Add value to the result to be selected
  2. Whether the result to be selected meets the conditions is: add result set (pruning)
  3. DFS depth first
  4. Back to check next

Code:

/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
    
    
public:
    void FindPathDFS(TreeNode *root, int expectNumber,vector<vector<int>>&result,vector<int>vec)
    {
    
    
        if(nullptr == root)
            return;
        vec.push_back(root->val);//添加结果到待选结果中
        //条件更新,剪枝
        expectNumber -= root->val;
        if(expectNumber < 0)
            return;
        if(root->left == nullptr && root->right == nullptr && expectNumber == 0){
    
    
            result.push_back(vec);
        }
        FindPathDFS(root->left, expectNumber,result, vec);
        FindPathDFS(root->right, expectNumber,result, vec);
        vec.pop_back();//回退的过程
    }
    vector<vector<int> > FindPath(TreeNode* root,int expectNumber) {
    
    
        vector<vector<int> > result;
        vector<int> vec;
        FindPathDFS(root, expectNumber,result,vec);
        return result;
    }
};

The arrangement of the character string

Title description
Enter a character string and print out all the characters in the character string in lexicographic order. For example, input the string abc, and print out all the strings abc, abc, bac, bca, cab and cba that can be arranged by the characters a, b, and c in lexicographic order.
Ideas:

Code:

class Solution {
    
    
public:
    void swap(string &str, int i, int j){
    
    
        char temp = str[i];
        str[i] = str[j];
        str[j] = temp;
    }
    bool IsExist(vector<string> &result, string str){
    
    
        auto it = result.begin();
        for(;it != result.end();++it){
    
    
            if(*it == str){
    
    
                return true;
            }
        }
        return false;
    }
    void PermutationHelper(string &str, int start, vector<string>& result){
    
    
        if(start == str.length()-1){
    
    
            if(!IsExist(result, str)){
    
    
                result.push_back(str);
            }
            return;
        }
        for(int i = start;i < str.size();++i){
    
    
            swap(str, start, i);
            PermutationHelper(str, start+1,result);
            swap(str, start, i);
        }
    }
    vector<string> Permutation(string str) {
    
    
        vector<string> result;
        if(str.length()>0 ){
    
    
            PermutationHelper(str, 0, result);
            sort(result.begin(), result.end());
        }
        return result;
    }
};

PS: Today’s question needs to be understood

Optimization: Use set to avoid the step of traversing the array to remove duplicates, remove the swap function in the previous solution, and exchange directly inside.

Code:

class Solution {
    
    
public:
    void PermutationHelper(string &str, int start, set<string>& result){
    
    
        if(start == str.length()-1){
    
    
                result.insert(str);
            return;
        }
        for(int i = start;i < str.size();++i){
    
    
            swap(str[start], str[i]);
            PermutationHelper(str, start+1,result);
            swap(str[start], str[i]);
        }
    }
    vector<string> Permutation(string str) {
    
    
        set<string> result;
        if(str.length()>0 ){
    
    
            PermutationHelper(str, 0, result);
        }
        return vector<string>(result.begin(),result.end());
    }
};

Post a picture of a big brother for easy understanding:
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_35353824/article/details/107575315