Leetcode 0611. Valid Triangle Number
611. Valid Triangle NumberGiven an integer array nums, return the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle. Example 1: 123456Input: nums = [2,2,3,4]Output: 3Explanation: Valid combinations are: 2,3,4 (using the first 2)2,3,4 (using the second 2)2,2,3 Example 2: 12Input: nums = [4,2,3,4]Output: 4 题目大意给定一个整数数组 nums,返回数组中可作为三角形三条边长度的三元组数量。三角形三条边需满足:任意两边之和大于第三边。 核心解题思路:排序 + 双指针三角形三边的核心条件是:两边之和大于第三边。对于排序后的数组,可简化为: 设三条边为 a ≤...
Leetcode 0189. 轮转数组
189. 轮转数组给定一个整数数组 nums,将数组中的元素向右轮转 k 个位置,其中 k 是非负数。 示例 1: 123456输入: nums = [1,2,3,4,5,6,7], k = 3输出: [5,6,7,1,2,3,4]解释:向右轮转 1 步: [7,1,2,3,4,5,6]向右轮转 2 步: [6,7,1,2,3,4,5]向右轮转 3 步: [5,6,7,1,2,3,4] 示例 2: 12345输入:nums = [-1,-100,3,99], k = 2输出:[3,99,-1,-100]解释: 向右轮转 1 步: [99,-1,-100,3]向右轮转 2 步: [3,99,-1,-100] 题目大意给定一个整数数组 nums 和非负整数 k,将数组元素向右轮转 k 个位置(即每个元素向右移动 k 位,末尾元素移动到开头)。要求尽可能优化时间和空间复杂度。 核心解题思路:三次反转法常规思路(如临时数组、多次右移)要么空间复杂度高(O (n)),要么时间复杂度高(O (nk))。三次反转法通过巧妙的反转操作,实现 O (n) 时间复杂度 + O (1)...
Leetcode 0148. 排序链表
148. 排序链表给你链表的头结点 head ,请将其按 升序 排列并返回 排序后的链表 。 示例 1: 12输入:head = [4,2,1,3]输出:[1,2,3,4] 示例 2: 12输入:head = [-1,5,3,4,0]输出:[-1,0,3,4,5] 示例 3: 12输入:head = []输出:[] 题目大意给定链表的头节点 head,要求将链表按升序排列并返回排序后的链表头节点。 解题思路对于链表排序,最适合的高效算法是归并排序,原因如下: 归并排序的时间复杂度为 O (n log n),是链表排序的最优选择 链表的归并操作不需要像数组那样额外分配 O (n)...
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...
Leetcode 0082. Remove Duplicates from Sorted List II
82. Remove Duplicates from Sorted List IIGiven the head of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Return the linked list sorted as well. Example 1: 12Input: head = [1,2,3,3,4,4,5]Output: [1,2,5] Example 2: 12Input: head = [1,1,1,2,3]Output: [2,3] 题目大意给定一个已排序的链表,要求删除所有存在重复的节点(即重复的节点一个都不保留),仅保留原链表中只出现过一次的节点,最终返回排序后的新链表头节点。 核心解题思路由于链表已排序,重复节点必然相邻,因此可通过「遍历链表 + 跳过重复节点」的思路解决,关键是: 虚拟头节点:避免删除头节点时的特殊处理(如示例 2 中头节点...
数据库事务隔离机制全面解析:并发问题与解决方案
一、事务与并发控制基础1.1 事务ACID特性在分布式系统中,事务需要满足以下核心特性: 原子性:事务操作不可分割,全部完成或全部失败 一致性:事务执行前后数据库状态保持一致 隔离性:事务之间相互隔离,避免并发执行的干扰 持久性:事务提交后永久生效,即使系统崩溃也不会丢失 对于InnoDB存储引擎,ACID特性通过日志系统(Redo Log)和多版本并发控制(MVCC)实现,MySQL 8.0版本后已完全支持事务性存储引擎。 1.2 并发问题分类体系四个经典的并发问题及其表现形式: 问题类型 定义说明 典型场景 脏写 事务A写入数据后未提交,事务B又覆盖该数据,导致A的写入被B无效 两个事务对同一行数据进行并发修改,典型例子为数据库主从复制中的write-ahead...
上传下载系列的整理修改改改 - 服务端 - 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); ...
上传下载系列的整理修改改改 - 服务端 - 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"); ...
Leetcode 0287. Find the Duplicate Number
287. Find the Duplicate NumberGiven an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive. There is only one repeated number in nums, return this repeated number. You must solve the problem without modifying the array nums and using only constant extra space. Example 1: 12Input: nums = [1,3,4,2,2]Output: 2 Example 2: 12Input: nums = [3,1,3,4,2]Output: 3 Example 3: 12Input: nums = [3,3,3,3,3]Output: 3 题目大意给定一个包含 n + 1 个整数的数组 nums,其中每个整数都在...
Leetcode 0151. Reverse Words in a String
151. Reverse Words in a StringGiven an input string s, reverse the order of the words. A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space. Return a string of the words in reverse order concatenated by a single space. Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces. Example 1: 12Input: s...
Leetcode 0028. Find the Index of the First Occurrence in a String
28. Find the Index of the First Occurrence in a StringGiven two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Example 1: 1234Input: haystack = "sadbutsad", needle = "sad"Output: 0Explanation: "sad" occurs at index 0 and 6.The first occurrence is at index 0, so we return 0. Example 2: 123Input: haystack = "leetcode", needle = "leeto"Output: -1Explanation:...
上传下载系列的整理修改改改 - 服务端 - 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){ ...
上传下载系列的整理修改改改 - 服务端 - 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...
上传下载系列的整理修改改改 - 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,...

