Leetcode 0021.合并两个有序链表
21. 合并两个有序链表将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 示例 1: 12输入:l1 = [1,2,4], l2 = [1,3,4]输出:[1,1,2,3,4,4] 示例 2: 12输入:l1 = [], l2 = []输出:[] 示例 3: 12输入:l1 = [], l2 = [0]输出:[0] 题目大意需要将两个升序排列的链表合并成一个新的升序链表,新链表由原两个链表的所有节点组成,且保持升序排列。 解题思路 使用虚拟头节点(dummy node)简化边界情况处理 双指针遍历两个链表,比较当前节点值,将较小的节点接入结果链表 当一个链表遍历完毕后,将另一个链表的剩余部分直接接入结果链表 这种方法的时间复杂度为 O (n + m),其中 n 和 m 分别是两个链表的长度,空间复杂度为 O (1),仅使用了常数个额外节点。 1234567891011121314151617181920212223242526272829303132333435363738/** * Definition for si...
网络理论核心知识整理
一、网络分层:从 OSI 到 TCP/IP 的架构演进1.1 ISO/OSI 七层模型OSI 模型将网络通信划分为七层,构建了标准化的网络通信框架,各层在实际网络交互中扮演着不可或缺的角色: 应用层:直接为用户应用程序提供服务,是用户与网络的接口层。例如,HTTP 协议用于网页数据传输,在浏览器输入网址后,HTTP 协议负责将网页资源从服务器请求并传输到客户端;SMTP 协议则用于电子邮件的发送,实现邮件从发件人服务器到收件人服务器的传输。 表示层:负责数据格式转换与加密解密。当不同系统间进行数据交互时,如 Windows 系统与 Linux 系统,需要表示层将数据转换为双方都能理解的格式;在数据传输安全方面,SSL/TLS 协议就在表示层实现数据加密,保护用户信息不被窃取。 会话层:管理应用程序间的会话连接。以在线购物为例,用户从浏览商品到下单付款的整个过程,会话层会维持用户与服务器之间的会话,确保交易流程的连续性和正确性。 传输层:保障端到端的数据传输,核心协议 TCP 和 UDP 有不同的应用场景。TCP 是面向连接的可靠协议,适用于对数据准...
Leetcode 0020. Valid Parentheses
20. Valid ParenthesesGiven a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. Every close bracket has a corresponding open bracket of the same type. 题目大意给定一个只包含 '('、')'、'{'、'}'、'[' 和 &...
Leetcode 0019. Remove Nth Node From End of List
19. Remove Nth Node From End of ListGiven the head of a linked list, remove the nth node from the end of the list and return its head. Example 1: 12Input: head = [1,2,3,4,5], n = 2Output: [1,2,3,5] Example 2: 12Input: head = [1], n = 1Output: [] Example 3: 12Input: head = [1,2], n = 1Output: [1] 题目大意给定一个链表的头节点,要求删除链表的倒数第 N 个节点,并返回删除后的链表头节点。 解题思路可以使用双指针法高效解决这个问题,只需一次遍历: 定义两个指针 fast 和 slow,初始都指向虚拟头节点 先让 fast 指针向前移动 N 步 然后让 fast 和 slow 同时向前移动,直到 fast 到达链表末尾 此时 slow 指针指向的就是要删除节点的前一个节点 通过调整指...
TCP 与 UDP 协议对比:抓包视角下的特性与应用分析
导言在 TCP/IP 协议簇的传输层体系中,传输控制协议(Transmission Control Protocol, TCP)与用户数据报协议(User Datagram Protocol, UDP)作为两种核心通信协议,分别承担着差异化的网络传输任务。本研究基于 Wireshark 等专业网络抓包工具采集的实证数据,系统性探究两种协议的技术架构、运行机制及其典型应用场景。 一、传输层协议概述作为 OSI 七层模型的关键层级,传输层承担着端到端数据传输管理的核心职能,涵盖数据分段重组、传输可靠性保障、流量控制等重要功能。TCP 与 UDP 作为传输层的核心协议,其设计理念存在本质差异,这种差异性直接影响其在不同网络环境中的适用性。 TCP 协议采用面向连接的通信模式,通过三次握手机制建立连接,四次挥手过程释放连接,从而实现数据的可靠传输。与之相对,UDP 协议采用无连接设计,省略连接建立与释放流程,仅负责将应用层数据封装为数据报进行传输,不保证数据的有序性与完整性。 二、TCP 协议的技术特性与抓包分析2.1 核心技术机制TCP 协议的可靠性保障体系由一系列关键机制构...
Leetcode 0017. Letter Combinations of a Phone Number
17. Letter Combinations of a Phone Number题目Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters. Example: 12Input: "23"Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]. Note: Although th...
Leetcode 0016. 3Sum Closest
16. 3Sum Closest题目Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution. Example: 123Given array nums = [-1, 2, 1, -4], and target = 1.The sum that is closest to the target is 2. (-1 + 2 + 1 = 2). 题目大意给定一个数组,要求在这个数组中找出 3 个数之和离 target 最近。 解题思路 初始值优化: 原代码中it初始化为 0,在某些特殊情况下(如所有可能的和都是正数且较大)可能导致额外计算 改为使用数组前三个元素的和作为初始值,更合理地设置了起点...
HTTP 无状态性相关概念详解
一、无状态与有状态1.1 无状态无状态性体现为服务器在完成单次请求处理后,不保存任何与该事务相关的上下文信息。每个 HTTP 请求均被视为独立的原子操作,服务器响应过程完全依赖当前请求携带的信息,而不依赖先前请求产生的历史数据。 以淘宝网首页为例,用户首次请求时,服务器依推荐算法生成商品推荐与活动界面;页面刷新时,服务器将新请求视为全新事务,重新检索数据并渲染页面,不参考历史访问记录。这种设计契合无状态协议原则,有效提升前端服务器集群并发处理能力,减少服务器资源消耗,适用于高并发场景。 无状态设计简化服务器逻辑,降低复杂度。在淘宝每日亿级用户访问首页的场景下,若记录用户历史与会话状态,将消耗大量内存与计算资源。无状态设计使服务器独立处理每个请求,故障时其他服务器可快速接管,不影响服务,也便于系统扩展维护,新服务器无需同步历史数据即可参与请求处理。 1.2 有状态与无状态协议不同,有状态通信机制需服务器维护客户端会话状态,包括身份认证、操作记录、交易进度等核心数据。 以支付宝转账为例,系统将交易状态(处理中 / 已完成 / 异常)持久化存储。用户后续操作时,服务...
Leetcode 0015. 3Sum
15. 3Sum题目Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: The solution set must not contain duplicate triplets. Example: 1234567Given array nums = [-1, 0, 1, 2, -1, -4],A solution set is:[ [-1, 0, 1], [-1, -1, 2]] 题目大意给定一个数组,要求在这个数组中找出 3 个数之和为 0 的所有组合。 解题思路用 map 提前计算好任意 2 个数字之和,保存起来,可以将时间复杂度降到 O(n^2)。这一题比较麻烦的一点在于,最后输出解的时候,要求输出不重复的解。数组中同一个数字可能出现多次,同一个数字也可能使用多次,但是最后输出解...
linux:多线程编程中互斥访问与线程同步机制的理论与实践
导言在多线程并发编程环境中,共享资源的安全访问与线程间的协同工作是确保程序正确性与高效性的核心问题。本文基于生产者 - 消费者模型的实现代码,系统阐述互斥访问共享资源与线程间同步的理论基础、实现机制及实践。 一、 引言随着多核处理器技术的发展,多线程编程已成为提升程序性能的关键技术手段。然而,多线程并发执行也引入了新的挑战:当多个线程共享有限资源时,未经协调的并发操作可能导致数据不一致、死锁等严重问题。互斥访问与线程同步机制正是为解决这些问题而设计的核心技术,它们共同构成了多线程编程的基础框架。本文以一个典型的生产者 - 消费者模型实现为研究对象,深入剖析互斥与同步机制的工作原理。 二、 互斥访问共享资源的理论基础2.1 共享资源与竞态条件共享资源指的是可被多个线程同时访问的内存区域、数据结构或外部设备。在生产者 - 消费者模型中,由Res结构体表示的资源池及其包含的产品链表(Production结构体链表)构成了典型的共享资源。当多个线程(生产者与消费者)同时对这些资源进行修改时,可能引发竞态条件(Race Condition)—— 即程序的最终结果依赖于线程执行的时序。 竞态...
Leetcode 0014. Longest Common Prefix
14. Longest Common Prefix题目Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "". Example 1: Input: strs = ["flower","flow","flight"] Output: "fl" Example 2: Input: strs = ["dog","racecar","car"] Output: "" Explanation: There is no common prefix among the input strings. Constraints: 1 <= strs.length <...
Leetcode 0013. Roman to Integer
13. Roman to Integer题目Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. 12345678Symbol ValueI 1V 5X 10L 50C 100D 500M 1000 For example, 2 is written as II in Roman numeral, just two one's added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from le...
linux:POSIX 线程库 (`pthread`) 详解与多线程编程实践
一、pthread 库概述POSIX 线程 (POSIX Threads),简称 pthread,是 C 语言中实现多线程编程的标准库。它提供了一套丰富的 API,用于创建、同步和管理线程。pthread 库在 Unix、Linux 和 macOS 等系统上广泛支持,是开发高性能并发程序的重要工具。 核心组件 线程管理:创建、终止和等待线程 同步机制:互斥锁 (Mutex)、读写锁、条件变量 线程同步:信号量、屏障 线程特定数据:每个线程独立的数据存储 二、线程的基本操作1. 线程创建与终止pthread_create 函数12int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); 参数说明: thread:指向 pthread_t 类型的指针,用于存储新创建线程的 ID attr:线程属性设置,NULL 表示使用默认属性 start_routine:线程入口函数,返回值和参...
Leetcode 0012. Integer to Roman
12. Integer to Roman题目Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. 12345678Symbol ValueI 1V 5X 10L 50C 100D 500M 1000 For example, 2 is written as II in Roman numeral, just two one's added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from le...
Leetcode 0011. Container With Most Water
11. Container With Most Water题目Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container and n is at least 2. The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of wa...
linux:从源码视角解析 pthread_cleanup_push 与 pthread_cleanup_pop 的成对出现机制
导言在多线程编程领域,线程资源的正确释放是保障程序稳定性与可靠性的关键环节。pthread_cleanup_push和pthread_cleanup_pop作为线程资源清理的重要机制,其成对出现的要求并非随意设定,而是由底层源码实现逻辑所决定。本文将从源码角度深入剖析这一要求的根本原因,并结合具体代码示例说明其在实际资源管理中的重要性。 一、宏定义的语法约束在多数系统的实现中,pthread_cleanup_push和pthread_cleanup_pop并非以普通函数的形式存在,而是通过宏定义来实现功能。从语法结构上看,很多实现里pthread_cleanup_push会以类似左花括号 “{” 的形式结束,而pthread_cleanup_pop则以类似右花括号 “}” 的形式开始。 这种宏定义的结构设计,是为了确保两者之间的代码块形成一个完整的语法单元。若不成对出现,会直接破坏代码的语法结构,导致编译器在解析过程中出现语法错误,使得程序无法通过编译。这一语法层面的约束,从根本上要求这两个宏必须成对使用。 二、清理栈的维护逻辑线程的清理机制依赖于一个内部的清理栈,pthread...
Leetcode 0010. Regular Expression Matching
10. Regular Expression MatchingGiven an input string s and a pattern p, implement regular expression matching with support for '.' and '*' where: '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). Example 1: 123Input: s = "aa", p = "a"Output: falseExplanation: "a" does not match the entire string "aa". Example 2: 123Inpu...
Leetcode 0009. Palindrome Number
9. Palindrome Number题目Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: 12Input: 121Output: true Example 2: 123Input: -121Output: falseExplanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: 123Input: 10Output: falseExplanation: Reads 01 from right to left. Therefore it is not a palindrome. Follow up: Coud you solve it without converting th...
《C陷阱与缺陷》学习:不踩坑的好代码
一、引言《C 陷阱与缺陷》(C Traps and Pitfalls)由知名计算机科学家 Andrew Koenig 所著,作为 C 语言编程领域的经典权威著作,自出版以来始终是 C 语言开发者进阶路上的必读书籍。本书聚焦于 C 语言底层机制,通过系统性的分析与丰富的实践案例,深度剖析了 C 语言中容易引发逻辑错误、安全漏洞和性能问题的语法特性、实现细节及不良编程习惯,旨在帮助程序员构建对 C 语言的全面认知,进而编写出具备高安全性、高可靠性和高健壮性的代码。本学习笔记以该书第二版为蓝本,结合现代 C 语言开发场景,系统梳理 C 语言编程中各类常见陷阱及其有效的防御策略,为开发者提供实用的参考指南。 二、词法分析陷阱2.1 "贪心法" 原则C 编译器在进行词法分析阶段,严格遵循 "贪心法"(又称 "大嘴法")规则,该规则的核心逻辑在于尽可能地将连续字符序列组合成单个符号单元。这一特性源于 C 语言早期设计中对代码简洁性和解析效率的平衡考量,却也为代码编写带来潜在歧义风险。例如: 1a---b // 编译器依据贪心法解...
《C 和指针》学习:从底层原理到实战进阶
一、指针本质的深度剖析1.1 指针的内存映射机制指针变量在内存中占据固定大小的存储空间(取决于系统位数,32 位系统为 4 字节,64 位系统为 8 字节),其存储的数值是另一个内存单元的地址编码。这种地址编码与内存物理地址存在映射关系,操作系统通过内存管理单元(MMU)实现虚拟地址到物理地址的转换,而指针操作的始终是虚拟地址空间。 1.2 指针类型的约束作用指针的类型并非仅为语法约束,而是决定了指针运算的步长和解引用时的内存访问范围。例如: int *p:p++操作使地址增加sizeof(int),解引用时访问 4 字节内存 char *p:p++操作使地址增加 1 字节,解引用时访问 1 字节内存 这种类型约束是 C 语言类型安全的基础,也是避免内存越界的重要保障。 二、指针与数组的辩证关系2.1 数组名的双重属性数组名在多数语境下表现为 "指向首元素的指针常量",但存在两个例外: 作为sizeof运算符的操作数时,返回整个数组的字节大小(如sizeof(int [5])返回 20) 作为&运算符的操作数时,产生指向整个数组的指针(...
Leetcode 0008. String to Integer (atoi)
8. String to Integer (atoi)题目Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer (similar to C/C++'s atoi function). The algorithm for myAtoi(string s) is as follows: Read in and ignore any leading whitespace. Check if the next character (if not already at the end of the string) is '-' or '+'. Read this character in if it is either. This determines if the final result is negative or positive respectively. Assume the result is p...
Leetcode 0007. Reverse Integer
7. Reverse Integer题目Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Example 2: Input: -123 Output: -321 Example 3: Input: 120 Output: 21 **Note:**Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−2^31, 2^31 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. 题目大意给出一个 32 位的有符号整数,需要将这个整数中每位上的数字进行反转。注意:假设我们的环境只能存储得下 32 位的有...
信号与线程机制详解:从屏蔽位图到共享独立区域
导言在操作系统的复杂架构体系中,信号与线程作为保障程序高效运行与协同调度的核心机制,其原理与实现对系统性能与稳定性起着决定性作用。信号作为进程间异步通信的关键途径,通过信号屏蔽策略与位图管理机制,构建起进程对外部事件的动态响应体系;线程则以资源共享与独立分配的双重特性,在提升程序并发执行效率的同时,引发数据一致性与资源管理等关键问题。而pthread库作为线程编程的重要工具,为开发者提供了便捷的线程操作接口。深入探究这些核心概念,对于开发高可靠性的系统级软件具有重要理论与实践意义。 一、信号机制详解1.1 信号的定义与作用信号作为一种软件中断机制,承担着进程间异步事件通知的重要功能。操作系统预先定义了丰富的信号类型,例如SIGINT(由用户通过Ctrl + C组合键触发的中断信号)、SIGTERM(用于正常终止进程的信号)等。当特定系统事件发生或执行特定系统调用时,信号将被发送至目标进程,进程根据自身配置的信号处理策略(默认处理、自定义处理函数或忽略)进行响应,从而实现系统层面的事件驱动处理机制。 1.2 信号屏蔽信号屏蔽作为进程对信号处理的重要控制手段,通过信号屏蔽字(Sign...
Leetcode 0006. Zigzag Conversion
6. Zigzag Conversion题目The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) 123P A H NA P L S I I GY I R And then read line by line: "PAHNAPLSIIGYIR" Write the code that will take a string and make this conversion given a number of rows: 1string convert(string s, int numRows); Example 1: 12Input: s = "PAYPALISHIRING", numRows = 3Outpu...
Leetcode 0005. Longest Palindromic Substring
5. Longest Palindromic Substring题目Given a string s, return the longest palindromic substring in s. Example 1: 1234Input: s = "babad"Output: "bab"Note: "aba" is also a valid answer. Example 2: 123Input: s = "cbbd"Output: "bb" Example 3: 123Input: s = "a"Output: "a" Example 4: 123Input: s = "ac"Output: "a" Constraints: 1 <= s.length <= 1000 s consist of only digits and English letters (lower-cas...

