• LeetCode力扣(剑指offer 36-39)36. 二叉搜索树与双向链表37. 序列化二叉树38. 字符串的排列39. 数组中出现次数超过一半的数字


    剑指 Offer 36. 二叉搜索树与双向链表

    题解:

    思路很简单,就是一个中序遍历,一边遍历一边把节点连接起来。

    代码:

    1. class Solution {
    2. public:
    3. Node *pre,*head;
    4. void dfs(Node* root){
    5. if(root==nullptr) return;
    6. dfs(root->left);
    7. if(pre==nullptr) head=root;//这是个头节点
    8. else
    9. pre->right=root;
    10. root->left=pre;
    11. pre=root;
    12. dfs(root->right);
    13. }
    14. Node* treeToDoublyList(Node* root) {
    15. if(root == nullptr) return root;
    16. dfs(root);
    17. head->left=pre;
    18. pre->right=head;
    19. return head;
    20. }
    21. };

    结果:

    剑指 Offer 37. 序列化二叉树

    题解:

    序列化一般采用前序,中序,后序,这里采用前序。

    序列化,用前序遍历将二叉树的节点保存到一个字符串中,每个节点之间用’,‘隔开。值得注意的是,在遍历的过程中需要将二叉树看成一颗满二叉树,空节点用’null‘表示。

    反序列化,将字符串放在队列que中,用’,‘判断并推入到队列中。通过前序遍历,一边前序遍历一边建立二叉树节点。

    代码:

    1. class Codec {
    2. public:
    3. // Encodes a tree to a single string.
    4. string serialize(TreeNode* root) {
    5. if(root==nullptr) return "null,";
    6. string str=to_string(root->val)+',' ;
    7. str+=serialize(root->left);
    8. str+=serialize(root->right);
    9. return str;
    10. }
    11. // Decodes your encoded data to tree.
    12. TreeNode* deserialize(string data) {
    13. queue<string> que;
    14. string str;
    15. for(auto i:data){
    16. if(i==','){
    17. que.push(str);
    18. str.clear();
    19. }
    20. else
    21. str.push_back(i);
    22. }
    23. return rdeserialize(que);
    24. }
    25. TreeNode* rdeserialize(queue<string> &que){
    26. if (que.front() == "null") {
    27. que.pop();
    28. return nullptr;
    29. }
    30. TreeNode* root = new TreeNode(stoi(que.front()));
    31. que.pop();
    32. root->left = rdeserialize(que);
    33. root->right = rdeserialize(que);
    34. return root;
    35. }
    36. };

    结果:

     

    剑指 Offer 38. 字符串的排列

    题解:

    最关键是去重复,因为有重复的字符。可以用一个标志位,判断当前的字符是否出现在前面的字符串中。

    代码:

    1. class Solution {
    2. public:
    3. vector<string> res;
    4. void dfs(string &s,int sz,int pos){
    5. if(pos==sz) res.push_back(s);
    6. for(int i=pos;i<sz;++i){
    7. bool flag=true;
    8. for(int j=pos;j<i;++j){
    9. if(s[j]==s[i]) flag=false;
    10. }
    11. if(flag){
    12. swap(s[i],s[pos]);
    13. dfs(s,sz,pos+1);
    14. swap(s[i],s[pos]);
    15. }
    16. }
    17. }
    18. vector<string> permutation(string s) {
    19. dfs(s,s.size(),0);
    20. return res;
    21. }
    22. };

    结果:

     

    剑指 Offer 39. 数组中出现次数超过一半的数字

    数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。

    你可以假设数组是非空的,并且给定的数组总是存在多数元素。

    题解:

    方法一:排序

    如果重复字符有超过一半的,排序后必然会在数组中间位置。

    代码:

    1. class Solution {
    2. public:
    3. int majorityElement(vector<int>& nums) {
    4. sort(nums.begin(),nums.end());
    5. return nums[nums.size()/2];
    6. }
    7. };

    结果:

     方法二:哈希

    用哈希存储,一旦这个值大于数组长度的一半就返回该值。

    代码:

    1. class Solution {
    2. public:
    3. int majorityElement(vector<int>& nums) {
    4. unordered_map<int,int>map;
    5. for(auto i:nums){
    6. ++map[i];
    7. if(map[i]>nums.size()/2) return i;
    8. }
    9. return -1;
    10. }
    11. };

    结果:

  • 相关阅读:
    学会2种方法,小白也能快速产出标准的Axure原型
    互联网轻量级框架整合之MyBatis动态SQL
    mongo大数据处理优化
    QListView的使用
    计算机视觉中的对象跟踪(完整指南)
    LightningChart.NET全新版本正式发布——助力打造超快数据可视化时代
    uni-app:实现等待加载功能
    【Python零基础入门篇 · 10】:集合的相关操作
    深度网络架构的设计技巧(二)之BoT:Bottleneck Transformers for Visual Recognition
    英雄联盟|王者|穿越火线 bgm AI配乐大赛分享
  • 原文地址:https://blog.csdn.net/u011895157/article/details/125607195