7.15题目总结

 

输入一颗二叉树的根节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。(注意: 在返回值的list中,数组长度大的数组靠前)

递归先序遍历树, 把结点加入路径;若该结点是叶子结点则比较当前路径和是否等于期待和;弹出结点,每一轮递归返回到父结点时,当前路径也应该回退一个结点

    //递归先序遍历树, 把结点加入路径。
    //若该结点是叶子结点则比较当前路径和是否等于期待和。
    //弹出结点,每一轮递归返回到父结点时,当前路径也应该回退一个结点
    private ArrayList<ArrayList<Integer>> listAll = new ArrayList<>();
    private ArrayList<Integer> list = new ArrayList<>();

    public ArrayList<ArrayList<Integer>> FindPath1(Node root, int target) {
        if (root == null) return listAll;
        list.add(root.value);
        target -= root.value;
        if (target == 0 && root.left == null && root.right == null) {
            listAll.add(new ArrayList<>(list));//符合要求的就加入到其中
        }
        FindPath1(root.left, target);
        FindPath1(root.right, target);
        //无论当前路径是否加出了target,必须去掉最后一个,然后返回父节点,
        //去查找另一条路径,最终的path肯定为null
        list.remove(list.size() - 1);
        return listAll;

    }

输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)

遍历一遍,按链表next顺序将每一个节点依次复制,复制的节点在原节点的next(插入到原链表中);遍历一遍,确定每一个节点的random指针;遍历一遍,断掉复制的连接,生成两个新的链表

关键在于时间复杂度o(n)和空间复杂度o(c)
 

public class CloneComplexLinkedlistTest {
//    输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个
//    节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。
//    (注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)
    @Test
    public void test1(){
        RandomListNode node = new RandomListNode(1);
        RandomListNode node1 = new RandomListNode(2);
        RandomListNode node2 = new RandomListNode(3);
        RandomListNode node3 = new RandomListNode(4);
        RandomListNode node4 = new RandomListNode(5);
        RandomListNode node5 = new RandomListNode(6);
        RandomListNode node6 = new RandomListNode(7);
        RandomListNode node7 = new RandomListNode(8);
        node.next =  node1;
        node1.next =  node2;
        node2.next =  node3;
        node3.next =  node4;
        node4.next =  node5;
        node5.next =  node6;
        node6.next =  node7;
        Clone(node);
    }
    public RandomListNode Clone(RandomListNode pHead) {
        clone1(pHead);
        clone2(pHead);
        return clone3(pHead);
    }
    public void clone1(RandomListNode pHead){//遍历一遍
        //按链表next顺序将每一个节点依次复制,复制的节点在原节点的next(插入到原链表中)
        RandomListNode head = pHead;
        while (head != null){
            RandomListNode node = new RandomListNode(pHead.label);
            node.label = head.label;
            node.next = head.next;
            node.random = null;
            head.next = node;

            head = node.next;
        }
    }
    public void clone2(RandomListNode pHead){//遍历一遍,确定每一个节点的random指针
        RandomListNode head = pHead;
        while (head != null){
            RandomListNode node = head.next;
            if(head.random != null){
                node.random = head.random.next;
            }
            head = node.next;
        }
    }
    public RandomListNode clone3(RandomListNode pHead){//遍历一遍,断掉复制的连接,生成两个新的链表
        RandomListNode head = pHead;
        RandomListNode cloneHead = null;
        RandomListNode node = null;
        if(head != null){
            cloneHead = node = head.next;
            head.next = node.next;
            head = head.next;//此时,head为原始head.next,cloneHead为新得到的复制的链表头head
        }
        while (head != null){
            node.next = head.next;
            node = node.next;

            head.next = node.next;//head不为空,则head.next一定不为空(为复制的元素)

            head = head.next;//注意细节
        }
        return cloneHead;
    }
}

class RandomListNode {
    int label;
    RandomListNode next = null;
    RandomListNode random = null;

    RandomListNode(int label) {
        this.label = label;
    }
}

 

 

内容概要:本文介绍了一个基于冠豪猪优化算法(CPO)的无人机三维路径规划项目,利用Python实现了在复杂三维环境中为无人机规划安全、高效、低能耗飞行路径的完整解决方案。项目涵盖空间环境建模、无人机动力学约束、路径编码、多目标代价函数设计以及CPO算法的核心实现。通过体素网格建模、动态障碍物处理、路径平滑技术和多约束融合机制,系统能够在高维、密集障碍环境下快速搜索出满足飞行可行性、安全性与能效最优的路径,并支持在线重规划以适应动态环境变化。文中还提供了关键模块的代码示例,包括环境建模、路径评估和CPO优化流程。; 适合人群:具备一定Python编程基础和优化算法基础知识,从事无人机、智能机器人、路径规划或智能优化算法研究的相关科研人员与工程技术人员,尤其适合研究生及有一定工作经验的研发工程师。; 使用场景及目标:①应用于复杂三维环境下的无人机自主导航与避障;②研究智能优化算法(如CPO)在路径规划中的实际部署与性能优化;③实现多目标(路径最短、能耗最低、安全性最高)耦合条件下的工程化路径求解;④构建可扩展的智能无人系统决策框架。; 阅读建议:建议结合文中模型架构与代码示例进行实践运行,重点关注目标函数设计、CPO算法改进策略与约束处理机制,宜在仿真环境中测试不同场景以深入理解算法行为与系统鲁棒性。
11-29
![](https://cdn.luogu.com.cn/upload/image_hosting/unknowndomain/2025/11/28/IMG20251128143506.jpg) ### 题目重述 利用消息队列实现一个简单的客户端/服务器应用:多个客户端进程可并发地向服务器进程发送消息,服务器收到后向每个客户端发送回执。例如,客户端(PID=1234)发送 `"hello"`,服务器回执 `"receipt of<hello> to Process 1234"`。 要求编写服务器和客户端程序,使用消息队列进行通信。 --- ### 给出答案(答案为带注释的代码块) #### 服务端程序:`server.c` ```c #include <stdio.h> #include <stdlib.h> #include <sys/msg.h> #include <string.h> #include <unistd.h> #define SERVER_KEY 1000 #define MAX_MSG_SIZE 128 // 消息结构体 struct msg_buffer { long mtype; // 消息类型 long client_pid; // 客户端 PID char text[MAX_MSG_SIZE]; // 消息正文 }; int main() { key_t key = SERVER_KEY; int msgid = msgget(key, 0666 | IPC_CREAT); if (msgid == -1) { perror("msgget failed"); exit(1); } printf("Server started, waiting for messages...\n"); struct msg_buffer msg; while (1) { // 接收任意类型为1的消息(来自客户端) if (msgrcv(msgid, &msg, sizeof(long) + MAX_MSG_SIZE, 1, 0) == -1) { perror("msgrcv failed"); break; } printf("Server received from PID %ld: %s\n", msg.client_pid, msg.text); // 构造回执消息 char receipt[128]; snprintf(receipt, sizeof(receipt), "receipt of<%s> to Process %ld", msg.text, msg.client_pid); // 根据客户端 PID 查找其专属消息队列 int client_msgid = msgget((key_t)(msg.client_pid % 65535), 0666); if (client_msgid != -1) { struct msg_buffer reply; reply.mtype = 1; // 回复类型 strncpy(reply.text, receipt, MAX_MSG_SIZE); msgsnd(client_msgid, &reply, strlen(reply.text) + 1, 0); } else { printf("No reply queue for PID %ld\n", msg.client_pid); } } // 清理消息队列 msgctl(msgid, IPC_RMID, NULL); return 0; } ``` --- #### 客户端程序:`client.c` ```c #include <stdio.h> #include <stdlib.h> #include <sys/msg.h> #include <string.h> #include <unistd.h> #define SERVER_KEY 1000 #define MAX_MSG_SIZE 128 // 消息结构体 struct msg_buffer { long mtype; long client_pid; char text[MAX_MSG_SIZE]; }; int main() { // 创建自己的消息队列(基于 PID) key_t client_key = getpid() % 65535; int client_msgid = msgget(client_key, 0666 | IPC_CREAT); if (client_msgid == -1) { perror("msgget (client)"); exit(1); } // 连接服务器消息队列 int server_msgid = msgget(SERVER_KEY, 0666); if (server_msgid == -1) { perror("msgget (server)"); exit(1); } struct msg_buffer msg; msg.mtype = 1; // 类型固定为1 msg.client_pid = getpid(); // 记录自身 PID strcpy(msg.text, "hello"); // 发送内容 // 发送到服务器 if (msgsnd(server_msgid, &msg, sizeof(long) + MAX_MSG_SIZE, 0) == -1) { perror("msgsnd"); exit(1); } printf("Client %ld sent: hello\n", msg.client_pid); // 接收回执 struct msg_buffer reply; if (msgrcv(client_msgid, &reply, MAX_MSG_SIZE, 1, 0) == -1) { perror("msgrcv (reply)"); exit(1); } printf("Client received: %s\n", reply.text); // 删除自己的消息队列 msgctl(client_msgid, IPC_RMID, NULL); return 0; } ``` --- ### 代码解析 1. **服务器逻辑**:创建一个全局消息队列(`SERVER_KEY`),持续监听类型为1的消息。提取客户端PID和消息内容,生成回执,并通过客户端独立的消息队列返回响应。 2. **客户端逻辑**:基于自身PID创建私有消息队列用于接收回执;向服务器公共队列发送包含自身PID和消息内容的数据包;随后等待并打印回执。 3. **通信机制**:使用 `msgget` 按键获取队列ID,`msgsnd` 发送消息,`msgrcv` 接收指定类型消息,`msgctl` 控制资源释放。 4. **多客户端支持**:每个客户端使用其PID模值得到唯一键建立独立接收通道,确保服务器能准确路由回执。 --- ### 知识点(列出该代码中遇到的知识点) - **消息队列(Message Queue)**:内核级进程间通信机制,支持多进程异步收发结构化消息,通过键值定位队列。 - **消息类型(mtype)**:用于过滤接收特定类型消息,实现多路复用或优先级调度。 - **IPC资源标识**:`msgget` 使用 `ftok` 或显式键生成唯一ID,不同进程可通过相同键访问同一资源。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值