LeetCode 力扣 刷题记录 热题 HOT 100(78,79,94,96,98)题目+算法分析+Cpp解答

GitHub链接:https://github.com/WilliamWuLH/LeetCode

如果你觉得不错可以 ⭐Star 和 Fork ❤

78.Subsets

回溯法:

class Solution {
public:
    vector> ans;
    vector> subsets(vector& nums) {
        vector temp;
        backtrack(0, temp, nums);
        return ans;
    }
    void backtrack(int pos, vector temp, vector& nums){
        ans.push_back(temp);
        for(int i=pos; i

枚举法:

class Solution {
public:
    vector> subsets(vector& nums) {
        vector> ans;
        vector temp;
        int len = nums.size();
        ans.push_back(temp);
        for(int i=0; i

前序遍历:

class Solution {
public:
    vector> ans;
    vector> subsets(vector& nums) {
        vector temp;
        preorder(0, temp, nums);
        return ans;
    }
    void preorder(int pos, vector temp, vector& nums){
        if(pos >= nums.size()){
            ans.push_back(temp);
            return;
        }       
        preorder(pos+1, temp, nums);
        temp.push_back(nums[pos]);
        preorder(pos+1, temp, nums);
    }
};

79.Word Search

深度优先搜索 DFS + 回溯法:

​ 很好理解的回溯法,对就继续走下去,不对就回到之前的状态另找一条路走。

​ 根据题目的意思按照题目的走路方式写代码就对了。

​ 代码长了点,因为是完全按照 DFS 时可能遇到的各种情况进行分类讨论了。

class Solution {
public:
    bool ans = false;
    vector> use;
    bool exist(vector>& board, string word) {
        for(int i = 0; i < board.size(); i++){
            vector temp;
            use.push_back(temp);
            for(int j = 0; j < board[i].size(); j++){
                use[i].push_back(0);
            }
        }
        backtrack(0, 0, 0, board, word);
        return ans;
    }
    void backtrack(int num, int pos1, int pos2, vector>& board, string word){
        if(ans)
            return;
        else if(num >= word.length()){
            ans = true;
            return;
        }
        else if(num == 0){
            for(int i = 0; i < board.size(); i++){
                for(int j = 0; j < board[i].size(); j++){
                    if(board[i][j] == word[0]){
                        use[i][j] = 1;
                        backtrack(1, i, j, board, word);
                        use[i][j] = 0;
                    }
                }
            }
        }
        else{
            if(pos1 > 0 && board[pos1-1][pos2] == word[num] && use[pos1-1][pos2] != 1){
                use[pos1-1][pos2] = 1;
                backtrack(num+1, pos1-1, pos2, board, word);
                use[pos1-1][pos2] = 0;
            }
            if(pos1 < board.size()-1 && board[pos1+1][pos2] == word[num] 
                                    && use[pos1+1][pos2] != 1){
                use[pos1+1][pos2] = 1;
                backtrack(num+1, pos1+1, pos2, board, word);
                use[pos1+1][pos2] = 0;
            }
            if(pos2 > 0 && board[pos1][pos2-1] == word[num] && use[pos1][pos2-1] != 1){
                use[pos1][pos2-1] = 1;
                backtrack(num+1, pos1, pos2-1, board, word);
                use[pos1][pos2-1] = 0;
            }
            if(pos2 < board[pos1].size()-1 && board[pos1][pos2+1] == word[num] 
                                    && use[pos1][pos2+1] != 1){
                use[pos1][pos2+1] = 1;
                backtrack(num+1, pos1, pos2+1, board, word);
                use[pos1][pos2+1] = 0;
            }
        }
        return;
    }
};

94.Binary Tree Inorder Traversal

二叉树的中序遍历:

​ 送分题。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector inorderTraversal(TreeNode* root) {
        vector ans;
        inorder(root, ans);
        return ans;
    }
    void inorder(TreeNode* root, vector& ans){
        if(root == NULL)
            return;
        inorder(root->left, ans);
        ans.push_back(root->val);
        inorder(root->right, ans);
    }
};

96.Unique Binary Search Trees

数学题 + 记录 + 相乘 + 相加:

​ 记录下从 0 到 n 之间的各个数 所可能构造出来的二叉查找树(二叉搜索树)的个数。

​ 记录下来的原因是:之后的二叉查找树个数计算会用到之前的结果。

class Solution {
public:
    int numTrees(int n) {
        long table[1000];
        table[0] = 1;
        table[1] = 1;
        for(int i=2; i<=n; i++){
            table[i] = 0;
            for(int j=1; j<=i; j++){
                table[i] += (table[j-1] * table[i-j]);
            }
        }
        return table[n];
    }
};

98.Validate Binary Search Tree

关于二叉查找树的中序遍历:

​ 根据二叉查找树的性质,对二叉查找树进行中序遍历可以得到一个有序的序列。

​ 通过判断进行中序遍历得到的序列是否严格有序,可以得知这个二叉查找树是否正确(有效)。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isValidBST(TreeNode* root) {
        vector nums;
        inorder(root, nums);
        int len = nums.size();
        for(int i=1; i& nums){
        if(root == NULL)
            return;
        inorder(root->left, nums);
        nums.push_back(root->val);
        inorder(root->right, nums);
    }
};

你可能感兴趣的:(算法,LeetCode,C++)