# 游壳说 > 游壳说 - 专注分享AI软件工程实践以及商业投资思考 ## 所有文章 - [用上这4个原则,为AI编程提效](https://blog.floatshell.com/blog/claude-md-ai-coding-principles/): Andrej Karpathy 最近写了一份 CLAUDE.md 示例,只有 4 条规则。 有人拿去测了:编码准确率从 65% 涨到 94%。 不是模型变强了,是约束条件做对了。 - Published 2026-05-27 - [致精神股东们的信2026](https://blog.floatshell.com/blog/%E8%87%B4%E7%B2%BE%E7%A5%9E%E8%82%A1%E4%B8%9C%E4%BB%AC%E7%9A%84%E4%BF%A12026/): 前言 2025 年是变幻莫测的一年,随着 AI 大模型能力日益强大,变革发生的速率也随之变快。每几个月都会有新的能力或者应用爆发出来,与此同时又有新的领域不断被 AI 深度渗透,而所有人都必须接受现实 … - Published 2026-02-14 - [Docker 容器与容器云读书笔记](https://blog.floatshell.com/blog/docker-%E5%AE%B9%E5%99%A8%E4%B8%8E%E5%AE%B9%E5%99%A8%E4%BA%91%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0/): Iaas 基础设施即服务 Paas 平台即服务 Saas 软件即服务 容器云以容器为资源分割和调度的基本单位,专注资源共享和隔离,容器的编排和部署 每个docker命令都会发送给Docker … - Published 2026-01-26 - [Docker 原理剖析](https://blog.floatshell.com/blog/docker-%E5%8E%9F%E7%90%86%E5%89%96%E6%9E%90/): Namespaces 命名空间 (namespaces) 是 Linux 为我们提供的用于分离进程树、网络接口、挂载点以及进程间通信等资源的方法,这是实现进程间隔离的核心技术,通过配置不同的命名空间我 … - Published 2026-01-26 - [正则表达式快速入门](https://blog.floatshell.com/blog/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%BF%AB%E9%80%9F%E5%85%A5%E9%97%A8/): 应用场景:有关字符串的搜索,替换,验证,过滤 正则表达式语言是一种内置于程序设计语言中的一种迷你语言 演示实例将使用 JS 和 Go 来进行展示 不同语言对正则的实现会有所区别 如何让匹配更精准? 制 … - Published 2026-01-26 - [上下文工程中提示词编写的原则和技巧](https://blog.floatshell.com/blog/%E4%B8%8A%E4%B8%8B%E6%96%87%E5%B7%A5%E7%A8%8B%E4%B8%AD%E6%8F%90%E7%A4%BA%E8%AF%8D%E7%BC%96%E5%86%99%E7%9A%84%E5%8E%9F%E5%88%99%E5%92%8C%E6%8A%80%E5%B7%A7/): 摘要自anthropic:Effective context engineering for AI agents 引言:从单一提示到上下文状态管理 在构建复杂的 AI 智能体(Agent)时,我们关注 … - Published 2025-10-12 - [编写高效工具的最佳实践指南(精华版)](https://blog.floatshell.com/blog/%E7%BC%96%E5%86%99%E9%AB%98%E6%95%88%E5%B7%A5%E5%85%B7%E7%9A%84%E6%9C%80%E4%BD%B3%E5%AE%9E%E8%B7%B5%E6%8C%87%E5%8D%97-%E7%B2%BE%E5%8D%8E%E7%89%88/): 原文来自anthropic:Writing effective tools for agents — with agents 本文概述了为AI代理构建高效工具的关键策略,强调了工具对代理效能的重要性。 … - Published 2025-09-13 - [提示词技巧之元提示](https://blog.floatshell.com/blog/%E6%8F%90%E7%A4%BA%E8%AF%8D%E6%8A%80%E5%B7%A7%E4%B9%8B%E5%85%83%E6%8F%90%E7%A4%BA/): 介绍元提示词的概念和应用,通过模版化和反馈迭代机制,帮助生成更高质量的AI提示词,提高大语言模型的输出效果。 - Published 2025-06-08 - [CGO封装CPP库的一些最佳实践](https://blog.floatshell.com/blog/cgo%E5%B0%81%E8%A3%85cpp%E5%BA%93%E7%9A%84%E4%B8%80%E4%BA%9B%E6%9C%80%E4%BD%B3%E5%AE%9E%E8%B7%B5/): 背景 最近业务上需要复用CPP编写的客户端SDK库,为了让团队主力语言Golang能够顺利接入SDK,因此使用了CGO桥接技术将C++11编写的SDK库封装成生产环境可用的Golang SDK,在翻阅 … - Published 2022-03-27 - [用Go STL查询DB引发的内存泄露](https://blog.floatshell.com/blog/%E7%94%A8go-stl%E6%9F%A5%E8%AF%A2db%E5%BC%95%E5%8F%91%E7%9A%84%E5%86%85%E5%AD%98%E6%B3%84%E9%9C%B2/): 问题起因 这几天有一个 Go API service 经过定时监控发现占用的内存不断上涨,内存从初始的 70M 一直上升到超过 1G 直到吃光内存退出,基本上就可以断定是存在内存泄露的问题了,但是因为 … - Published 2020-03-06 - [理解 CPU Cache 对并发性能的影响](https://blog.floatshell.com/blog/%E7%90%86%E8%A7%A3-cpu-cache-%E5%AF%B9%E5%B9%B6%E5%8F%91%E6%80%A7%E8%83%BD%E7%9A%84%E5%BD%B1%E5%93%8D/): 一般来说每个 CPU 核有 L1 和 L2 缓存,L3是共享缓存 以缓存行为单位存储,通常是 64 字节为一行 这种利用局部性原理的缓存是数组访问比链表访问快的主要原因 伪共享问题 false … - Published 2019-12-30 - [Go常见并发模式](https://blog.floatshell.com/blog/go%E5%B8%B8%E8%A7%81%E5%B9%B6%E5%8F%91%E6%A8%A1%E5%BC%8F/): Go语言的并发模式是其最具特色的编程范式之一。本文总结了在Go开发中最常见的几种并发模式,包括请求-接收模式、单一状态者模式、生产-消费模式、Pipeline流水线模式等。这些模式能够帮助我们更好地组 … - Published 2019-09-22 - [Leetcode 989. Add to Array-Form of Integer](https://blog.floatshell.com/blog/leetcode-989.-add-to-array-form-of-integer/): 题目描述 For a non-negative integer X, the array-form of X is an array of its digits in left to right … - Published 2019-02-14 - [Leetcode 985. Sum of Even Numbers After Queries](https://blog.floatshell.com/blog/leetcode-985.-sum-of-even-numbers-after-queries/): 题目描述 时间复杂度:O(N) 空间复杂度:O(1) - Published 2019-02-13 - [poj 3616 Milking Time](https://blog.floatshell.com/blog/poj-3616-milking-time/): 原题地址 知识点:权值区间DP - Published 2018-07-29 - [poj 2385 Apple Catching 0ms](https://blog.floatshell.com/blog/poj-2385-apple-catching/): 原题地址 知识点:DP - Published 2018-07-14 - [poj 2229 Sumsets](https://blog.floatshell.com/blog/poj-2229-sumsets/): 原题地址 知识点:DP - Published 2018-07-12 - [poj 3176 Cow Bowling](https://blog.floatshell.com/blog/poj-3176-cow-bowling/): 原题地址 知识点:DP - Published 2018-07-11 - [Poj 3262 Protecting the Flowers](https://blog.floatshell.com/blog/poj-3262-protecting-the-flowers/): 原题地址 知识点:贪心 - Published 2018-07-11 - [poj 1017 Packets](https://blog.floatshell.com/blog/poj-1017-packets/): 知识点:贪心 - Published 2018-07-10 - [poj 3040 Allowance](https://blog.floatshell.com/blog/poj-3040-allowance/): 知识点: 贪心 - Published 2018-07-10 - [poj 1979 Red and Black](https://blog.floatshell.com/blog/poj-1979-red-and-black/): 知识点:DFS - Published 2018-06-28 - [通过Go HTTP Client的报错看标准库https请求](https://blog.floatshell.com/blog/%E9%80%9A%E8%BF%87go-http-client%E7%9A%84%E6%8A%A5%E9%94%99%E7%9C%8B%E6%A0%87%E5%87%86%E5%BA%93https%E8%AF%B7%E6%B1%82/): 在用 Go 自带的 http client 进行默认 Get 操作的时候,发现如下错误 - Published 2018-06-28 - [LeetCode题解:最长回文串之manacher算法](https://blog.floatshell.com/blog/leetcode%E9%A2%98%E8%A7%A3%E6%9C%80%E9%95%BF%E5%9B%9E%E6%96%87%E4%B8%B2%E4%B9%8Bmanacher%E7%AE%97%E6%B3%95/): manacher 俗称马拉车算法,也是本文的主角,是一种能够将最长回文串的求解复杂度降低到 O(N) 的一种高效算法, 当我第一次见到求解最长回文串的题目时,首先采用的就是暴力解法,O(N平方) 复杂 … - Published 2018-04-17 - [无缓冲通道引发的BUG](https://blog.floatshell.com/blog/%E6%97%A0%E7%BC%93%E5%86%B2%E9%80%9A%E9%81%93%E5%BC%95%E5%8F%91%E7%9A%84bug/): 最近在刚玩具级的 MapReduce ,其中充斥着大量的并发编程的代码,稍有不慎就可能导致 Map 或者 Reduce 的时候遭遇到未知的阻塞状态 (Blocking),导致整个系统无法按照计划 处理 … - Published 2018-04-11 - [分布式初探 Distributed 101](https://blog.floatshell.com/blog/%E5%88%86%E5%B8%83%E5%BC%8F%E5%88%9D%E6%8E%A2-distributed-101/): 什么是分布式系统?使用多机来处理单机相同问题的系统 - Published 2018-04-06 - [0交换排序 Google笔试题](https://blog.floatshell.com/blog/0%E4%BA%A4%E6%8D%A2%E6%8E%92%E5%BA%8F-google%E7%AC%94%E8%AF%95%E9%A2%98/): 题目: 长度为n的数组乱序存放着0至n-1,现在只能进行0与其他数的交换,请排序这个数组 - Published 2018-03-29 - [在 Gin 中实践TDD测试API](https://blog.floatshell.com/blog/%E5%9C%A8-gin-%E4%B8%AD%E5%AE%9E%E8%B7%B5tdd%E6%B5%8B%E8%AF%95api/): 如果这篇文章能够从此让你放下手中的快递员,从此开始做一个 TDD First 的 programmer ,那么本文的目的也就达到了 - Published 2018-03-26 - [Go下解析XML文档时的编码问题](https://blog.floatshell.com/blog/go%E4%B8%8B%E8%A7%A3%E6%9E%90xml%E6%96%87%E6%A1%A3%E6%97%B6%E7%9A%84%E7%BC%96%E7%A0%81%E9%97%AE%E9%A2%98/): 最近在弄一些 RSS 解析的东西,记录一下如何解析非UTF-8编码 XML 文档,直接上代码 - Published 2018-03-10 - [使用tensorflow的Object Detection API做物体识别](https://blog.floatshell.com/blog/%E4%BD%BF%E7%94%A8tensorflow%E7%9A%84object-detection-api%E5%81%9A%E7%89%A9%E4%BD%93%E8%AF%86%E5%88%AB/): 学校有一个实验要做柑橘识别,因此想要用 tensorflow 用自己的训练集训练出一个只识别柑橘的模型出来,故有下文,别的物体识别也可以使用这种方式自己找数据去训练 - Published 2018-02-06 - [SQL基础简明总结](https://blog.floatshell.com/blog/sql%E5%9F%BA%E7%A1%80%E7%AE%80%E6%98%8E%E6%80%BB%E7%BB%93/): Inner Join 只含有关联的行 - Published 2017-11-04 - [Linux必知之网络基础篇](https://blog.floatshell.com/blog/linux%E5%BF%85%E7%9F%A5%E4%B9%8B%E7%BD%91%E7%BB%9C%E5%9F%BA%E7%A1%80%E7%AF%87/): 基本层次结构 - Published 2017-10-31 - [Linux日常之进程内存篇](https://blog.floatshell.com/blog/linux%E6%97%A5%E5%B8%B8%E4%B9%8B%E8%BF%9B%E7%A8%8B%E5%86%85%E5%AD%98%E7%AF%87/): 基本概念 内核:运行于硬件之上,是操作系统的核心,运行于内存中 进程:计算机中运行的所有程序,并由内核统一管理,这些进程组成了用户空间 - Published 2017-10-31 - [Linux日常之cron篇](https://blog.floatshell.com/blog/linux%E6%97%A5%E5%B8%B8%E4%B9%8Bcron%E7%AF%87/): cron 能够让我们创建 linux 的定时任务,是系统的内置服务,通过加入自己的配置来达到在某时间段重复执行任务的目的 - Published 2017-10-30 - [一文讲透koa-源码剖析](https://blog.floatshell.com/blog/%E4%B8%80%E6%96%87%E8%AE%B2%E9%80%8Fkoa-%E6%BA%90%E7%A0%81%E5%89%96%E6%9E%90/): 前言 本文从头开始由浅入深剖析现在十分流行的koa框架的核心源码,适合已经熟练掌握koa框架使用的开发人员阅读 - Published 2017-10-17 - [常见设计模式概要及其实践-TypeScript实现](https://blog.floatshell.com/blog/%E5%B8%B8%E8%A7%81%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F%E6%A6%82%E8%A6%81%E5%8F%8A%E5%85%B6%E5%AE%9E%E8%B7%B5-typescript%E5%AE%9E%E7%8E%B0/): 若有不足或纰漏,欢迎指教 前言: 得益于TypeScript强大灵活的OOP语法,使得在js中使用经典且可靠的OOP设计模式变成了现实,虽然有的公司已经在大规模地使用TS进行中大型项目的开发,但极少有 … - Published 2017-10-15 - [使用sftp搭建本地快速部署环境](https://blog.floatshell.com/blog/%E4%BD%BF%E7%94%A8sftp%E6%90%AD%E5%BB%BA%E6%9C%AC%E5%9C%B0%E5%BF%AB%E9%80%9F%E9%83%A8%E7%BD%B2%E7%8E%AF%E5%A2%83/): 准备 首先,我们要确保sshd也就是我们的ssh服务处于运行状态之中,下面以centos为例来讲解搭建的过程。 - Published 2017-09-10 - [用supertest进行API测试](https://blog.floatshell.com/blog/%E7%94%A8supertest%E8%BF%9B%E8%A1%8Capi%E6%B5%8B%E8%AF%95/): 在node开发中,写完了API接口之后,往往都要进行测试,现在常用的测试模块一般都是mocha和chai, 然后我们应该有相配合的请求模块来帮助我们更好地完成断言的工作,今天要介绍的主角就是 … - Published 2017-04-23 - [python 语法基础](https://blog.floatshell.com/blog/python-%E8%AF%AD%E6%B3%95%E5%9F%BA%E7%A1%80/): 记录了python跟别的语言不一样的一些基础知识点 - Published 2017-04-11 - [机器学习的常用概念](https://blog.floatshell.com/blog/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0%E7%9A%84%E5%B8%B8%E7%94%A8%E6%A6%82%E5%BF%B5/): 特征(属性) 通常是训练样本集的列,可看作是每个列的列名,比如区分鸟的品种可以用体重,后背颜色此类的特征来加以区分 - Published 2017-04-08 - [Linux WEB基础 学习笔记](https://blog.floatshell.com/blog/linux-web%E5%9F%BA%E7%A1%80-%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/): 系统版本选择 根据不同发行版本的特性 - Published 2017-03-29 - [Express下多次callback引发的问题](https://blog.floatshell.com/blog/express%E4%B8%8B%E5%A4%9A%E6%AC%A1callback%E5%BC%95%E5%8F%91%E7%9A%84%E9%97%AE%E9%A2%98/): 错误结果 Error: Can’t render headers after they are sent to the client. 错误:当响应头被发回客户端时不能被正确渲染! 出现场 … - Published 2017-03-23 - [夹杂数字字母大小写的字符串按字典排序方法](https://blog.floatshell.com/blog/%E5%A4%B9%E6%9D%82%E6%95%B0%E5%AD%97%E5%AD%97%E6%AF%8D%E5%A4%A7%E5%B0%8F%E5%86%99%E7%9A%84%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%8E%92%E5%BA%8F%E6%96%B9%E6%B3%95/): 核心步骤概要 过滤 过滤数字等特殊符号,只剩下字母 - Published 2016-11-04 ## LeetCode 题解 - [[969. Pancake Sorting 翻转排序][1]](https://blog.floatshell.com/tech-series-leetcode-solutions/969.-pancake-sorting-%E7%BF%BB%E8%BD%AC%E6%8E%92%E5%BA%8F/): 思路 因为是只能反转前 N 个元素,因此从后向前依次把最大的排好即可 最优解 class Solution { public: vector<int> … - Published 2026-02-05 - [1002. Find Common Characters 寻找公共字符](https://blog.floatshell.com/tech-series-leetcode-solutions/1002.-find-common-characters-%E5%AF%BB%E6%89%BE%E5%85%AC%E5%85%B1%E5%AD%97%E7%AC%A6/): 题目描述 // Author: Tecker // time: O(N) // space: O(1) vector<string> … - Published 2026-02-05 - [1004. Max Consecutive Ones III](https://blog.floatshell.com/tech-series-leetcode-solutions/1004.-max-consecutive-ones-iii/): 题目描述: Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of … - Published 2026-02-05 - [1014. Capacity To Ship Packages Within D Days 运输最小容量](https://blog.floatshell.com/tech-series-leetcode-solutions/1014.-capacity-to-ship-packages-within-d-days-%E8%BF%90%E8%BE%93%E6%9C%80%E5%B0%8F%E5%AE%B9%E9%87%8F/): 每天只能按顺序运送重 weight[i] 的物资,运输D天,求传送带的最小承重量 最小承重量为最重的那一个物资 最大承重量为所有物资的和,因为 D 至少可以为 1 这里有一个连续的搜索范围,妙用二分搜 … - Published 2026-02-05 - [1018. Binary Prefix Divisible By 5](https://blog.floatshell.com/tech-series-leetcode-solutions/1018.-binary-prefix-divisible-by-5/): Given an array A of 0s and 1s, consider N_i: the i-th subarray from A[0] to A[i] interpreted as a … - Published 2026-02-05 - [1024. Video Stitching](https://blog.floatshell.com/tech-series-leetcode-solutions/1024.-video-stitching/): 给定一组片段的头和尾,求能拼成 0-T 分钟的最少选择片段个数 DP 做法: 每一个 0-T 的每一个段都有一个选择个数,考虑每一个段与每一个给定片段的关系,可以分成五种情况 // time: … - Published 2026-02-05 - [1025. Divisor Game](https://blog.floatshell.com/tech-series-leetcode-solutions/1025.-divisor-game/): bool divisorGame(int N, bool res=false) { if (dp[N] != 0) return dp[N]==1; // 返回已经求过的解 for(int … - Published 2026-02-05 - [1029. Two City Scheduling](https://blog.floatshell.com/tech-series-leetcode-solutions/1029.-two-city-scheduling/): 有 2N 个人,需要被平均分配到 A 和 B 两座城市,每个人分配的花费给出,求最小花费? 题型:DP + 暴力搜索 每一个人都可以被分派到 A 或者 B 中,初始花费为0 每次向下传递的时候都可以在 … - Published 2026-02-05 - [1031. Maximum Sum of Two Non-Overlapping Subarrays](https://blog.floatshell.com/tech-series-leetcode-solutions/1031.-maximum-sum-of-two-non-overlapping-subarrays/): 两个不重叠定长子数组的最大和,两个数组的长度已给出 两种情况,长的区间在前,短的区间在后;长的在后,短的在前 int maxSumTwoNoOverlap(vector<int>& … - Published 2026-02-05 - [1078. Occurrences After Bigram](https://blog.floatshell.com/tech-series-leetcode-solutions/1078.-occurrences-after-bigram/): 给一个句子,英文单词之间用空格分隔开,给定第一个词和第二个词,将接着的所有第三个词都返回 只要查找所有的:词1 + 空格 + 词2 + 空格 即可 // time: O(N) // space: … - Published 2026-02-05 - [1079. Letter Tile Possibilities](https://blog.floatshell.com/tech-series-leetcode-solutions/1079.-letter-tile-possibilities/): 求一串含有重复字符的字符串的非空子序列个数? 首先,子序列的长度最短是1,最长是串长 然后对每一个长度的串的每一个字符,我们可以选串中的某一字符A,或者下一个非A字符(避免重复) class … - Published 2026-02-05 - [1080. Insufficient Nodes in Root to Leaf Paths](https://blog.floatshell.com/tech-series-leetcode-solutions/1080.-insufficient-nodes-in-root-to-leaf-paths/): 根到叶子路径和严格小于某个给定值,那么这些叶子节点进行修剪 rejudge … #tree - Published 2026-02-05 - [1081. Smallest Subsequence of Distinct Characters](https://blog.floatshell.com/tech-series-leetcode-solutions/1081.-smallest-subsequence-of-distinct-characters/): 找出只含字母的字符串中的字典序最小的不含重复字符的字符序列 本题的一个关键点是字典序最小,另一个是序列,因此是顺序遍历 然后为了字典序最小,我们需要贪心进行遍历,如果当前构造的子序列的最后字符还可以更 … - Published 2026-02-05 - [109. Convert Sorted List to Binary Search Tree](https://blog.floatshell.com/tech-series-leetcode-solutions/109.-convert-sorted-list-to-binary-search-tree/): 有序链表转平衡二叉搜索树 首先看到平衡,意味着我们每次都需要以链表的中点为根来构建平衡二叉搜索树 重点: 链表找中点 -> 快慢指针 对链表进行切分 -> 保证每次递归使用快慢指针的时候能 … - Published 2026-02-05 - [110. Balanced Binary Tree](https://blog.floatshell.com/tech-series-leetcode-solutions/110.-balanced-binary-tree/): 检查树是否平衡,即左右子树的高度差小于等于1 // time: O(N) bool isBalanced(TreeNode* root) { if (root==NULL) return true; … - Published 2026-02-05 - [112. Path Sum](https://blog.floatshell.com/tech-series-leetcode-solutions/112.-path-sum/): Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all … - Published 2026-02-05 - [12. Integer to Roman 数字转罗马串](https://blog.floatshell.com/tech-series-leetcode-solutions/12.-integer-to-roman-%E6%95%B0%E5%AD%97%E8%BD%AC%E7%BD%97%E9%A9%AC%E4%B8%B2/): 原题描述 Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range … - Published 2026-02-05 - [1233. Remove Sub-Folders from the Filesystem](https://blog.floatshell.com/tech-series-leetcode-solutions/1233.-remove-sub-folders-from-the-filesystem/): 给一系列目录字符串,要求只返回父目录 排序再去除 O(nlogN) 排序之后,可能为父子目录关系的目录便会在一起且第一个肯定为父目录,通过比对字符串前缀是否相等来去重 将层级问题通过排序简化为线性问题 … - Published 2026-02-05 - [131. Palindrome Partitioning 回文子串分割](https://blog.floatshell.com/tech-series-leetcode-solutions/131.-palindrome-partitioning-%E5%9B%9E%E6%96%87%E5%AD%90%E4%B8%B2%E5%88%86%E5%89%B2/): 思路 既然是子串,就有 start 和 end 两个端点,遍历字符串,start 从 0 开始,推进 end 寻找回文串,找到之后,将 start 继续向前推进,重复上述过程,这个过程可以递归查找,但 … - Published 2026-02-05 - [134. Gas Station 加油问题](https://blog.floatshell.com/tech-series-leetcode-solutions/134.-gas-station-%E5%8A%A0%E6%B2%B9%E9%97%AE%E9%A2%98/): 每个油站有汽油值和到下一个站需要的汽油值,问:从哪个下标的油站开始走(初始汽油为0)能够走完所有的油站,走不到返回 -1 隐含条件: 所有油站的汽油值和必须大于所有 cost 之和 如果从某起点到某终 … - Published 2026-02-05 - [135. Candy 分糖果](https://blog.floatshell.com/tech-series-leetcode-solutions/135.-candy-%E5%88%86%E7%B3%96%E6%9E%9C/): int candy(vector<int>& rate) { int len=rate.size(); int res=len; if (len<=1) return … - Published 2026-02-05 - [1409. Queries on a Permutation With Key](https://blog.floatshell.com/tech-series-leetcode-solutions/1409.-queries-on-a-permutation-with-key/): 数组 1到M ,输入查询数组,每次查询元素所在的下标,查询完后元素要往头部移动,M不会超过1000 暴力解法 因为M的数量限制,因此可以使用暴力解法进行遍历和移动元素 class Solution { … - Published 2026-02-05 - [15. 3Sum](https://blog.floatshell.com/tech-series-leetcode-solutions/15.-3sum/): 题目描述:给定一个数组,寻找所有不重复的三元组使得它们的和为0 核心思想 3sum 的思想其实跟 2sum 的思想非常类似,都是利用双指针的技巧来解题,区别在于需要进行去重,已经排好序的数组去重其实只 … - Published 2026-02-05 - [152. Maximum Product Subarray](https://blog.floatshell.com/tech-series-leetcode-solutions/152.-maximum-product-subarray/): 因为存在正负数相乘的问题,如果是负数,那么乘的记忆值越小越好,如果是正数,乘的记忆值越大越好,因此这里我们需要记录两个值,当前下标前的最小值和下标前的最大值 int … - Published 2026-02-05 - [162. Find Peak Element](https://blog.floatshell.com/tech-series-leetcode-solutions/162.-find-peak-element/): 寻找峰顶元素的下标 简单画一个图,可以发现山峰元素和它的下一个元素的关系 O(N) int findPeakElement(const vector<int> &num) { … - Published 2026-02-05 - [165. Compare Version Numbers 版本号比较](https://blog.floatshell.com/tech-series-leetcode-solutions/165.-compare-version-numbers-%E7%89%88%E6%9C%AC%E5%8F%B7%E6%AF%94%E8%BE%83/): 重点在于 stringstream 的使用 int compareVersion(string ver1, string ver2) { stringstream s1(ver1); … - Published 2026-02-05 - [168. Excel Sheet Column Title](https://blog.floatshell.com/tech-series-leetcode-solutions/168.-excel-sheet-column-title/): 根据数字转换成 excel 英文列名 string convertToTitle(int n) { string res=""; while(n) { int a = n%26; if … - Published 2026-02-05 - [172. Factorial Trailing Zeroes](https://blog.floatshell.com/tech-series-leetcode-solutions/172.-factorial-trailing-zeroes/): 数阶乘结果的末尾0个数 根据奇偶性可知 2 的个数一定多于 5 的个数,因此 0 的个数取决于 5 的个数,递归数比为5,起始为 5 的等比数列的 5 的个数,递归求解即可 int … - Published 2026-02-05 - [173. Binary Search Tree Iterator](https://blog.floatshell.com/tech-series-leetcode-solutions/173.-binary-search-tree-iterator/): 刚开始的思路比较暴力,直接构建一个 vector 来从小到大存节点的值 class BSTIterator { public: BSTIterator(TreeNode* root) { if … - Published 2026-02-05 - [190. Reverse Bits 逆置位序列](https://blog.floatshell.com/tech-series-leetcode-solutions/190.-reverse-bits-%E9%80%86%E7%BD%AE%E4%BD%8D%E5%BA%8F%E5%88%97/): 解法:多次分组逆置 abcdefgh -> efghabcd -> ghefcdab -> hgfedcba 前 16 位后 16 位交换,然后 16 位中的前 8 位和后 8 位交 … - Published 2026-02-05 - [201. Bitwise AND of Numbers Range](https://blog.floatshell.com/tech-series-leetcode-solutions/201.-bitwise-and-of-numbers-range/): 输入int类型的两个数 m和n,求他们范围内数字的位与运算的结果 m一直增长到n,无论怎么与,只要它们不相等,中间一定存在奇数和偶数,相与过后的位一定为0,因此n相对m左对齐的相同部分才可能出现1 … - Published 2026-02-05 - [202. Happy Number 快乐数](https://blog.floatshell.com/tech-series-leetcode-solutions/202.-happy-number-%E5%BF%AB%E4%B9%90%E6%95%B0/): n 如果满足各个位数字的平方和重新赋值给 n,循环操作n能为1,则初始的n为快乐数 本题考查对结果循环的检测,应用快慢指针检测环的思想来检测 n 经过多次迭代操作后是否存在循环 Floyd Cycle … - Published 2026-02-05 - [204. Count Primes](https://blog.floatshell.com/tech-series-leetcode-solutions/204.-count-primes/): 求小于 N 的素数个数,N >= 0 素数筛 class Solution { public: int countPrimes(int n) { if (n == 0) return 0; … - Published 2026-02-05 - [205. Isomorphic Strings](https://blog.floatshell.com/tech-series-leetcode-solutions/205.-isomorphic-strings/): 同构字符串的定义:串S中的某一种字符可以替换成另一种字符使得与串T相同,不限替换次数,替换映射要求两种不同字符不能映射到同一字符 class Solution { public: bool … - Published 2026-02-05 - [215. Kth Largest Element in an Array 第 K 大的数](https://blog.floatshell.com/tech-series-leetcode-solutions/215.-kth-largest-element-in-an-array-%E7%AC%AC-k-%E5%A4%A7%E7%9A%84%E6%95%B0/): 第一次解法 从大到小排序然后输出 K-1 的元素 时间复杂度: n(logn) int findKthLargest(vector<int>& nums, int k) { … - Published 2026-02-05 - [236. Lowest Common Ancestor of a Binary Tree](https://blog.floatshell.com/tech-series-leetcode-solutions/236.-lowest-common-ancestor-of-a-binary-tree/): 寻找最近公共祖先 class Solution { public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, … - Published 2026-02-05 - [26. Remove Duplicates from Sorted Array 已排序数组原地去重](https://blog.floatshell.com/tech-series-leetcode-solutions/26.-remove-duplicates-from-sorted-array-%E5%B7%B2%E6%8E%92%E5%BA%8F%E6%95%B0%E7%BB%84%E5%8E%9F%E5%9C%B0%E5%8E%BB%E9%87%8D/): 题目描述 第一次解法:多情况讨论 int removeDuplicates(vector<int>& nums) { if (nums.size() <= 1) return … - Published 2026-02-05 - [28. Implement strStr()](https://blog.floatshell.com/tech-series-leetcode-solutions/28.-implement-strstr/): 暴力解法 int strStr(string haystack, string needle) { if(needle.size()==0) return 0; if … - Published 2026-02-05 - [29. Divide Two Integers 实现除法](https://blog.floatshell.com/tech-series-leetcode-solutions/29.-divide-two-integers-%E5%AE%9E%E7%8E%B0%E9%99%A4%E6%B3%95/): 除了防止溢出这个坑之外,我们还要关注的是如何递增得尽可能得快来减少运算次数 int divide(int a, int b) { if (a == INT_MIN && b == … - Published 2026-02-05 - [3. Longest Substring Without Repeating Characters](https://blog.floatshell.com/tech-series-leetcode-solutions/3.-longest-substring-without-repeating-characters/): 最长不含重复字符的子字符串 int lengthOfLongestSubstring(string s) { if (s.size() == 0) return 0; int curLen = 0; … - Published 2026-02-05 - [338. Counting Bits 位计数](https://blog.floatshell.com/tech-series-leetcode-solutions/338.-counting-bits-%E4%BD%8D%E8%AE%A1%E6%95%B0/): 题目描述:给定一个非负整数,给出 0这个整数的每一个数的二进制中1的个数 Input: 2 Output: [0,1,1] Input: 5 Output: [0,1,1,2,1,2] 第一种解法:找 … - Published 2026-02-05 - [34. Find First and Last Position of Element in Sorted Array 寻找排序数组目标的开始和结束](https://blog.floatshell.com/tech-series-leetcode-solutions/34.-find-first-and-last-position-of-element-in-sorted-array-%E5%AF%BB%E6%89%BE%E6%8E%92%E5%BA%8F%E6%95%B0%E7%BB%84%E7%9B%AE%E6%A0%87%E7%9A%84%E5%BC%80%E5%A7%8B%E5%92%8C%E7%BB%93%E6%9D%9F/): 解法:二分查找 关键在于如何定位左端点与右端点 vector<int> searchRange(vector<int>& nums, int target) { int … - Published 2026-02-05 - [347. Top K Frequent Elements 出现频率TopK的元素](https://blog.floatshell.com/tech-series-leetcode-solutions/347.-top-k-frequent-elements-%E5%87%BA%E7%8E%B0%E9%A2%91%E7%8E%87topk%E7%9A%84%E5%85%83%E7%B4%A0/): 桶排序 时间复杂度:O(n) 构建频率字典 O(n) 将数字放置进对应频率下标的桶中 O(n) 从后向前遍历桶,将元素取出到结果中,最后返回结果。最坏情况:O(n) vector<int> … - Published 2026-02-05 - [36. Valid Sudoku 判断部分数独图是否有效](https://blog.floatshell.com/tech-series-leetcode-solutions/36.-valid-sudoku-%E5%88%A4%E6%96%AD%E9%83%A8%E5%88%86%E6%95%B0%E7%8B%AC%E5%9B%BE%E6%98%AF%E5%90%A6%E6%9C%89%E6%95%88/): 特殊的遍历方式,下标作为简单的哈希表 time: O(N²) bool isValidSudoku(vector<vector<char>>& board) { int … - Published 2026-02-05 - [37. Sudoku Solver 数独解题器](https://blog.floatshell.com/tech-series-leetcode-solutions/37.-sudoku-solver-%E6%95%B0%E7%8B%AC%E8%A7%A3%E9%A2%98%E5%99%A8/): 主要的解法就是 DFS 遍历,填入 1 - 9 的数字,通过预处理已有的限制条件来进行剪枝,减少解空间 class Solution { public: void … - Published 2026-02-05 - [371. Sum of Two Integers](https://blog.floatshell.com/tech-series-leetcode-solutions/371.-sum-of-two-integers/): 题目描述:不用+或 - 实现两个 int 数相加 核心思想 不带进位的相加可以用位间的异或 ^ 表示,进位分为两种情况,第一是两位都为1,另一种情况是两位分别为0和1然后进位为1的时候才向前进位,向前 … - Published 2026-02-05 - [413. Arithmetic Slices 算术切片](https://blog.floatshell.com/tech-series-leetcode-solutions/413.-arithmetic-slices-%E7%AE%97%E6%9C%AF%E5%88%87%E7%89%87/): 第一种解法:二维DP 暴力考虑所有长度大于3的切片,这种方法较慢 更好的解法:一维DP dp[i] 表示以 A[i] 结尾的切片的算术切片个数,在原有的算术切片的基础上再纳入一个新的数字,只会比以前一 … - Published 2026-02-05 - [437. Path Sum III](https://blog.floatshell.com/tech-series-leetcode-solutions/437.-path-sum-iii/): 题目描述:不需要从根到叶的,但是要求是需要从上到下的节点,找出所有的路径使得节点的值的和等于目标值,返回路径的个数 核心思想 递归计数的技巧,不需要从根到叶的路径,只不过是将搜索的起点作为每一个起点即 … - Published 2026-02-05 - [451. Sort Characters By Frequency 按出现频率排序字符](https://blog.floatshell.com/tech-series-leetcode-solutions/451.-sort-characters-by-frequency-%E6%8C%89%E5%87%BA%E7%8E%B0%E9%A2%91%E7%8E%87%E6%8E%92%E5%BA%8F%E5%AD%97%E7%AC%A6/): 桶排序的思想 时间复杂度:O(n) string frequencySort(string s) { if (s.empty()) return s; // 频率统计 … - Published 2026-02-05 - [50. Pow(x, n) 实现pow乘方函数](https://blog.floatshell.com/tech-series-leetcode-solutions/50.-powx-n-%E5%AE%9E%E7%8E%B0pow%E4%B9%98%E6%96%B9%E5%87%BD%E6%95%B0/): double myPow(double x, int n) { double res=1.0; for(int i=n;i!=0;i/=2) { // 奇数就单独乘一次,偶数总会除到 1 if … - Published 2026-02-05 - [503. Next Greater Element II](https://blog.floatshell.com/tech-series-leetcode-solutions/503.-next-greater-element-ii/): 第一次解法 暴力遍历整个数组,时间复杂度 O(N²) 每个元素都从头开始找并记录比它大的元素的下标,在元素左边的时候总是更新,在元素右边的时候只要找到一个符合要求的就 break 找下一个,找不到则 … - Published 2026-02-05 - [567. Permutation in String 字符串中的组合](https://blog.floatshell.com/tech-series-leetcode-solutions/567.-permutation-in-string-%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%AD%E7%9A%84%E7%BB%84%E5%90%88/): 描述:给定字符串 s1 和 s2,问在 s2 的子串中是否有能通过 s1 的排列组合得到的 解法一:定长数组滑动 12ms 子串和 s1 会有相同的哈希签名,每往前滑动一格,子串签名只需要加上下一字符 … - Published 2026-02-05 - [60. Permutation Sequence 第 K 个字典序排列](https://blog.floatshell.com/tech-series-leetcode-solutions/60.-permutation-sequence-%E7%AC%AC-k-%E4%B8%AA%E5%AD%97%E5%85%B8%E5%BA%8F%E6%8E%92%E5%88%97/): 解法一:枚举 K-1 次 代码量极短,好想但是比较耗时 时间复杂度:O(n!) 空间复杂度:O(n) string getPermutation(int n, int k) { string a(n, … - Published 2026-02-05 - [636. Exclusive Time of Functions](https://blog.floatshell.com/tech-series-leetcode-solutions/636.-exclusive-time-of-functions/): 思路 函数按照时间顺序执行就是一个调用栈的结构,因此这是一道简单运用栈按照题目说明进行模拟函数调用的题目,主要的坑在于花费时间的计算,看清题意 Solution #include … - Published 2026-02-05 - [647. Palindromic Substrings 回文子字符串](https://blog.floatshell.com/tech-series-leetcode-solutions/647.-palindromic-substrings-%E5%9B%9E%E6%96%87%E5%AD%90%E5%AD%97%E7%AC%A6%E4%B8%B2/): 第一次解法 从 DP 的角度考虑,因为需要所有所有的子串,因此需要一个二维数组来存放结果,dp[i][j] 表示下标 i - j 的字符串是回文串,如果 i == j ,则计数器+1,并标记 … - Published 2026-02-05 - [687. Longest Univalue Path](https://blog.floatshell.com/tech-series-leetcode-solutions/687.-longest-univalue-path/): 题目描述:在二叉树中寻找任意一条路径,路径上的节点的值都相等,求路径包含的边数最大值 第一次解法 灵感来源自 path sum 的递归计数,只不过我们每次都要取最大值,比较麻烦的 corner … - Published 2026-02-05 - [712. Minimum ASCII Delete Sum for Two Strings 串相等的最小删除代价](https://blog.floatshell.com/tech-series-leetcode-solutions/712.-minimum-ascii-delete-sum-for-two-strings-%E4%B8%B2%E7%9B%B8%E7%AD%89%E7%9A%84%E6%9C%80%E5%B0%8F%E5%88%A0%E9%99%A4%E4%BB%A3%E4%BB%B7/): dp[i][j] 代表 串A的i-1位字符,串B的j-1位字符,对于每一个 i-1 来说,j-1都有删和不删的选择,如果这两个字符相等,那么选择不删,dp[i][j]=dp[i-1][j-1],否则需 … - Published 2026-02-05 - [75. Sort Colors 荷兰国旗问题](https://blog.floatshell.com/tech-series-leetcode-solutions/75.-sort-colors-%E8%8D%B7%E5%85%B0%E5%9B%BD%E6%97%97%E9%97%AE%E9%A2%98/): 解法:快排思想的运用 使用三指针即可 void sortColors(vector<int>& nums) { for(int … - Published 2026-02-05 - [859. Buddy Strings](https://blog.floatshell.com/tech-series-leetcode-solutions/859.-buddy-strings/): 原题:两个小写字母组成的字符串当且仅当串 A 交换两个字符与串 B 相等的时候返回 true 否则返回 false 第一次解法 根据题意直白来解,如果仅仅是交换字符能相等的话那么两个字符的哈希签名必须 … - Published 2026-02-05 - [877. Stone Game 石子游戏](https://blog.floatshell.com/tech-series-leetcode-solutions/877.-stone-game-%E7%9F%B3%E5%AD%90%E6%B8%B8%E6%88%8F/): 题目描述 Alex 先拿,石子总数为奇数,每次只能拿前或后,两个人都做最优的选择,谁的石子数多谁赢,Alex 赢返回 true 定义二维数组 dp[i][j] 表示下标 i至j的元素区间先拿的人和后拿 … - Published 2026-02-05 - [89. Gray Code 格雷码](https://blog.floatshell.com/tech-series-leetcode-solutions/89.-gray-code-%E6%A0%BC%E9%9B%B7%E7%A0%81/): 当前数字 n 的格雷码 = (n>>1) ^ n vector<int> grayCode(int n) { vector<int> res; for(int … - Published 2026-02-05 - [95. Unique Binary Search Trees II](https://blog.floatshell.com/tech-series-leetcode-solutions/95.-unique-binary-search-trees-ii/): 给定 N ,生成所有含有节点 1-N 的二叉搜索树序列 每一个值都可以做为根,递归分治,让左边的元素组成左子树,右边的元素组成右子树,将问题的规模进一步减小 class Solution { … - Published 2026-02-05 - [952. Largest Component Size by Common Factor](https://blog.floatshell.com/tech-series-leetcode-solutions/952.-largest-component-size-by-common-factor/): Given a non-empty array of unique positive integers A, consider the following graph: There are … - Published 2026-02-05 - [96. Unique Binary Search Trees](https://blog.floatshell.com/tech-series-leetcode-solutions/96.-unique-binary-search-trees/): 目标 G(n) = 以 1-n 每一个数字为根 F(1,n) + F(2,n) + F(3,n) …. + F(n,n) F(i,n) = G(i-1) * G(n-i) 左右子树种数相乘 (和是 … - Published 2026-02-05 - [970. Powerful Integers 平方数](https://blog.floatshell.com/tech-series-leetcode-solutions/970.-powerful-integers-%E5%B9%B3%E6%96%B9%E6%95%B0/): Given two non-negative integers x and y, an integer is powerful if it is equal to x^i + y^j for some … - Published 2026-02-05 - [971. Flip Binary Tree To Match Preorder Traversal](https://blog.floatshell.com/tech-series-leetcode-solutions/971.-flip-binary-tree-to-match-preorder-traversal/): Given a binary tree with N nodes, each node has a different value from 1, …, N. A node in … - Published 2026-02-05 - [977. Squares of a Sorted Array](https://blog.floatshell.com/tech-series-leetcode-solutions/977.-squares-of-a-sorted-array/): 原题地址 题目描述 给定一递增数组,返回元素平方数构成的数组,同样要求递增 用例 Input: [-4,-1,0,3,10] Output: [0,1,9,16,100] Input: … - Published 2026-02-05 - [98. Validate Binary Search Tree 验证二叉搜索树](https://blog.floatshell.com/tech-series-leetcode-solutions/98.-validate-binary-search-tree-%E9%AA%8C%E8%AF%81%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91/): 考察二叉树的中序遍历 time: O(logN) 平均树高度,最坏 O(N) space: O(logN) 栈空间和前继节点 bool isValidBST(TreeNode* root) { … - Published 2026-02-05 - [983. Minimum Cost For Tickets 买票的最小花费](https://blog.floatshell.com/tech-series-leetcode-solutions/983.-minimum-cost-for-tickets-%E4%B9%B0%E7%A5%A8%E7%9A%84%E6%9C%80%E5%B0%8F%E8%8A%B1%E8%B4%B9/): 题目描述:给定三种票分别是 1 日,7日和30日以及它们的票价costs,给定一年中的需要出行的日子,求最小花费 分析 典型的DP题,需要先搞清楚递推式,如果是第N天买票,那么总共的花费可以分成三种情 … - Published 2026-02-05 - [985. Sum of Even Numbers After Queries](https://blog.floatshell.com/tech-series-leetcode-solutions/985.-sum-of-even-numbers-after-queries/): 题目描述 时间复杂度:O(N) 空间复杂度:O(1) // Author: Tecker // 176ms, 28.7MB; beat 96.40%, 100% class Solution { … - Published 2026-02-05 - [989. Add to Array-Form of Integer](https://blog.floatshell.com/tech-series-leetcode-solutions/989.-add-to-array-form-of-integer/): 题目描述 For a non-negative integer X, the array-form of X is an array of its digits in left to right … - Published 2026-02-05 - [997. Find the Town Judge](https://blog.floatshell.com/tech-series-leetcode-solutions/997.-find-the-town-judge/): 原题 题目给定了所有的单向连接的关系,需要我们找出被所有其他节点所连接的唯一节点,且这个节点不指向任何节点 刚开始想要用并查集来做,犯了错误,那就是并查集的连接其实是具有传递性的,更适合用于无向连接, … - Published 2026-02-05 - [Best Time to Buy and Sell Stock 买卖股票最佳时间系列](https://blog.floatshell.com/tech-series-leetcode-solutions/best-time-to-buy-and-sell-stock-%E4%B9%B0%E5%8D%96%E8%82%A1%E7%A5%A8%E6%9C%80%E4%BD%B3%E6%97%B6%E9%97%B4%E7%B3%BB%E5%88%97/): 第一题 只能完成一次交易,求最大收益? 解法类似于最大的连续子数组和,区别在于连续子数组的和取决于当前元素与当前元素加上之前连续和的最大值,股票的买卖的价位只取决于递增子数组中右边最大减去左边最小的值 … - Published 2026-02-05 - [Combination Sum 系列](https://blog.floatshell.com/tech-series-leetcode-solutions/combination-sum-%E7%B3%BB%E5%88%97/): 一次性把 Combination Sum 有关问题都做了一遍,对回溯搜索穷举又有了更加深刻的理解,在此总结 核心思想 递归选择后回溯当前选择,这在组合穷举搜寻的题目中都非常有用 void … - Published 2026-02-05 - [Construct Binary Tree 系列](https://blog.floatshell.com/tech-series-leetcode-solutions/construct-binary-tree-%E7%B3%BB%E5%88%97/): 题目的意思是根据已有二叉树的两种遍历顺序序列逆向还原出已有的二叉树 树的三种遍历方式 前序遍历 (preorder) :中 - 左 - 右 中序遍历 (inorder) : 左 - 中 - 右 后序遍 … - Published 2026-02-05 - [Decode Ways 系列](https://blog.floatshell.com/tech-series-leetcode-solutions/decode-ways-%E7%B3%BB%E5%88%97/): 91. Decode Ways 给一串数字,求解码成 A-Z 有多少种解码方式? 每一个位置都存在取一位和取两位的情况 如果取一位的时候为合法序列,两位也合法,种数就是前面两个之和 只有一位合法,种 … - Published 2026-02-05 - [LRU cache](https://blog.floatshell.com/tech-series-leetcode-solutions/lru-cache/): // author: Tecker Yu // time: 92 ms, 99.55% // mem: 38 M, 84.45% class LRUCache { public: struct … - Published 2026-02-05 - [Populating Next Right Pointers in Each Node 系列](https://blog.floatshell.com/tech-series-leetcode-solutions/populating-next-right-pointers-in-each-node-%E7%B3%BB%E5%88%97/): 题目的意思是使用 next 指针连接树中同一层次的节点,主要的难点在于空间复杂度必须在常数级 题型一 高度差为 0 的完美满二叉树 特点:每一个节点都有一个相邻接点,比如有左节点,那么它的下一个就是右 … - Published 2026-02-05 - [Power of 系列](https://blog.floatshell.com/tech-series-leetcode-solutions/power-of-%E7%B3%BB%E5%88%97/): 如何判断一个数是否为基数 i 的 N 次方? 最简单的方式就是看它能不能不断地整除基数 // i = 2 bool isPowerOfTwo(int n) { while(n!=0 … - Published 2026-02-05 - [Search in Rotated Sorted Array 旋转数组查找](https://blog.floatshell.com/tech-series-leetcode-solutions/search-in-rotated-sorted-array-%E6%97%8B%E8%BD%AC%E6%95%B0%E7%BB%84%E6%9F%A5%E6%89%BE/): No.33 在没有重复数字的升序数组中查找值并返回下标 int search(vector<int>& nums, int target) { int start=0; int … - Published 2026-02-05 - [Single Number 系列](https://blog.floatshell.com/tech-series-leetcode-solutions/single-number-%E7%B3%BB%E5%88%97/): 本题总共有 3 题,其中的核心思想在于利用位运算来实现一个计数器使得从 0 开始计数到 K 时能够自动重置为 0,这样当我们遍历整个数组的每一个数的时候,重复出现 K 次的数字输入设计好的布尔表达式进 … - Published 2026-02-05 - [String to Integer (atoi) 字符串转数字](https://blog.floatshell.com/tech-series-leetcode-solutions/string-to-integer-atoi-%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%BD%AC%E6%95%B0%E5%AD%97/): 原题地址 主要的难点在于溢出的判断 判断依据: 将极值去掉最后一位(也就是除以10) 来判断,如果数字已经比这个去掉个位的数字要大的话,添上最后一位必会溢出,如果等于的话,比较极值的个位数字与即将添加 … - Published 2026-02-05 - [Word Break 系列](https://blog.floatshell.com/tech-series-leetcode-solutions/word-break-%E7%B3%BB%E5%88%97/): 给定字符串和一个字典,问字符串手否能够能够被字典中的词完美组合出来 解法一:DP[i] 表示串从起始位置到 i 位置都能够用词典中的词构成,向前推进即可 // author: Tecker Yu // … - Published 2026-02-05 - [常用 STL 操作](https://blog.floatshell.com/tech-series-leetcode-solutions/%E5%B8%B8%E7%94%A8-stl-%E6%93%8D%E4%BD%9C/): 分割字符串 #include <sstream> stringstream ss (s); getline(ss, sno, ':'); // 结果放置在字符串 sno 中 … - Published 2026-02-05 - [单词阶梯系列](https://blog.floatshell.com/tech-series-leetcode-solutions/%E5%8D%95%E8%AF%8D%E9%98%B6%E6%A2%AF%E7%B3%BB%E5%88%97/): 题目1描述:给定起始词和终止词,以及一个单词表,找到起始词每次只能变一个字母且中间词必须在字典里,返回最小变换次数 核心思路 根据定义构建邻接表,然后进行宽搜,找到终止词或搜索结束后返回结果即可 时间 … - Published 2026-02-05 - [二叉树与链表专题](https://blog.floatshell.com/tech-series-leetcode-solutions/%E4%BA%8C%E5%8F%89%E6%A0%91%E4%B8%8E%E9%93%BE%E8%A1%A8%E4%B8%93%E9%A2%98/): 114. Flatten Binary Tree to Linked List 将二叉树原地转换成链表,按照先序遍历的顺序 自底向上构造 class Solution { public: void … - Published 2026-02-05 - [滑动窗口专题](https://blog.floatshell.com/tech-series-leetcode-solutions/%E6%BB%91%E5%8A%A8%E7%AA%97%E5%8F%A3%E4%B8%93%E9%A2%98/): 209. Minimum Size Subarray Sum 给定一个都是正数的数组和值s,求满足和≥s的连续子数组的最小长度,没有则返回0 class Solution { public: int … - Published 2026-02-05 - [矩阵旋转](https://blog.floatshell.com/tech-series-leetcode-solutions/%E7%9F%A9%E9%98%B5%E6%97%8B%E8%BD%AC/): 先上下反转,然后对角线对称( i 和 j 互换) void rotate(vector<vector<int>>& mat) { … - Published 2026-02-05 - [正则匹配系列](https://blog.floatshell.com/tech-series-leetcode-solutions/%E6%AD%A3%E5%88%99%E5%8C%B9%E9%85%8D%E7%B3%BB%E5%88%97/): leetcode44 Given an input string (s) and a pattern (p), implement wildcard pattern matching with … - Published 2026-02-05 - [字符串翻转](https://blog.floatshell.com/tech-series-leetcode-solutions/%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%BF%BB%E8%BD%AC/): 问题描述:把前 m 个字符搬到尾部 暴力翻转 首先先将一个字符移到尾部,然后重复 m 次,时间复杂度 O(mn),空间 O(1) 三次翻转 时间复杂度 O(N) 空间 O(1) 分割成两段分别翻转, … - Published 2026-02-05 ## 分布式系统 - [Protobuf](https://blog.floatshell.com/tech-series-distributed-system/protobuf/): 是一种数据描述语言,一种紧凑的二进制消息格式,适合用于数据传输,数据存储。 消息长度:protobuf打包的数据不自带长度信息,需要应用程序自己在发送和接收消息的时候做切分 因此消息头部会有固定的长度 … - Published 2026-01-27 - [Raft](https://blog.floatshell.com/tech-series-distributed-system/raft/): 一致性算法正是用于解决分布式环境下多副本之间数据一致性的问题 通过选举Leader并由Leader节点负责管理日志复制来实现多副本的一致性 流程图 角色 Leader:负责接收客户端的请求,将日志复制 … - Published 2026-01-27 - [分布式 uuid 生成](https://blog.floatshell.com/tech-series-distributed-system/%E5%88%86%E5%B8%83%E5%BC%8F-uuid-%E7%94%9F%E6%88%90/): 可用于唯一ID生成,可应用于短链接生成 Twitter SnowFlake 发号器转短链接 生成一个64位的 ID ,正好可以用 long 整型存储,组成部分为 0 - 41位时间戳 - 5位数据中心 … - Published 2026-01-27 - [分布式哈希表 DHT](https://blog.floatshell.com/tech-series-distributed-system/%E5%88%86%E5%B8%83%E5%BC%8F%E5%93%88%E5%B8%8C%E8%A1%A8-dht/): Chord 协议 基于一致性哈希,维护一个类似于双向链表的环,节点可以加入,传递数据,退出 加入: 任何一个新来的节点(假设叫 A),需要先跟 DHT 中已有的任一节点(假设叫 B)建立连接 A 随机 … - Published 2026-01-27 - [分布式理论基础](https://blog.floatshell.com/tech-series-distributed-system/%E5%88%86%E5%B8%83%E5%BC%8F%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80/): 经典拜占庭将军问题 故事概要:多个军队之间的作战计划一致性的问题,即便某些军队的信使传递了错误的消息,也能够维持最后的投票结果一致性 口信消息型解法:如果叛将人数为 m,将军人数不能少于 3m + 1 … - Published 2026-01-27 - [架构演进](https://blog.floatshell.com/tech-series-distributed-system/%E6%9E%B6%E6%9E%84%E6%BC%94%E8%BF%9B/): 反向代理,负载均衡和缓存 数据库读写分离,写的时候同时往缓存写,写库与读库隔一段时间进行同步 数据库分库分表,大表化小表,数据副本。使用分布式数据库 LVS为 nginx 做负载均衡 异地 DNS … - Published 2026-01-27 - [时间轮](https://blog.floatshell.com/tech-series-distributed-system/%E6%97%B6%E9%97%B4%E8%BD%AE/): 实现高性能定时器 避免对定时任务进行轮询,以时间为优先级放入小顶堆,拿出最小的时间,并与当前时间相减,在这个相差的时间中,任务队列都可以休眠,直到被唤醒,执行完任务后再将新的任务加入,算时间差,继续休 … - Published 2026-01-27 - [一致性哈希算法](https://blog.floatshell.com/tech-series-distributed-system/%E4%B8%80%E8%87%B4%E6%80%A7%E5%93%88%E5%B8%8C%E7%AE%97%E6%B3%95/): 为了解决集群中机器变动或各种原因导致的缓存失效的问题,我们使用一个哈希环,通过对2^32取模来将进来的键值分布在一个 0 到 2^32 范围的一个环上,将机器的唯一标识符(IP地址)分布在整个环上,这 … - Published 2026-01-27 ## Go 标准库源码剖析 - [fmt 包](https://blog.floatshell.com/tech-series-go-stdlib-analysis/fmt-%E5%8C%85/): 从 hello world 开始使用频率最高最常用的 fmt.Println 开始切入,逐层由外向内剖析 打印函数默认输出到系统的标准输出即 os.Stdout ,鉴于包中有更通用的 Fprintln … - Published 2026-01-31 - [RPC](https://blog.floatshell.com/tech-series-go-stdlib-analysis/rpc/): 这里send需要上锁是是因为一个rpcClient可以支持并发发送请求。pending这个map是用来存目前client正在处理的call,其中key用seq来标记,seq单调递增,这个seq类似在对 … - Published 2026-01-31 - [RWMutex读写互斥锁实现](https://blog.floatshell.com/tech-series-go-stdlib-analysis/rwmutex%E8%AF%BB%E5%86%99%E4%BA%92%E6%96%A5%E9%94%81%E5%AE%9E%E7%8E%B0/): 写锁 func (rw *RWMutex) Lock() { // 竞态检测 if race.Enabled { _ = rw.w.state race.Disable() } // 使用Mutex锁 … - Published 2026-01-31 - [Socket 编程](https://blog.floatshell.com/tech-series-go-stdlib-analysis/socket-%E7%BC%96%E7%A8%8B/): Go 对 C 的套接字系统调用进行了一些封装,使 API 更少,更易用 TCP 编程 socket,bind,和 listen 被合并成 -> … - Published 2026-01-31 - [sync 包](https://blog.floatshell.com/tech-series-go-stdlib-analysis/sync-%E5%8C%85/): mutex 互斥锁 type Mutex struct { state int32 //互斥锁上锁状态枚举值如下所示 sema uint32 //信号量,向处于Gwaitting的G发送信号 } … - Published 2026-01-31 - [sync.Pool 包](https://blog.floatshell.com/tech-series-go-stdlib-analysis/sync.pool-%E5%8C%85/): 简而言之,sync.Pool 其实拿的是大段的连续内存,因为需要并发安全来存放和拿出这些内存对象,在为数不多的 P 上的竞争会比在很多个 G 上同时竞争的锁粒度要小得多,同时为了更少的竞争,pool … - Published 2026-01-31 - [Waitgroup 实现](https://blog.floatshell.com/tech-series-go-stdlib-analysis/waitgroup-%E5%AE%9E%E7%8E%B0/): 数据结构 type WaitGroup struct { noCopy noCopy // 64 位的原子操作需要有64位的内存对齐,而32位的编译器无法保证对齐,因此如果是32位的话前8字节做 … - Published 2026-01-31 ## Go 基础教程 - [CGO](https://blog.floatshell.com/tech-series-go-basics/cgo/): 需要 import “C” 二进制的桥接接口 因为内存模型的不同, Go 的内存可伸缩而 C 的内存是稳定的,Go访问一段静态内存很容易,但是 C 访问 Go 的内存可能会出现 … - Published 2026-01-31 - [channel 与 Select](https://blog.floatshell.com/tech-series-go-basics/channel-%E4%B8%8E-select/): make 操作返回 chan 引用,实际的结构体被分配在堆中 缓冲 chan 数据结构: 基于数组的循环队列 ring buffer 用来暂存数据 基于链表的单向队列sudog,用于保存阻塞在此 … - Published 2026-01-31 - [defer 实现原理](https://blog.floatshell.com/tech-series-go-basics/defer-%E5%AE%9E%E7%8E%B0%E5%8E%9F%E7%90%86/): type _defer struct { sp uintptr //函数栈指针 pc uintptr //程序计数器 fn *funcval //函数地址 link *_defer //指向自身结构 … - Published 2026-01-31 - [GC](https://blog.floatshell.com/tech-series-go-basics/gc/): 三色并发标记-清除垃圾回收器 tricolor concurrent mark-sweep 对象被标记为白灰黑三色 初始情况下,所有的对象都是白色,接着从 goroutine 栈空间和全局变量开始扫描 … - Published 2026-01-31 - [go module 包管理快速上手](https://blog.floatshell.com/tech-series-go-basics/go-module-%E5%8C%85%E7%AE%A1%E7%90%86%E5%BF%AB%E9%80%9F%E4%B8%8A%E6%89%8B/): 语义版本 module 使用的是v主要版本号.次要版本号.补丁版本号 来表示一个包的版本 如果需要进一步指定 beta 或者 alpha 版本的话可以在后面接上 - 然后用 . 来分割自定义标识内容, … - Published 2026-01-31 - [goroutine](https://blog.floatshell.com/tech-series-go-basics/goroutine/): 进程:程序的执行过程。某程序在运行时的产物 线程:在用户进程空间中的控制流 一个进程一定有一个主线程,多线程需要通过主线程创建 运行时会自动帮我们创建和销毁系统级线程 goroutine 是用户级线程 … - Published 2026-01-31 - [nil 在 Go 中的真实意义](https://blog.floatshell.com/tech-series-go-basics/nil-%E5%9C%A8-go-%E4%B8%AD%E7%9A%84%E7%9C%9F%E5%AE%9E%E6%84%8F%E4%B9%89/): 大多数类型的零值 除了数值类型的默认值是 0,布尔类型的默认值是 false,字符串类型的默认值是空串之外,其余所有的内置类型的如 指针,slice,map,channel 甚至是函数的零值和接口皆为 … - Published 2026-01-31 - [plan9 汇编入门](https://blog.floatshell.com/tech-series-go-basics/plan9-%E6%B1%87%E7%BC%96%E5%85%A5%E9%97%A8/): 查看生成的汇编 $ go tool compile -S main.go - Published 2026-01-31 - [RPC](https://blog.floatshell.com/tech-series-go-basics/rpc/): 要实现一个RPC,基本的元素大概有这几个:序列化协议、网络模型和线程模型。而go rpc里的codec基本上实现的就是序列化协议 方法只能有两个可序列化的参数,其中第二个参数是指针类型,并且返回一个 … - Published 2026-01-31 - [编码技巧](https://blog.floatshell.com/tech-series-go-basics/%E7%BC%96%E7%A0%81%E6%8A%80%E5%B7%A7/): 错误处理 错误是值,实现 Error() string 接口 不仅仅是检查错误,还要优雅处理 堆栈信息对错误不友好,难以阅读和解析,只能告诉我们哪里出错了,却不能告诉我们为什么会出错(问题的根源) 使 … - Published 2026-01-31 - [常见并发模式](https://blog.floatshell.com/tech-series-go-basics/%E5%B8%B8%E8%A7%81%E5%B9%B6%E5%8F%91%E6%A8%A1%E5%BC%8F/): 极简的请求-接收模式 // 请求函数根据参数返回接收器 func Glob(pattern string) <-chan Item { c := make(chan Item) // 1.初始化 … - Published 2026-01-31 - [错误处理](https://blog.floatshell.com/tech-series-go-basics/%E9%94%99%E8%AF%AF%E5%A4%84%E7%90%86/): error 是一个接口类型,里面只有一个方法 Error() string panic 的时候控制权会从函数中立即结束并逐级收回到运行时(如果调用路径中没有 recover) 运行时的 panic 不 … - Published 2026-01-31 - [堆和栈](https://blog.floatshell.com/tech-series-go-basics/%E5%A0%86%E5%92%8C%E6%A0%88/): | 堆栈段 | 栈空间 堆空间 高地址 -> 低地址 Go中的堆栈段是没有保护页机制的,但是会在函数调用的时候插入一段检查代码,典型的时间换空间的设计,还可以通过内联来减少检查的次数 早期的栈管 … - Published 2026-01-31 - [反射](https://blog.floatshell.com/tech-series-go-basics/%E5%8F%8D%E5%B0%84/): 清空 struct func clear(v interface{}) { p := reflect.ValueOf(v).Elem() p.Set(reflect.Zero(p.Type())) } … - Published 2026-01-31 - [浮点数比较](https://blog.floatshell.com/tech-series-go-basics/%E6%B5%AE%E7%82%B9%E6%95%B0%E6%AF%94%E8%BE%83/): import ( "fmt" "math" ) func main() { // 设置精确度为0.00001 var a Accuracy = func() … - Published 2026-01-31 - [内存分配](https://blog.floatshell.com/tech-series-go-basics/%E5%86%85%E5%AD%98%E5%88%86%E9%85%8D/): 栈空间 动态按需分配内存,编译器为每一个函数调用都插入检查分配的内存是否够用,从而节省了用来防止栈空间溢出的保护页表,如果实际运行中超过了运行时分配的内存空间,会尝试进行栈拷贝 逃逸分析 如果变量的作 … - Published 2026-01-31 - [内置数据类型的实现](https://blog.floatshell.com/tech-series-go-basics/%E5%86%85%E7%BD%AE%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E7%9A%84%E5%AE%9E%E7%8E%B0/): 内置的扩充类型皆为结构体 string 字符串 struct string { byte* str; intgo len; } 通常string常量是编译器分配到只读段的(.rodata),对应的数据 … - Published 2026-01-31 - [运行时](https://blog.floatshell.com/tech-series-go-basics/%E8%BF%90%E8%A1%8C%E6%97%B6/): M 封装了系统线程,P 作为 G 的执行上下文,每个 P 会维护自己的一个队列,避免从全局队列拿 G ,降低锁的粒度 初始化 解析命令行参数,初始化一个 m0,启动一个系统线程与m0关联 栈空间初始化 … - Published 2026-01-31 - [指针](https://blog.floatshell.com/tech-series-go-basics/%E6%8C%87%E9%92%88/): unsafe.Pointer 可以表示任何指向可寻址的值的指针,是指针值和 uintptr 的桥梁,通过它我们可以将前两者进行双向转换 不可寻址:常量,基本类型值字面量,函数和方法,算术操作结果值,大 … - Published 2026-01-31 ## Go 性能优化手册 - [FastHTTP](https://blog.floatshell.com/tech-series-go-performance/fasthttp/): 缓冲HTTP响应 利用 bufio 一次性读取多个请求,处理完之后一次写入多个响应 减少 received 系统调用次数,提高吞吐量 带来的问题:如果某一个请求的处理比较耗时,那么它会拖累其他同一批处 … - Published 2026-01-31 - [FastJSON](https://blog.floatshell.com/tech-series-go-performance/fastjson/): 内存重用 所有的序列串放入 parser 对象中进行解析的时候,返回的结果总会是 parser 中的一部分内存 缺点: 经过解析的结果不能被随处持有 偶尔的大json会产生内存碎片 快速转义字符串 使 … - Published 2026-01-31 - [标准库性能分析](https://blog.floatshell.com/tech-series-go-performance/%E6%A0%87%E5%87%86%E5%BA%93%E6%80%A7%E8%83%BD%E5%88%86%E6%9E%90/): runtime/pprof 首先进行 时间 - CPU 时间分析 import "github.com/pkg/profile" func main() { defer … - Published 2026-01-31 - [并发编程](https://blog.floatshell.com/tech-series-go-performance/%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B/): 基本原则 控制临界区的纯度 控制临界区的粒度 减少临界区代码的执行时间 避免长时间持有互斥量 优先使用原子操作而不是互斥量 uint32 是原子操作方法支持的最短数值类型,可用于节省计数器的空间 超时 … - Published 2026-01-31 - [理解 CPU Cache 对性能的影响](https://blog.floatshell.com/tech-series-go-performance/%E7%90%86%E8%A7%A3-cpu-cache-%E5%AF%B9%E6%80%A7%E8%83%BD%E7%9A%84%E5%BD%B1%E5%93%8D/): 一般来说每个 CPU 核有 L1 和 L2 缓存,L3是共享缓存 以缓存行为单位存储,通常是 64 字节为一行 这是数组访问比链表访问快的主要原因 伪共享问题 false sharing 如果两个核都 … - Published 2026-01-31 - [系统性能排查通法](https://blog.floatshell.com/tech-series-go-performance/%E7%B3%BB%E7%BB%9F%E6%80%A7%E8%83%BD%E6%8E%92%E6%9F%A5%E9%80%9A%E6%B3%95/): 针对 CPU 先使用 top 找到最占CPU资源的进程 火焰图查看最耗时的操作,并优化这个耗时的操作逻辑 频繁的系统调用 多线程减少执行时间 也有可能是访问量大导致 CPU 的负担加重,可以考虑使用 … - Published 2026-01-31 - [字符串与IO](https://blog.floatshell.com/tech-series-go-performance/%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%8Eio/): 零内存分配转换 string 和 byte func String(b []byte) string { return *(*string)(unsafe.Pointer(&b)) } … - Published 2026-01-31 ## 操作系统 - [Linux 内核设计与实现读书笔记](https://blog.floatshell.com/tech-series-os/linux-%E5%86%85%E6%A0%B8%E8%AE%BE%E8%AE%A1%E4%B8%8E%E5%AE%9E%E7%8E%B0%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0/): 单内核和微内核设计 单内核:单模块二进制文件,直接通信,一个大内核地址空间,单独的大过程 微内核:过程被划分成多个独立的过程,每个过程叫做一个服务器,有强烈请求特权服务的服务器才运行在特权模式下,其他 … - Published 2026-01-26 - [linux内核](https://blog.floatshell.com/tech-series-os/linux%E5%86%85%E6%A0%B8/): 内核通过文件的方式暴露自身的接口,其实这些文件并不存在 系统调用 为应用程序提供系统服务,只能由操作系统内核来执行,应用程序作为调用方,必须执行一个陷阱或系统调用指令将控制权转移到操作系统,操作系统通 … - Published 2026-01-26 - [进程](https://blog.floatshell.com/tech-series-os/%E8%BF%9B%E7%A8%8B/): 进程是正在运行的程序实例,包括程序计数器,寄存器和变量当前值 进程的内部 虚拟地址空间+至少一个控制线程 空间的分段: r-x是程序段,r是定义的常量,rw是定义的变量存放的段 rw其中又可以分为栈堆 … - Published 2026-01-26 - [内存管理](https://blog.floatshell.com/tech-series-os/%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86/): 地址空间 解决保护和重定向的问题 当一个进程运行时,程序的起始物理地址装载到基址寄存器,长度加载到界限寄存器 动态重定向:通过把访问内存的指令重解释,加上基址寄存器的值,检查如果地址超越界限,会产生错 … - Published 2026-01-26 - [深入理解计算机系统笔记](https://blog.floatshell.com/tech-series-os/%E6%B7%B1%E5%85%A5%E7%90%86%E8%A7%A3%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%B3%BB%E7%BB%9F%E7%AC%94%E8%AE%B0/): 编码 反码和源码对于一个数字 0 有两种编码方式 补码编码有唯一性,符号位为1表示负数,符号位为0表示非负数 减法可以转化为加法,电路设计更简单高效 相加进位最高位的进位可以被省去 编译的本质 … - Published 2026-01-26 - [文件系统](https://blog.floatshell.com/tech-series-os/%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F/): 磁盘会被划分为多个分区,每个分区有一个独立的文件系统,0号扇区用来引导计算机,紧挨着就是分区表,给出了每个分区的起止地址 分区的布局 从引导块开始,超级块包含文件系统的关键信息参数 inode 数据块 … - Published 2026-01-26 ### APUE笔记 - [Ch 3 文件IO](https://blog.floatshell.com/tech-series-os/apue/ch-3-%E6%96%87%E4%BB%B6io/): 文件操作 open 和 opent 参数:要打开或创建的文件名 这两个返回的文件描述符一定是最小的未用描述符数值,关闭的文件描述符就是未用的。 opent 的参数 fd 指出了相对路径名在文件系统中的 … - Published 2026-01-26 - [Ch 4 文件和目录](https://blog.floatshell.com/tech-series-os/apue/ch-4-%E6%96%87%E4%BB%B6%E5%92%8C%E7%9B%AE%E5%BD%95/): 获取文件信息的函数:stat, fstat, fstatat, lstat ls 命令调用最多的是 stat 文件类型: 普通文件(文本和二进制) 目录文件 块特殊文件 字符特殊文件 命名管道 … - Published 2026-01-26 - [Ch 5 标准 IO stdio.h](https://blog.floatshell.com/tech-series-os/apue/ch-5-%E6%A0%87%E5%87%86-io-stdio.h/): 缓冲区分配,以优化的块长度执行 IO,这些处理使得用户不必担心如何选择使用正确的块长度 当用标准IO库打开或创建一个文件时,我们已使一个流与一个文件相关联 流的定向:决定了流是读写单字节还是多字节字符 … - Published 2026-01-26 - [Ch 7 进程环境](https://blog.floatshell.com/tech-series-os/apue/ch-7-%E8%BF%9B%E7%A8%8B%E7%8E%AF%E5%A2%83/): 启动例程: exit ( main (argc, argv) ) 进程终止方式 正常终止: 从 main 返回 调用 exit,_exit,_Exit 最后一个线程从其启动例程返回 从最后一个线程调用 … - Published 2026-01-26 - [Ch 8 进程控制](https://blog.floatshell.com/tech-series-os/apue/ch-8-%E8%BF%9B%E7%A8%8B%E6%8E%A7%E5%88%B6/): 进程ID虽然是唯一的,但是却是可复用的。 大多数系统都会实现延迟复用算法,使被赋予新建进程的ID不同于最近终止进程所使用的ID。防止新进程被误认成之前的进程 ID=0的进程是调度进程,常被称为交换进程 … - Published 2026-01-26 - [Ch 9 进程关系](https://blog.floatshell.com/tech-series-os/apue/ch-9-%E8%BF%9B%E7%A8%8B%E5%85%B3%E7%B3%BB/): 终端登录 读取 /etc/ttys,每一个终端 fork 一个并执行 getty ,用于读取登录用户名和初始环境集,输入完成后再执行 exec login 接着读取密码,通过了再执行 shell 完成 … - Published 2026-01-26 - [Ch 10 信号](https://blog.floatshell.com/tech-series-os/apue/ch-10-%E4%BF%A1%E5%8F%B7/): 信号提供了一种异步处理事件的方法 信号名:SIG开头 产生信号的条件: 终端键 硬件异常:除0,无效的内存引用(SIGSEGV) kill 函数可将任意信号发送给另一个进程或进程组 检测到某些软件条件 … - Published 2026-01-26 - [Ch 11 线程](https://blog.floatshell.com/tech-series-os/apue/ch-11-%E7%BA%BF%E7%A8%8B/): 有利于提高程序的吞吐量,便于共享资源,交互程序可以使用多线程来改善响应时间 线程包含有:标识线程的线程ID,一组寄存器值,栈,调度优先级和策略,信号屏蔽字,errno变量以及线程私有数据。 进程的所有 … - Published 2026-01-26 - [Ch 12 线程控制](https://blog.floatshell.com/tech-series-os/apue/ch-12-%E7%BA%BF%E7%A8%8B%E6%8E%A7%E5%88%B6/): 线程限制 可以通过 sysconf 函数进行查询,其中含有 以 linux 系统为例 线程退出时操作系统实现销毁线程特定数据的最大次数:4 进程可以创建的线程KEY的最大个数:1024 一个线程栈的最 … - Published 2026-01-26 - [Ch 13 守护进程](https://blog.floatshell.com/tech-series-os/apue/ch-13-%E5%AE%88%E6%8A%A4%E8%BF%9B%E7%A8%8B/): 守护进程是生存期长的一种进程,常常在系统引导装入时启动,仅在系统关闭时才终止。 后台运行,无控制终端 需要解决出错的时候报告信息的问题 常见守护进程 kswapd 内存换页守护进程 支持将虚拟内存脏页 … - Published 2026-01-26 - [Ch 14 高级IO](https://blog.floatshell.com/tech-series-os/apue/ch-14-%E9%AB%98%E7%BA%A7io/): 记录锁 用于确保一个进程在单独写一个文件。当第一个进程在读或者修改文件的某部分时,使用记录锁可以阻止其他进程修改同一文件区。有点类似于数据库中的范围间隙锁 POSIX.1 的 fcntl 锁结构 包含 … - Published 2026-01-26 - [Ch 15 进程间通信](https://blog.floatshell.com/tech-series-os/apue/ch-15-%E8%BF%9B%E7%A8%8B%E9%97%B4%E9%80%9A%E4%BF%A1/): 管道 单向,父子之间 一个管道由一个进程创建,在进程调用 fork 之后,这个管道就能在父子间使用,通过 pipe 函数创建,返回两个描述符,一个为读打开,一个为写打开 fork 之后我们指定哪些描述 … - Published 2026-01-26 - [Ch 17 高级进程间通信](https://blog.floatshell.com/tech-series-os/apue/ch-17-%E9%AB%98%E7%BA%A7%E8%BF%9B%E7%A8%8B%E9%97%B4%E9%80%9A%E4%BF%A1/): unix 域套接字 专门用于同一台电脑中的进程间的通信,相比于因特网套接字效率更高,仅仅只传输数据,不执行协议的处理 创建好的 UNIX域套接字可以起到全双工管道的作用,两端对读写开放,称为 fd 管 … - Published 2026-01-26 ## 计算机网络 - [cookie 和 session](https://blog.floatshell.com/tech-series-network/cookie-%E5%92%8C-session/): cookie cookie 的产生是为了解决 http 无状态无法识别用户问题,第一次访问不携带 cookie ,服务器通过 set-cookie 字段让客户端浏览器拿到 cookie 信息,从而在下 … - Published 2026-01-29 - [DNS 协议](https://blog.floatshell.com/tech-series-network/dns-%E5%8D%8F%E8%AE%AE/): 浏览器输入域名,首先查找,本地 hosts 文件 文件没有 -> 本地DNS解析 -> 参数配置中首选的 DNS 服务器区域解析或映射缓存 -> 再发给根解析,从顶向下递归解析 从客 … - Published 2026-01-29 - [HTTP 协议](https://blog.floatshell.com/tech-series-network/http-%E5%8D%8F%E8%AE%AE/): http 1.0 TCP不能复用,一般服务器在响应完请求后主动关闭连接 http 1.1 默认启用 keep-alive 重用连接,避免了建立/释放连接的开销 http 请求是一个接着一个的,不需要重 … - Published 2026-01-29 - [IO 复用](https://blog.floatshell.com/tech-series-network/io-%E5%A4%8D%E7%94%A8/): 单个线程通过记录跟踪每一个Sock(I/O流)的状态,来同时管理多个I/O流 用户输入的阻塞,导致如果服务端终止,那么它将看不到套接字的EOF,直到套接字读时为止,因此进程需要有预先告知内核的能力,使 … - Published 2026-01-29 - [SSL 协议](https://blog.floatshell.com/tech-series-network/ssl-%E5%8D%8F%E8%AE%AE/): 应用层在使用套接字之前,多了一个SSL子层,SSL套接字 并不限于 HTTP协议使用 步骤 协商加密算法 服务器鉴别:服务端下发证书,客户端用CA RSA公钥进行解密验证 会话密钥计算:随机秘密数作为 … - Published 2026-01-29 - [TCP 协议](https://blog.floatshell.com/tech-series-network/tcp-%E5%8D%8F%E8%AE%AE/): 五层协议:应用,传输,网络,数据链路,物理 OSI七层:应用层分为 应用,表示,会话 可靠传输 TCP 通过校验和,序列号,确认应答,重发控制,连接管理以及窗口控制等机制实现可靠传输 通过 ACK 的 … - Published 2026-01-29 - [UDP 协议](https://blog.floatshell.com/tech-series-network/udp-%E5%8D%8F%E8%AE%AE/): UDP不提供复杂的控制机制,利用IP提供面向无连接的通信服务。并且它是将应用程序发来的数据在收到的那一刻,立刻按照原样发送到网络上的一种机制。 即使是出现网络拥堵的情况下,UDP也无法进行流量控制等避 … - Published 2026-01-29 - [单点登录 SSO](https://blog.floatshell.com/tech-series-network/%E5%8D%95%E7%82%B9%E7%99%BB%E5%BD%95-sso/): 用户访问app系统,app系统是需要登录的,但用户现在没有登录。 跳转到CAS server,即SSO登录系统,以后图中的CAS Server我们统一叫做SSO系统。 SSO系统也没有登录,弹出用户 … - Published 2026-01-29 - [高并发](https://blog.floatshell.com/tech-series-network/%E9%AB%98%E5%B9%B6%E5%8F%91/): 系统保护措施:缓存,降级和限流 缓存 提高系统吞吐量 缓存穿透 是一种攻击手段,查询数据库里根本不存在的数据,每次都不命中,导致每次都会触发无效的数据库读请求 解决方案: 布隆过滤器发现没有就快速返回 … - Published 2026-01-29 - [套接字编程](https://blog.floatshell.com/tech-series-network/%E5%A5%97%E6%8E%A5%E5%AD%97%E7%BC%96%E7%A8%8B/): 套接字的本质就是一个有相应描述符的打开文件 地址存放在16个字节的结构体中,2字节协议族,2字节端口,4字节IP地址,8字节空对齐 通用套接字结构:2字节协议族,14字节的地址数据 最后设计 … - Published 2026-01-29 - [协议头部](https://blog.floatshell.com/tech-series-network/%E5%8D%8F%E8%AE%AE%E5%A4%B4%E9%83%A8/): MAC帧头定义:目的mac,源mac,上层协议类型 帧尾:数据帧尾校验和 IP头结构:IP协议版本,协议号,源IP,目标IP,TTL生存时间,16位首部校验和,16位标识,3位标志和13位片偏移,16 … - Published 2026-01-29 ## 数据库 - [LSM Tree](https://blog.floatshell.com/tech-series-database/lsm-tree/): mutable 和 immutable 数据结构 不可变的数据结构有一个巨大的优势:可压缩,更新的时候不需要预留额外空间(不在原地更新,原地更新更大的数据会引发重分配,需要进行碎片整理) 使用顺序写 … - Published 2026-01-27 - [MySQL](https://blog.floatshell.com/tech-series-database/mysql/): 基本架构 类似一个后端的服务器,数据库会事先开好线程并缓存这些线程,一条语句过来,数据库就会开启一个线程去处理,先去查询缓存中找,解析器中也会有相应的缓存,解析优化完后就通过 API 请求存储引擎 并 … - Published 2026-01-27 - [Mysql 技术内幕读书笔记](https://blog.floatshell.com/tech-series-database/mysql-%E6%8A%80%E6%9C%AF%E5%86%85%E5%B9%95%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0/): 数据库:文件系统上的文件集合 实例:由后台线程和共享内存区组成的进程 架构:单进程多线程 配置读取:以最后一个配置文件为准 特点:插件式的表插件引擎 各种存储引擎 InnoDB 行锁设计,支持外键,默 … - Published 2026-01-27 - [Redis](https://blog.floatshell.com/tech-series-database/redis/): 数据结构 string 使用 SDS 记录了 len, free, buf三个字段 特性:空间预分配,惰性释放 优点:常数时间获得长度,防止缓冲区溢出 dict 使用散列表 Rehash 动态均摊扩容 … - Published 2026-01-27 - [并发控制与锁](https://blog.floatshell.com/tech-series-database/%E5%B9%B6%E5%8F%91%E6%8E%A7%E5%88%B6%E4%B8%8E%E9%94%81/): 数据库中的所有事务都是串行执行的,那么它非常容易成为整个应用的性能瓶颈,虽然说没法水平扩展的节点在最后都会成为瓶颈,但是串行执行事务的数据库会加速这一过程;而并发(Concurrency)使一切事情的 … - Published 2026-01-27 - [大数据专题](https://blog.floatshell.com/tech-series-database/%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B8%93%E9%A2%98/): 数据规模估计 搜索关键词字符串的平均长度 50 字节 1亿个字符串大约需要 5G 的内存空间 1亿个 int 大约需要 400 M 1亿个 1 字节数据为 1 G TopK 问题 寻找n个数据中前K大 … - Published 2026-01-27 - [深入浅出 InfluxDB — 从索引到数据块](https://blog.floatshell.com/tech-series-database/%E6%B7%B1%E5%85%A5%E6%B5%85%E5%87%BA-influxdb--%E4%BB%8E%E7%B4%A2%E5%BC%95%E5%88%B0%E6%95%B0%E6%8D%AE%E5%9D%97/): 前言 上一篇文章主要介绍了存储引擎的主要组件以及它们之间的关系,本文在读者理解了存储引擎的大致工作原理的基础上,继续从基本概念开始着手介绍 influxdb 是如何对硬盘中的数据进行索引的,以及查询语 … - Published 2026-01-27 - [深入浅出 InfluxDB — 存储引擎](https://blog.floatshell.com/tech-series-database/%E6%B7%B1%E5%85%A5%E6%B5%85%E5%87%BA-influxdb--%E5%AD%98%E5%82%A8%E5%BC%95%E6%93%8E/): 前言 上一篇文章主要介绍了数据点是如何从客户端写入 InfluxDB 的,初次从源码层面接触到了 InfluxDB 存储引擎的几个主要结构,详细介绍了数据从 database 直到 WAL 的落盘过程 … - Published 2026-01-27 - [深入浅出 InfluxDB — 写操作](https://blog.floatshell.com/tech-series-database/%E6%B7%B1%E5%85%A5%E6%B5%85%E5%87%BA-influxdb--%E5%86%99%E6%93%8D%E4%BD%9C/): 前言 本文为深入浅出 InfluxDB 系列文章的第二篇,已理解 InfluxDB 基本术语的读者可以放心食用,源码讲解基于开源版 InfluxDB v1.8 交互模型分析 典型的 CS 模式 … - Published 2026-01-27 ## Root Pages - [关于](https://blog.floatshell.com/about/): 我是托克,云厂程序员,主要关注 AI 软件工程实践、云原生基础设施、后端系统、长期投资和个体认知升级。 这个站点叫「游壳说」,英文名 FloatShell。我把它当作公开笔记本:一部分文章记录工程问题 … - Published 2026-05-24 - [归档](https://blog.floatshell.com/archives/): - Published 2026-05-23 --- Generated on 2026-05-27 00:33:46 UTC Site: https://blog.floatshell.com/