力扣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
相关推荐
天河归来10 小时前
第三方接口不稳定,如何让核心任务继续完成
java·大数据
Knight_AL10 小时前
Java 文件下载时文件名中的 `#` 被转换成 `%23`?一次 URL 编码与解码问题排查
java·开发语言
ZhengEnCi11 小时前
GPT-6 Astra 爆改 Qwen 权重真有那么神吗:权重空间编程 0.016% 参数的真相
算法·架构
罗西的思考11 小时前
【轻量化复刻大模型 / KIMI K3】KDA(Kimi Delta Attention):并行
人工智能·算法·机器学习
砚底藏山河11 小时前
python量化自定义周期 K 线合成
java·开发语言·数据库·python·金融
猿究院_夏燊12 小时前
MySQL 的四种隔离级别
java·数据库·mysql
码流子13 小时前
移动收费怎么做?手持终端+App+收费中心的架构实践
java·开发语言·架构
霸道流氓气质13 小时前
AI 流式对话与实时聊天平台:SSE 解码竞态、有状态会话隔离与背压取消的 Vue3 + Spring AI 生产级实战
java·人工智能·spring
夜不会漫长13 小时前
C++:红黑树
数据结构·算法
云创智城-yuncitys13 小时前
YunParking 4.0:JDK 17 与 Spring Boot 3.2 的停车云升级
java·开发语言·spring boot