LeetCode HOT 100 --- 2021/8/2

二叉树展开为链表

在这里插入图片描述
分析:
  先进行先序遍历,然后根据遍历结果进行链接。
代码:

/**
 * 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<TreeNode*> res;
    void dfs(TreeNode* root) {
        if(root) {
            res.push_back(root);
            dfs(root->left);
            dfs(root->right);
        }
    }
    
    void flatten(TreeNode* root) {
        dfs(root);
        TreeNode* ptr = new TreeNode(0);
        TreeNode* head = ptr;
        for(TreeNode* x : res) {
            ptr->left = NULL;
            ptr->right = x;
            ptr = x;
        }
        root = head->right;
    }
};

最长连续序列

在这里插入图片描述
分析:
  暴力求解:先利用set将所有数据进行去重。然后枚举集合里的元素,接着从枚举的元素开始顺序枚举,直到满足不再连续的条件,记录此次枚举的最大长度即可。
  但是,题目要求时间复杂度为o(n),所以需要进行优化。考虑如下情况:设当前枚举的为x,如果x - 1已经在集合里面,那么就直接跳过,因为枚举从x-1开始时其最大长度肯定是大于从x开始。
代码:

class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
        unordered_set<int> nums_set;
        for(int x : nums) {
            nums_set.insert(x);
        }
        int res = 0;
        for(int x : nums_set) {
            if(nums_set.count(x - 1) == 0) {
                int cur = x;
                int temp = 0;
                while(nums_set.count(cur)) {
                    temp++;
                    cur++;
                }
                res = max(res, temp);
            }
        }
        return res;
    }
};

环形链表 II

在这里插入图片描述
方法一:
  遍历链表,如果当前节点不在集合里就加入,否则说明遇到环的开始处。
代码:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        unordered_set<ListNode*> st;
        ListNode* ptr = head;
        while(ptr) {
            if(st.count(ptr)) {
                return ptr;
            }else {
                st.insert(ptr);
                ptr = ptr->next;
            }
        }
        return NULL;
    }
};

方法二:
  快慢指针。我们使用两个指针,fast与slow。它们起始都位于链表的头部。随后,slow指针每次向后移动一个位置,而fast指针向后移动两个位置。如果链表中存在环,则fast指针最终将再次与slow指针在环中相遇。
  证明见官方题解:
在这里插入图片描述
代码:

class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        ListNode *slow = head, *fast = head;
        while (fast != nullptr) {
            slow = slow->next;
            if (fast->next == nullptr) {
                return nullptr;
            }
            fast = fast->next->next;
            if (fast == slow) {
                ListNode *ptr = head;
                while (ptr != slow) {
                    ptr = ptr->next;
                    slow = slow->next;
                }
                return ptr;
            }
        }
        return nullptr;
    }
};
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Cyril_KI

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值