上传下载系列的整理修改改改 - 服务端 - 4
CD\LS命令12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152#include "Server.h"// CD命令实现int CmdCd(char *path, const int depth, FileBuf_t *msg, Node_t *p){ DIR *dir =opendir(path); if(dir == NULL){ sprintf(msg->buf, "(error: %s)", strerror(errno)); return -1; }else{ p->depth = depth; char *help; char *pathblock = strtok_r(path, " /", &help); f...
Leetcode 0105. Construct Binary Tree from Preorder and Inorder Traversal
105. Construct Binary Tree from Preorder and Inorder Traversal题目Given preorder and inorder traversal of a tree, construct the binary tree. **Note:**You may assume that duplicates do not exist in the tree. For example, given preorder = [3,9,20,15,7] inorder = [9,3,15,20,7] Return the following binary tree: 3 / \ 9 20 / \ 15 7 题目大意根据一棵树的前序遍历与中序遍历构造二叉树。 注意:你可以假设树中没有重复的元素。 解题思路 inorder_map:用于快速查找中序遍历中元素对应的索引,时间复杂度 O (1) pre:保存前序遍历数组的副本,避免在递归中反复传递参数 确定根节点:前序遍历的第一个元素为当前树的根节点 划分左右...
Leetcode 0104. Maximum Depth of Binary Tree
104. Maximum Depth of Binary TreeGiven the root of a binary tree, return its maximum depth. A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. Example 1: 12Input: root = [3,9,20,null,null,15,7]Output: 3 Example 2: 12Input: root = [1,null,2]Output: 2 题目大意给定一棵二叉树的根节点 root,返回该二叉树的最大深度。二叉树的最大深度是指从根节点到最远叶子节点的最长路径上的节点数量。 例如: 输入二叉树 [3,9,20,null,null,15,7],其最大深度为 3(路径:3 → 20 → 15 或 3 → 20 → 7,均包含 3 个节点)。 解题思路计算二叉树的最...
上传下载系列的整理修改改改 - 服务端 - 3
服务端信息处理123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111#include "Server.h"// 接收并处理命令int RecvCommand(Node_t *p){ int netfd = p->netfd; //接收指令 SignalSend_t *cmd = (SignalSend_t *)calloc(1, sizeof(SignalSend_t)); if (cmd == NULL){ printf("Error calloc\n"); syslog(...
Leetcode 0103. Binary Tree Zigzag Level Order Traversal
103. Binary Tree Zigzag Level Order Traversal Given the root of a binary tree, return the zigzag level order traversal of its nodes' values. (i.e., from left to right, then right to left for the next level and alternate between). Example 1: 12Input: root = [3,9,20,null,null,15,7]Output: [[3],[20,9],[15,7]] Example 2: 12Input: root = [1]Output: [[1]] Example 3: 12Input: root = []Output: [] 题目大意给定一棵二叉树的根节点,返回其节点值的锯齿形层序遍历。即:第一层从左到右,第二层从右到左,第三层再从左到右,以此类推,交替进行。 例如: 输入 root = [3,9,20,null...
Leetcode 0102. Binary Tree Level Order Traversal
102. Binary Tree Level Order TraversalGiven the root of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level). Example 1: 12Input: root = [3,9,20,null,null,15,7]Output: [[3],[9,20],[15,7]] Example 2: 12Input: root = [1]Output: [[1]] Example 3: 12Input: root = []Output: [] 题目大意给定一棵二叉树的根节点 root,返回其节点值的层序遍历结果(即从左到右、逐层遍历)。结果需以二维数组形式呈现,每一层的节点值构成一个子数组(例如,第一层 [根节点],第二层 [左子节点, 右子节点],以此类推)。 解题思路层序遍历的核心是按照树的层次依次访问节点,从根节点开始,先访问第一层(根节点)...
Leetcode 0101. Symmetric Tree
101. Symmetric TreeGiven the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center). Example 1: 12Input: root = [1,2,2,3,4,4,3]Output: true Example 2: 12Input: root = [1,2,2,null,3,null,3]Output: false 题目大意给定一棵二叉树的根节点 root,判断该二叉树是否是对称的(即围绕中心轴镜像对称)。 例如: 输入二叉树 [1,2,2,3,4,4,3],其左子树与右子树成镜像,故返回 true; 输入二叉树 [1,2,2,null,3,null,3],左子树与右子树不镜像,故返回 false。 解题思路判断二叉树是否对称的核心是比较左子树与右子树是否成镜像,即: 左子树的左节点需与右子树的右节点值相等; 左子树的右节点需与右子树的左节点值相等。 1. 递归法设计一个辅助函数,比较两个子树...
上传下载系列的整理修改改改 - 服务端 - 2
服务端登录验证123456789101112131415161718192021222324252627282930313233343536373839#include "Server.h"//#include <shadow.h>//#include <crypt.h>//#include <syslog.h>int IsLoading(int netfd){ if(send(netfd, "请输入用户名和密码", sizeof("请输入用户名和密码"), 0) <= 0){ printf("error recv loadmsg\n"); return -1; } char load[2][PATHLEN]; char *username = load[0]; if(recv(netfd, load, 2 * PATHLEN, 0) <= 0){ ...
Leetcode 0100. Same Tree
100. Same TreeGiven the roots of two binary trees p and q, write a function to check if they are the same or not. Two binary trees are considered the same if they are structurally identical, and the nodes have the same value. Example 1: 12Input: p = [1,2,3], q = [1,2,3]Output: true Example 2: 12Input: p = [1,2], q = [1,null,2]Output: false Example 3: 12Input: p = [1,2,1], q = [1,1,2]Output: false 题目大意给定两棵二叉树的根节点 p 和 q,判断这两棵树是否相同。两棵树相同的定义是:结构完全相同,且对应节点的值也相同。 例如: 输入两棵结构和节点值均相同的树 [1,2,3]...
上传下载系列的整理修改改改 - 服务端 - 1
服务端头文件123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384#ifndef SERVER_H#define SERVER_H#include "my_header.h"#include <arpa/inet.h>#include <fcntl.h>#include <dirent.h>#include <sys/mman.h>#include <unistd.h>#include <pthread.h>#include <netinet/in.h>#include <sys/epoll.h>#include <sys/types.h>#include <sys/socket.h>...
Leetcode 0099. Recover Binary Search Tree
99. Recover Binary Search TreeYou are given the root of a binary search tree (BST), where the values of exactly two nodes of the tree were swapped by mistake. Recover the tree without changing its structure. Example 1: 123Input: root = [1,3,null,null,2]Output: [3,1,null,null,2]Explanation: 3 cannot be a left child of 1 because 3 > 1. Swapping 1 and 3 makes the BST valid. Example 2: 123Input: root = [3,1,4,null,null,2]Output: [2,1,4,null,null,3]Explanation: 2 cannot be in the right subt...
Leetcode 0098. Validate Binary Search Tree
98. Validate Binary Search TreeGiven the root of a binary tree, determine if it is a valid binary search tree (BST). A valid BST is defined as follows: The left subtree of a node contains only nodes with keys strictly less than the node's key. The right subtree of a node contains only nodes with keys strictly greater than the node's key. Both the left and right subtrees must also be binary search trees. Example 1: 12Input: root = [2,1,3]Output: true Example 2: 123Input: root = [...
上传下载系列的整理修改改改 - 2
收发文件循环接收12345678910#include "Client.h"int recvn(int sockfd, void *buf, int size){ int total = 0; char *p = (char *)buf; while(total < size){ ssize_t sret = recv(sockfd, p+total, size-total, 0); total += sret; } return total;} 收文件根据对方发送的文件名,在当下实现文件的接收 123456789101112131415161718192021222324252627282930313233343536int RecvFile(int sockfd){ FileBuf train; char filename[128] = {0}; recv(sockfd, &train.size, ...
上传下载系列的整理修改改改
导言最近学习完成了数据库和多人聊天室,想基于这些东西,设计一个老掉牙的网盘项目(才不是收到齐哥刺激)。目前是只有空闲时间能做,大概还是跟之前更新一样,大概一周一更慢慢写。 头文件1234567891011121314151617181920212223242526272829303132333435/* Usage: 实现收发功能,收消息没有限制,发消息需要信号,Switch判断发送,结构体发送指令,接收消息,接收文件*/#ifndef CLIENT_H#define CLIENT_H#include "my_header.h"#include <arpa/inet.h>#include <fcntl.h>#include <unistd.h>#include <pthread.h>#include <netinet/in.h>#include <sys/epoll.h>#include <sys/types.h>#include <sys/socket.h>#in...
Leetcode 0096. Unique Binary Search Trees
96. Unique Binary Search TreesGiven an integer n, return *the number of structurally unique **BST'*s (binary search trees) which has exactly n nodes of unique values from 1 to n. Example 1: 12Input: n = 3Output: 5 Example 2: 12Input: n = 1Output: 1 题目大意给定一个整数 n,计算由 1 到 n 为节点所组成的结构独特的二叉搜索树(BST)的数量。 例如: 输入 n = 3,存在 5 种结构独特的 BST,返回 5; 输入 n = 1,只有 1 种 BST,返回 1。 解题思路计算独特 BST 的数量可以通过动态规划(DP) 实现,核心思路基于以下观察: 若选择 i 作为根节点,则左子树由 1~i-1 构成,右子树由 i+1~n 构成; 左子树的独特结构数量为 dp[i-1],右子树的独特结构数量为 dp[n-i](...
Leetcode 0095. Unique Binary Search Trees II
95. Unique Binary Search Trees IIGiven an integer n, return *all the structurally unique **BST'*s (binary search trees), which has exactly n nodes of unique values from 1 to n. Return the answer in any order. Example 1: 12Input: n = 3Output: [[1,null,2,null,3],[1,null,3,2],[2,1,3],[3,1,null,null,2],[3,2,null,1]] Example 2: 12Input: n = 1Output: [[1]] 题目大意给定一个整数 n,生成所有由 1 到 n 为节点所组成的结构独特的二叉搜索树(BST)。返回这些二叉搜索树的根节点列表。 二叉搜索树的特性是:对于任意节点,其左子树中的所有节点值都小于该节点值,右子树中的所有节点值都大于该节点值。 例如: 输入 n = 3,存在...
游戏管理系统数据库设计与操作
一、数据库设计1234567891011121314151617181920212223242526272829303132333435-- 创建游戏管理系统数据库CREATE DATABASE game_management_db;USE game_management_db;-- 创建游戏表(主表)CREATE TABLE games ( game_id INT PRIMARY KEY, -- 游戏编号,主键 game_name VARCHAR(100) NOT NULL, -- 游戏名称,非空约束 developer VARCHAR(50) NOT NULL, -- 开发商,非空约束 release_year YEAR, -- 发布年份 genre VARCHAR(30), -- 游戏类型 copies_in_stock INT DEFAULT 0 -- 库存数量,默认值0);-- 创建玩家表(主表)CREATE TABLE players ( player_id INT PRIMARY KEY, -- 玩家编号,主键 player_name ...
Leetcode 0094. Binary Tree Inorder Traversal
94. Binary Tree Inorder TraversalGiven the root of a binary tree, return the inorder traversal of its nodes' values. Example 1: Input: root = [1,null,2,3] Output: [1,3,2] Explanation: Example 2: Input: root = [1,2,3,4,5,null,8,null,null,6,7,9] Output: [4,2,6,5,7,1,3,9,8] Explanation: Example 3: Input: root = [] Output: [] Example 4: Input: root = [1] Output: [1] 题目大意 给定一棵二叉树的根节点 root,返回其节点值的中序遍历结果。中序遍历的顺序是「左子树 → 根节点 → 右子树」,遵循 “左 - 根 - 右” 的递归逻辑,且需按此顺序收集所有节点值。 解题思路二叉...
MySQL主键与外键设计原理详解
一、主键设计基础1.1 概念定义主键是用于唯一标识表中每一行记录的特殊字段。其核心特性包括: 唯一性:表中每个主键值必须不同 非空性:主键字段不能为NULL 稳定性:主键值一旦确定不应频繁变更 在MySQL中,主键通过PRIMARY KEY语法定义,可以是单字段或组合字段。主键会自动创建聚集索引,直接影响数据存储方式。 1.2 创建示例学生课程关系模型 123456789101112## 学生表| 字段名 | 类型 | 说明 ||--------|------|------|| id | int | 主键 || name | varchar(50) | 学生姓名 |## 课程表| 字段名 | 类型 | 说明 ||--------|------|------|| id | int | 主键 || title | varchar(100) | 课程名称 || student_id | int | 外键,关联学生表id | 1.3 验证示例学生课程验证 123456789INSERT INTO students (id, name) VALUES (1, '张三...
Leetcode 0093. Restore IP Addresses
93. Restore IP AddressesA valid IP address consists of exactly four integers separated by single dots. Each integer is between 0 and 255 (inclusive) and cannot have leading zeros. For example, "0.1.2.201" and "192.168.1.1" are valid IP addresses, but "0.011.255.245", "192.168.1.312" and "192.168@1.1" are invalid IP addresses. Given a string s containing only digits, return all possible valid IP addresses that can be formed by inserting dots ...
Leetcode 0090. Subsets II
90. Subsets IIGiven an integer array nums that may contain duplicates, return all possible subsets (the power set). The solution set must not contain duplicate subsets. Return the solution in any order. Example 1: 12Input: nums = [1,2,2]Output: [[],[1],[1,2],[1,2,2],[2],[2,2]] Example 2: 12Input: nums = [0]Output: [[],[0]] 题目大意给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(即幂集)。幂集需包含所有可能的子集(包括空集和数组本身),且不能有重复子集,结果顺序可任意。 例如: 输入 nums = [1,2,2],输出 [[],[1],[1,2],[1,2,2],[2],[2,2]](共 6 个子集,无重复); 输入 nums =...
MYSQL 基础操作语句整理
一、查询数据操作 (SELECT)1.1 基础语法12345SELECT [DISTINCT] 字段列表|* FROM 表名 [WHERE 条件表达式] [ORDER BY 字段 [ASC|DESC]] [LIMIT 起始位置, 记录数量] 1.2 执行流程 确定数据来源 (FROM 表名);筛选符合条件的记录 (WHERE 子句);选择需要显示的字段 (SELECT 子句);对结果进行排序 (ORDER BY 子句);限制返回记录数量 (LIMIT 子句) 1.3 高级用法示例 去重查询:在 employees 表中查询所有员工不同的部门编号 12SELECT DISTINCT department_id FROM employees 条件组合查询:在 orders 表中,查询 2023 年之后下单且订单状态为 "completed" 或 "shipped" 的前 10 条订单记录,并按订单金额降序排列 123456SELECT order_id, order_amount FROM orders WHERE o...
Leetcode 0086. Partition List
86. Partition ListGiven the head of a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. Example 1: 12Input: head = [1,4,3,2,5,2], x = 3Output: [1,2,2,4,3,5] Example 2: 12Input: head = [2,1], x = 2Output: [1,2] 题目大意给定链表的头节点 head 和一个值 x,要求将链表分隔成两部分:所有值小于 x 的节点排在所有值大于或等于 x 的节点之前。同时需要保留两部分中节点的原始相对顺序。 解题思路可以通过构建两个临时链表来解决: 创建两个虚拟头节点,分别用于...
TCP 文件传输系统:事件驱动与线程池协同架构下的代码解构与设计实践
一、引言在网络通信领域,TCP 协议凭借其强大的可靠性与稳定性,成为文件传输系统的首选。本教程聚焦于基于 TCP 的多线程文件传输系统,深入剖析事件驱动与线程池协同工作的架构设计。支持双向消息交互与文件传输,由九个核心文件构成,全面覆盖网络连接建立、事件监听、线程管理以及数据传输等关键环节。 二、代码结构分析1. 文件概览 文件名 主要功能 核心函数列表 head.h 全局结构体定义与函数声明 结构体:Train(封装消息与文件传输数据)、Queue_t(定义客户端连接队列结构)、Res_t(线程池资源相关结构体);函数声明:Ready(服务器套接字初始化)、EpollAdd(将文件描述符添加到 epoll 监听列表)等 client.c 客户端主逻辑实现 main函数包含事件驱动循环,集成消息处理、连接状态管理及用户输入响应等模块,负责客户端的连接管理与数据通信。 Server.c 服务器核心逻辑 main函数基于 epoll 事件驱动机制,整合信号处理模块,实现客户端连接接收、消息转发及线程池任务调度,是服务器的核心控制中枢。 Ready.c 服务器套接...
Leetcode 0084. 柱状图中最大的矩形
84. 柱状图中最大的矩形给定 n 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。 求在该柱状图中,能够勾勒出来的矩形的最大面积。 示例 1: 123输入:heights = [2,1,5,6,2,3]输出:10解释:最大的矩形为图中红色区域,面积为 10 示例 2: 12输入: heights = [2,4]输出: 4 解题思路该解法是对单调栈思路的优化实现,通过在数组末尾添加哨兵元素和栈初始化技巧,将左右边界的计算合并到一次遍历中,代码更简洁且效率更高。 核心优化思路 哨兵元素(Sentinel): 在原数组末尾添加 -1(高度为负数的哨兵),确保遍历结束时栈中所有元素都会被弹出计算(相当于 “大火收汁”); 栈初始时推入 -1(索引哨兵),解决栈空时的边界判断问题,同时自然对应 “左侧无更矮柱子” 的情况(left[i] = -1)。 一次遍历计算左右边界: 遍历每个元素作为右侧边界(right),当当前高度小于栈顶高度时,栈顶元素的左右边界均已确定: 右边界:当前索引 right(第一个比栈顶元素矮的位置); 左边界:弹出栈...

