IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> LeetCode HOT 100 --- 2021/8/1 -> 正文阅读

[数据结构与算法]LeetCode HOT 100 --- 2021/8/1

子集

在这里插入图片描述
方法一:
??位运算,如果数组的长度为3,则枚举000到111,000表示空集,111表示集合本身。
代码:

class Solution {
public:
    vector<vector<int>> subsets(vector<int>& nums) {
        vector<vector<int>> res;
        vector<int> temp;
        int n = nums.size();
        for(int i = 0; i < (1 << n); i++) {
            temp.clear();
            for(int j = 0; j < n; j++) {
                if(i & (1 << j)) {
                    temp.push_back(nums[j]);
                }
            }
            res.push_back(temp);
        }
        return res;
    }
};

方法二:
??递归实现枚举,背模板。
代码:

class Solution {
public:
    vector<int> t;
    vector<vector<int>> ans;

    void dfs(int cur, vector<int>& nums) {
        if (cur == nums.size()) {
            ans.push_back(t);
            return;
        }
        t.push_back(nums[cur]);  //选择下一个
        dfs(cur + 1, nums);
        t.pop_back();  //不选择下一个
        dfs(cur + 1, nums);
    }

    vector<vector<int>> subsets(vector<int>& nums) {
        dfs(0, nums);
        return ans;
    }
};

单词搜索

剑指 Offer 12. 矩阵中的路径

不同的二叉搜索树

在这里插入图片描述
分析:
??卡特兰数,详见:算法中的数学—卡特兰数(解析+代码实现)

代码:

class Solution {
public:
    int numTrees(int n) {
        vector<long long> dp(20);
        dp[0] = dp[1] = 1;
        for(int i = 2; i <= 19; i++) {
            dp[i] = dp[i - 1] * (4 * i - 2) / (i + 1);
        }
        return dp[n];
    }
};

验证二叉搜索树

在这里插入图片描述
方法一:
??二叉搜索树的中序遍历是一个递增序列。
代码:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> res;
    void dfs(TreeNode* root) {
        if(root) {
            dfs(root->left);
            res.push_back(root->val);
            dfs(root->right);
        }
    }
    
    bool isValidBST(TreeNode* root) {
        dfs(root);
        for(int i = 0; i < res.size() - 1; i++) {
            if(res[i] < res[i + 1]) {
                continue;
            }else {
                return false;
            }
        }
        return true;
    }
};

方法二:
??参考官方题解:递归。
代码:

class Solution {
public:
    bool helper(TreeNode* root, long long lower, long long upper) {
        if (root == nullptr) {
            return true;
        }
        if (root -> val <= lower || root -> val >= upper) {
            return false;
        }
        return helper(root -> left, lower, root -> val) && helper(root -> right, root -> val, upper);
    }
    bool isValidBST(TreeNode* root) {
        return helper(root, LONG_MIN, LONG_MAX);
    }
};

二叉树的层序遍历

在这里插入图片描述
分析:
??二叉树的层序遍历,背模板。
代码:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> res;
        if(root == nullptr) {
            return res;
        }
        queue<TreeNode*> que;
        que.push(root);
        vector<int> temp;
        while(!que.empty()) {
            int n = que.size();
            temp.clear();
            for(int i = 0; i < n; i++) {
                TreeNode* t = que.front();
                que.pop();
                temp.push_back(t->val);
                if(t->left) {
                    que.push(t->left);
                }
                if(t->right) {
                    que.push(t->right);
                }
            }
            res.push_back(temp);
        }
        return res;
    }
};

从前序与中序遍历序列构造二叉树

在这里插入图片描述
分析:
??递归构造二叉树。
代码:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
private:
    unordered_map<int, int> mp;

public:
    TreeNode* create(vector<int>& pre, vector<int>& in, int x1, int x2, int x3, int x4) {
        if(x1 > x2) {
            return NULL;
        }
        int root_val = pre[x1];
        //创建根
        TreeNode* root = new TreeNode(root_val);
        int x = mp[root_val];  //中序遍历中的位置
        int y = x - x3 + x1;
        int z = x2 - x4 + x + 1;
        //创建左子树
        root->left = create(pre, in, x1 + 1, y, x3, x - 1);
        root->right = create(pre, in, z, x2, x + 1, x4);
        return root; 
    }
    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
        int n = preorder.size();
        // 构造哈希映射,快速定位根节点
        for (int i = 0; i < n; ++i) {
            mp[inorder[i]] = i;
        }
        return create(preorder, inorder, 0, n - 1, 0, n - 1);
    }
};
  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2021-08-02 11:02:29  更:2021-08-02 11:05:06 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/10 12:07:46-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码