当前位置: 首页 > news >正文

8.5 位|归并|递归

 

lcr152

递归找m分界点,比较构造

 


class Solution {
vector<int> postorder;
public:
bool verifyTreeOrder(vector<int>& postorder) {
this->postorder = postorder;
return recur(0, postorder.size() - 1);
}

    bool recur(int left, int right)

{
if (left >= right) return true;

int root = postorder[right];
int index = left;

while (postorder[index] < root) index++;
int m = index;

while (index < right) {
  if (postorder[index] < root) return false;
index++;
}

return recur(left, m - 1) && recur(m, right - 1);
}
};

 

 

lc148.快慢+归并

if(!head || !head->next) return head;

 

 

ListNode* fast=head->next;

        while(fast && fast->next)

 

first=sortList(first); //!!!!
second=sortList(second);

merge

if(left->val < right->val)

class Solution {
//归并
public:
ListNode* sortList(ListNode* head) 
{
if(!head || !head->next) return head;

 

        ListNode* slow=head;
ListNode* fast=head->next;

        while(fast && fast->next)
{
slow=slow->next;
fast=fast->next->next;
}

//归
ListNode* second=slow->next;
slow->next=nullptr;
ListNode* first=head;

        first=sortList(first); //!!!!
second=sortList(second);

        return merge(first,second);
}

//并
ListNode* merge(ListNode* left,ListNode* right)
{
ListNode* dummy=new ListNode(0);
ListNode* tail=dummy;

        while(left && right)
{
if(left->val < right->val)
{
tail->next=left;
left=left->next;
}

            else
{
tail->next=right;
right=right->next;
}
tail=tail->next;
}

        //剩余部分
if(left) tail->next=left;
if(right) tail->next=right;

        return dummy->next;
}
};

 

 

lc106

中序定区间长,后序找根建树

class Solution {

    //中序存hash

    unordered_map<int,int> hash;

    vector<int> postorder;

 

public:

    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) 

    {

        this->postorder=postorder;

        int n=inorder.size();

 

        for(int i=0;i<n;i++)

        {

            hash[inorder[i]]=i;

        }

        return dfs(n-1,0,n-1);   

    }

//i 依据后序,定root pos

//l ,r 依据中序,定区间长度,辅助找root pos 

 

    TreeNode* dfs(int i,int l,int r)

    {

        if(l>r) return nullptr;

 

        int num=postorder[i];

        TreeNode* root=new TreeNode(num);

 

        int idx=hash[num];

        root->right=dfs(i-1,idx+1,r);

        root->left=dfs(i-1-(r-idx),l,idx-1);//跳过 根 右,到达左子树根

 

        return root;

    }

};

 

 

lc2917

将合格位设为1:ret |= (1 << i);

class Solution

{
public:
int findKOr(vector<int>& nums, int k) {
vector<int> cnt(32, 0); 
for (auto& n : nums) {
int i = 0;
while (i < 32) { 
if (n & (1 << i)) {
cnt[i]++;
}
i++;
}
}
int ret = 0;
for (int i = 0; i < 32; i++) {
if (cnt[i] >= k) { 
ret |= (1 << i);
}
}
return ret;
}
};

 

http://www.lryc.cn/news/610525.html

相关文章:

  • 腾讯云CodeBuddy AI IDE+CloudBase AI ToolKit打造理财小助手网页
  • C++ - 基于多设计模式下的同步异步日志系统(11w字)
  • 使用ProxySql实现MySQL的读写分离
  • 【模电笔记】—— 直流稳压电源——整流、滤波电路
  • C++返回值优化(RVO):高效返回对象的艺术
  • LINUX 85 SHElL if else 前瞻 实例
  • 解锁n8n:开启自动化工作流的无限可能
  • 数据挖掘,到底是在挖掘什么?
  • Leetcode-2080区间内查询数字的频率
  • 417页PDF | 2025年“人工智能+”行业标杆案例荟萃
  • 机器学习——集成学习(Ensemble Learning)详解:原理、方法与实战应用
  • 深度拆解Dify:开源LLM开发平台的架构密码与技术突围
  • 服务器端口连通性的测试工具和方法
  • ApacheCon Asia 2025 中国开源年度报告:Apache Doris 国内第一
  • Spring Boot 整合 Thymeleaf
  • 全球氨运输罐行业发展现状及趋势分析报告
  • makefile的使用与双向链表
  • Docker Compose管理新范式:可视化控制台结合cpolar提升容器编排效率?
  • Docker使用的常见问题
  • 解决微信小程序中camera组件被view事件穿透触发对焦以及camera的bindtap事件
  • 性能优化篇:SQL数据库查表速度优化
  • JAVA无人共享球杆柜系统球杆柜租赁系统源码支持微信小程序
  • TortoiseGit配置SSH Key或Putty Key
  • W3D引擎游戏开发----从入门到精通【22】
  • 微信小程序功能实现:页面导航与跳转
  • AI产品经理如何理解和应用Transformer架构,以提升产品的技术能力和用户体验?
  • SpringBoot基础复习
  • 06 基于sklearn的机械学习-欠拟合、过拟合、正则化、逻辑回归、k-means算法
  • 如何基于MQ实现分布式事务
  • 机器学习(13):逻辑回归