力扣hot100 31-40记录

31-40

https://leetcode.cn/studyplan/top-100-liked/

cpp 复制代码
class Solution {
public:
    pair<ListNode*, ListNode*> myreverse(ListNode* head, ListNode* tail){
        ListNode* prev = head, *curr = head;
        while(prev != tail){
            ListNode* nex = curr->next;
            curr->next = prev;
            prev = curr;
            curr = nex;
        }
        return {tail, head};
    }
    ListNode* reverseKGroup(ListNode* head, int k) {
        ListNode* hair = new ListNode(0);
        hair->next = head;
        ListNode* pre = hair;

        while(head){
            ListNode* tail = pre;
            for(int i = 0; i< k; i++){
                tail = tail->next;
                if(!tail) return hair->next;
            }
            ListNode* nex = tail->next;
            tie(head, tail) = myreverse(head, tail);
            pre->next = head;
            tail->next = nex;
            pre = tail;
            head = pre->next;
        }
        return hair->next;
    }
};
//31
cpp 复制代码
class Solution {
public:
    unordered_map<Node*, Node*> cachednode;

    Node* copyRandomList(Node* head) {
        if(head == nullptr) return nullptr;

        if(!cachednode.count(head)){
            Node* headnew = new Node(head->val);
            cachednode[head] = headnew;
            headnew->next = copyRandomList(head->next);
            headnew->random = copyRandomList(head->random);
        }
        return cachednode[head];
    }
};
//32
cpp 复制代码
class Solution {
public:
    ListNode* sortList(ListNode* head) {
        auto cur = head;
        int n=0;
        while(cur){cur=cur->next;n++;}
        ListNode* dummyHead = new ListNode(0);
        dummyHead->next=head;
        for(int k=1;k<n;k<<=1){
            cur=dummyHead->next;
            auto ans = dummyHead;
            while(cur){
                auto l1=cur;
                auto l2=split(cur, k);
                cur = split(l2, k);
                ans->next=merge(l1, l2);
                while(ans->next) ans=ans->next;
            }
        }
        return dummyHead->next;
    }
    ListNode* split(ListNode* head, int n){
        ListNode* cur = head;
        while(--n&&cur){
            cur=cur->next;
        }
        if(!cur) return nullptr;
        ListNode* tmp=cur->next;
        cur->next=nullptr;
        return tmp;
    }
    ListNode* merge(ListNode* l1, ListNode* l2){
        ListNode* dummyHead = new ListNode(0);
        auto cur=dummyHead;
        while(l1&&l2){
            if(l1->val<=l2->val){
                cur->next=l1;
                l1=l1->next;
                // cur->next->next=nullptr;
                cur=cur->next;
            }else{
                cur->next=l2;
                l2=l2->next;
                // cur->next->next=nullptr;
                cur=cur->next;
            }
        }
        if(l1) cur->next=l1;
        else cur->next=l2;
        return dummyHead->next;
    }
};
//33
cpp 复制代码
class Solution {
public:
    ListNode* mergetwolsit(ListNode* list1, ListNode* list2){
        ListNode* dummy = new ListNode(0);
        ListNode* cur = dummy;
        while(list1 && list2){
            if(list1->val > list2->val){
                cur->next = list2;
                list2 = list2->next;
            }else{
                cur->next = list1;
                list1 = list1->next;
            }
            cur = cur->next;
        }
        cur->next = list1 ? list1:list2;
        return dummy->next;
    }

    ListNode* mergeKLists(vector<ListNode*>& lists) {
        ListNode * ans = nullptr;
        for(int i = 0; i<lists.size(); i++){
            ans = mergetwolsit(ans, lists[i]);
        }
        return ans;
    }
};
//34
cpp 复制代码
class LRUCache {
public:
    int cap;
    list<pair<int, int>> datalists;
    unordered_map<int, list<pair<int,int>>::iterator> mp;
    LRUCache(int capacity) {
        cap = capacity;
    }
    
    int get(int key) {
        if(mp.find(key)!=mp.end()){
            int value = mp[key]->second;
            datalists.erase(mp[key]);
            datalists.push_front(make_pair(key, value));
            // 更新哈希表中的映射
            mp[key] = datalists.begin();
            return value;
        }else{
            return -1;
        }
    }
    
    void put(int key, int value) {
        if(mp.find(key)!=mp.end()){
            datalists.erase(mp[key]);
        }else if(datalists.size() == cap){
            mp.erase(datalists.back().first);
            datalists.pop_back();
        }
        datalists.push_front(make_pair(key, value));
        mp[key] = datalists.begin();
    }
};
//35
cpp 复制代码
class Solution {
public:
void inorder(TreeNode* root,vector<int>& res){
    if(!root) return;
    inorder(root->left, res);
    res.push_back(root->val);
    inorder(root->right, res);    
}
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> res;
        inorder(root, res);
        return res;
    }
};//36
cpp 复制代码
class Solution {
public:
    int maxDepth(TreeNode* root) {
        if(!root) return 0;
        return max(maxDepth(root->left), maxDepth(root->right)) + 1;
    }
};//37
cpp 复制代码
class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if(!root) return root;
        TreeNode* left = invertTree(root->left);
        TreeNode* right = invertTree(root->right);

        root->left = right;
        root->right = left;
        return root;
    }
};//38
cpp 复制代码
class Solution {
public:
    bool check(TreeNode *p, TreeNode *q){
        if(!p && !q) return true;
        if(!p || !q || p->val != q->val) return false;
        return check(p->left, q->right) && check(p->right, q->left);
    }
    bool isSymmetric(TreeNode* root) {
        if(!root) return  true;
        return check(root->left,root->right);
    }
};//39
cpp 复制代码
class Solution {
int ans;
int depth(TreeNode *rt){
    if(!rt) return 0;
    int ld = depth(rt->left);
    int rd = depth(rt->right);
    ans = max(ans, ld+rd+1);
    return max(rd, ld)+1;
}
public:
    int diameterOfBinaryTree(TreeNode* root) {
        ans=1;
        depth(root);
        return ans-1;
    }
};//40
相关推荐
一个public的class1 小时前
什么是 Java 泛型
java·开发语言·后端
士别三日&&当刮目相看1 小时前
JAVA学习*Object类
java·开发语言·学习
invincible_Tang1 小时前
R格式 (15届B) 高精度
开发语言·算法·r语言
快来卷java1 小时前
MySQL篇(一):慢查询定位及索引、B树相关知识详解
java·数据结构·b树·mysql·adb
独好紫罗兰2 小时前
洛谷题单2-P5715 【深基3.例8】三位数排序-python-流程图重构
开发语言·python·算法
凸头2 小时前
I/O多路复用 + Reactor和Proactor + 一致性哈希
java·哈希算法
序属秋秋秋2 小时前
算法基础_基础算法【高精度 + 前缀和 + 差分 + 双指针】
c语言·c++·学习·算法
玉树临风ives2 小时前
leetcode 2360 图中最长的环 题解
算法·leetcode·深度优先·图论
慵懒学者2 小时前
15 网络编程:三要素(IP地址、端口、协议)、UDP通信实现和TCP通信实现 (黑马Java视频笔记)
java·网络·笔记·tcp/ip·udp
anda01092 小时前
11-leveldb compact原理和性能优化
java·开发语言·性能优化