<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>alpha 的博客</title>
    <description>滑稽点,何晓东博客,php,mysql,linux,go</description>
    <link>http://alpha2016.github.io/</link>
    <atom:link href="http://alpha2016.github.io/feed.xml" rel="self" type="application/rss+xml" />
    <pubDate>Thu, 16 Apr 2026 03:08:47 +0000</pubDate>
    <lastBuildDate>Thu, 16 Apr 2026 03:08:47 +0000</lastBuildDate>
    <generator>Jekyll v3.10.0</generator>
    
      <item>
        <title>模型变强并不会减少工程；它把工程从『措辞』推向『世界』</title>
        <description>&lt;h1 id=&quot;从-prompt-engineering-到-harness-engineering我们不是在发明新学科而是在给旧透镜配新对象&quot;&gt;从 Prompt Engineering 到 Harness Engineering：我们不是在「发明新学科」，而是在给旧透镜配新对象&lt;/h1&gt;

&lt;h2 id=&quot;摘要&quot;&gt;摘要&lt;/h2&gt;

&lt;p&gt;「AI 只用了三年就从 Prompt Engineering 走到 Harness Engineering」这句话之所以抓人，是因为它暗示了一种技术路线的成熟：从&lt;strong&gt;会说话&lt;/strong&gt;，到&lt;strong&gt;会系统工程&lt;/strong&gt;。更精细的说法是：这些工程实践并不会 magically 变成某一门「古老学科」的翻版；更准确的是——&lt;strong&gt;我们被迫重新调用人类已经积累的语言学、信息论、控制论、组织理论、认知科学与逻辑传统&lt;/strong&gt;，去描述和约束一类&lt;strong&gt;新的人工行为体（agent）系统&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;本文给出一套更可操作的定义、边界与映射表，帮助你把隐喻写成可讨论、可教学、可落地的框架。&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;1-先把词说清楚六个概念的操作性定义&quot;&gt;1. 先把词说清楚：六个概念的操作性定义&lt;/h2&gt;

&lt;p&gt;下面定义偏&lt;strong&gt;工程实践&lt;/strong&gt;，而不是学术教科书定义；目的是让团队对齐「我们在讨论什么」。&lt;/p&gt;

&lt;h3 id=&quot;11-prompt-engineering提示工程&quot;&gt;1.1 Prompt Engineering（提示工程）&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;定义&lt;/strong&gt;：通过自然语言（或结构化指令）影响模型输出分布的一组方法，包括模板、约束、角色、示例、输出格式、反例、分解任务等。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;核心问题&lt;/strong&gt;：在模型能力与任务结构给定的情况下，如何把「意图」编码成可执行的文本接口。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;常见误区&lt;/strong&gt;：把 Prompt 当成「咒语」；忽视数据与评测；把一次性 demo 当成生产方案。&lt;/p&gt;

&lt;h3 id=&quot;12-context-engineering上下文工程&quot;&gt;1.2 Context Engineering（上下文工程）&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;定义&lt;/strong&gt;：为模型构造可计算、可检索、可裁剪、可验证的输入边界，包括：系统提示、工具返回、RAG 片段、记忆策略、窗口预算、结构化字段、引用与溯源等。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;核心问题&lt;/strong&gt;：在给定上下文预算与延迟约束下，如何最大化&lt;strong&gt;任务相关信息&lt;/strong&gt;并最小化&lt;strong&gt;噪声与幻觉燃料&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;常见误区&lt;/strong&gt;：堆上下文等于更聪明；把「更多 token」当成「更多信息」。&lt;/p&gt;

&lt;h3 id=&quot;13-reasoning-engineering推理过程工程&quot;&gt;1.3 Reasoning Engineering（推理/过程工程）&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;定义&lt;/strong&gt;：把复杂任务拆成可检查的中间态，并用显式过程（链式思考、树搜索、验证器、形式化检查、对偶模型互审等）提高可靠性与可解释性。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;核心问题&lt;/strong&gt;：如何把「一次性生成」变成「可审计的推理轨迹」。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;常见误区&lt;/strong&gt;：把长思维链当可靠；忽视错误传播与验证成本。&lt;/p&gt;

&lt;h3 id=&quot;14-skill-engineering技能工程&quot;&gt;1.4 Skill Engineering（技能工程）&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;定义&lt;/strong&gt;：把可重复、可版本化、可组合的能力封装为「可被模型稳定调用的规程」：检查单、工作流片段、工具协议、领域规则、失败恢复策略、质量门槛等。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;核心问题&lt;/strong&gt;：如何把隐性经验变成&lt;strong&gt;可分发、可回归测试&lt;/strong&gt;的能力包。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;常见误区&lt;/strong&gt;：把 Skill 当成「分类标签」；忽视运行态约束与失败模式。&lt;/p&gt;

&lt;h3 id=&quot;15-multi-agent--orchestration多智能体与编排&quot;&gt;1.5 Multi-Agent / Orchestration（多智能体与编排）&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;定义&lt;/strong&gt;：多个模型实例、工具与人在权限边界内协作完成目标：角色分工、消息路由、状态共享、冲突解决、并行与回滚。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;核心问题&lt;/strong&gt;：如何把「智能」变成「可协调的系统」。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;常见误区&lt;/strong&gt;：Agent 越多越好；忽视通信复杂度与责任链。&lt;/p&gt;

&lt;h3 id=&quot;16-harness-engineering约束挽具工程&quot;&gt;1.6 Harness Engineering（约束/挽具工程）&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;定义&lt;/strong&gt;：把模型与工具放进可运行的「挽具」里：沙箱、策略、观测、护栏、成本与配额、数据边界、审计日志、人在回路、回滚与发布。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;核心问题&lt;/strong&gt;：如何在开放环境里让系统&lt;strong&gt;可控、可停、可追责、可演进&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;常见误区&lt;/strong&gt;：把 Harness 当成「写更多 if」；忽视观测性与实验设计。&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;一句话串起来&lt;/strong&gt;：Prompt/Context 解决「输入」；Reasoning 解决「过程」；Skill/Tool 解决「能力」；Agent/Orchestration 解决「协作」；Harness 解决「系统」。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;2-映射到古老学科哪里对哪里需要加限定&quot;&gt;2. 「映射到古老学科」哪里对、哪里需要加限定&lt;/h2&gt;

&lt;p&gt;你提出的对应关系很有传播力；更精细的版本应当加两类限定：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;学科不是互斥的&lt;/strong&gt;：现实问题几乎都是交叉学科。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;工程对象是新的人工行为体系统&lt;/strong&gt;：旧学科提供语言与工具，但不能替代新现象里长出来的约束（合规、责任、供应链、组织政治等）。&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;下面给出「更细」的映射：每一行都写 &lt;strong&gt;强相关 / 弱相关 / 容易误读&lt;/strong&gt;。&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;工程概念&lt;/th&gt;
      &lt;th&gt;强相关传统领域&lt;/th&gt;
      &lt;th&gt;还应叠加的领域（常被忽略）&lt;/th&gt;
      &lt;th&gt;典型误读&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Prompt&lt;/td&gt;
      &lt;td&gt;语言学（语用、语义、话语分析）、修辞学&lt;/td&gt;
      &lt;td&gt;教育学（支架式教学）、技术写作&lt;/td&gt;
      &lt;td&gt;只谈「措辞」不谈任务结构与评测&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Context&lt;/td&gt;
      &lt;td&gt;信息科学、信息论、检索&lt;/td&gt;
      &lt;td&gt;图书馆学/情报学、HCI、隐私与安全&lt;/td&gt;
      &lt;td&gt;把上下文当无限黑板&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Reasoning&lt;/td&gt;
      &lt;td&gt;认知心理学（人类偏差）、逻辑学&lt;/td&gt;
      &lt;td&gt;计算复杂性、概率推理、形式化方法&lt;/td&gt;
      &lt;td&gt;把「像人思考」当「更正确」&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Skill&lt;/td&gt;
      &lt;td&gt;分类学（信息架构）&lt;/td&gt;
      &lt;td&gt;教育学（规程/训练）、软件工程（接口与版本）&lt;/td&gt;
      &lt;td&gt;Skill = 标签&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Multi-Agent&lt;/td&gt;
      &lt;td&gt;管理学、组织理论&lt;/td&gt;
      &lt;td&gt;分布式系统、博弈论、社会学（权力与激励）&lt;/td&gt;
      &lt;td&gt;Agent = 团队人数&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Harness&lt;/td&gt;
      &lt;td&gt;控制论（反馈与稳态）&lt;/td&gt;
      &lt;td&gt;系统工程、可靠性工程、风险治理、法学合规&lt;/td&gt;
      &lt;td&gt;Harness = 写规则 if&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;strong&gt;更稳的总命题&lt;/strong&gt;（可作为文章中心句）：&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;不是「AI 工程最终会变成某一门旧学科」，而是「AI 工程会迫使我们把旧学科里那些本来就很硬核的东西重新严肃化：语言、信息、过程、组织与系统约束」。新的是对象与风险外溢速度；旧的是人类用来驯服复杂性的知识传统。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;3-为什么三年叙事成立它描述的是责任边界的外移&quot;&gt;3. 为什么「三年」叙事成立：它描述的是责任边界的外移&lt;/h2&gt;

&lt;p&gt;从 Prompt 到 Harness，本质上不是模型突然更会了，而是&lt;strong&gt;责任从个人技巧外移到系统&lt;/strong&gt;：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Prompt 时代&lt;/strong&gt;：成败更像「个人与模型的对话艺术」。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Harness 时代&lt;/strong&gt;：成败更像「组织能否定义可运行系统」：观测、回滚、审计、权限、成本、SLA。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;所以三年里变化最快的往往不是「模型智商曲线」，而是&lt;strong&gt;把 AI 放进生产环境后，世界向你索要的那套工程证据&lt;/strong&gt;。&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;4-一套可用于团队对齐的分层模型比学科映射更利于落地&quot;&gt;4. 一套可用于团队对齐的「分层模型」（比学科映射更利于落地）&lt;/h2&gt;

&lt;p&gt;如果你要把文章写得「可执行」，建议加一张分层图（文字版如下）：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;接口层&lt;/strong&gt;：Prompt / UI / API（人类意图如何进入系统）&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;信息层&lt;/strong&gt;：Context / RAG / Memory（系统读什么、信什么、忘什么）&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;过程层&lt;/strong&gt;：Reasoning / Planning / Verification（系统如何走到答案）&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;能力层&lt;/strong&gt;：Tools / Skills / Policies（系统能做什么、不能做什么）&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;协作层&lt;/strong&gt;：Agents / Humans / Roles（谁负责什么、如何交接）&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;系统层&lt;/strong&gt;：Harness（观测、护栏、发布、成本、合规、追责）&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;强调&lt;/strong&gt;：学科映射适合启发；分层模型适合排期。两者并不冲突。&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;5-给读者的三条结论&quot;&gt;5. 给读者的三条结论&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;隐喻的价值&lt;/strong&gt;：把 Prompt 想成语言学、把 Harness 想成控制论，能帮助你快速借用成熟概念来沟通。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;隐喻的边界&lt;/strong&gt;：它们不是定义；一旦写成「等于」，就会漏掉工程里真正昂贵的部分：评测、数据、权限、组织与法律责任。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;更精细的一句话&lt;/strong&gt;：AI 工程正在把「会说话」扩展为「会运行」——于是我们必须同时成为&lt;strong&gt;技术写作者、信息架构师、过程设计师、系统工程师与治理者&lt;/strong&gt;。&lt;/li&gt;
&lt;/ol&gt;

&lt;hr /&gt;

</description>
        <pubDate>Wed, 16 Apr 2025 00:00:00 +0000</pubDate>
        <link>http://alpha2016.github.io/2025/04/16/%E6%A8%A1%E5%9E%8B%E5%8F%98%E5%BC%BA%E5%B9%B6%E4%B8%8D%E4%BC%9A%E5%87%8F%E5%B0%91%E5%B7%A5%E7%A8%8B-%E5%AE%83%E6%8A%8A%E5%B7%A5%E7%A8%8B%E4%BB%8E-%E6%8E%AA%E8%BE%9E-%E6%8E%A8%E5%90%91-%E4%B8%96%E7%95%8C/</link>
        <guid isPermaLink="true">http://alpha2016.github.io/2025/04/16/%E6%A8%A1%E5%9E%8B%E5%8F%98%E5%BC%BA%E5%B9%B6%E4%B8%8D%E4%BC%9A%E5%87%8F%E5%B0%91%E5%B7%A5%E7%A8%8B-%E5%AE%83%E6%8A%8A%E5%B7%A5%E7%A8%8B%E4%BB%8E-%E6%8E%AA%E8%BE%9E-%E6%8E%A8%E5%90%91-%E4%B8%96%E7%95%8C/</guid>
        
        
      </item>
    
      <item>
        <title>go 协程，数据竞争及对应解决方法</title>
        <description>&lt;h5 id=&quot;场景&quot;&gt;场景&lt;/h5&gt;
&lt;p&gt;有个查询结果集的操作，无可避免的需要在循环获取数据，然后将结果集放到 map 中，这个操作在压测的时候，没出现问题，发布到生产环境之后，开始偶现 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fatal error: concurrent map read and map write&lt;/code&gt; 错误，导致容器重启了。&lt;/p&gt;

&lt;h5 id=&quot;原因&quot;&gt;原因&lt;/h5&gt;
&lt;p&gt;多个协程同时对 map 进行读写操作，导致数据竞争
测试环境压测未复现是因为单个 pod 常规时间只有一个 CPU，资源不够用了才会使用两个 CPU，单核的情况下，协程是串行执行的，所以没有出现数据竞争的问题。
同时也没开着数据竞争检测，也没有检测出来这个问题&lt;/p&gt;

&lt;h5 id=&quot;调试&quot;&gt;调试&lt;/h5&gt;
&lt;p&gt;在本机多核CPU情况下，执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;go run --race main.go&lt;/code&gt; 启动项目，调用方法，会有提示 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;data race&lt;/code&gt;，再开始对应解决问题。
出现数据竞争告警时，就是需要解决问题，无则是其他情况&lt;/p&gt;
&lt;div class=&quot;language-json highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;err&quot;&gt;==================&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;WARNING:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;DATA&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;RACE&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;Write&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;at&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;00&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;00008e000&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;by&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;goroutine&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
 &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;main.main.func&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;Previous&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;at&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;00&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;00008e000&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;by&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;goroutine&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
 &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;main.main.func&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;==================&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h5 id=&quot;解决方案&quot;&gt;解决方案&lt;/h5&gt;
&lt;p&gt;&lt;strong&gt;① 使用 sync.Mutex/sync.RWMutex 加锁&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;互斥锁：
Mutex是互斥锁的意思，也叫排他锁，同一时刻一段代码只能被一个线程运行，使用只需要关注方法Lock（加锁）和Unlock（解锁）即可。
在Lock()和Unlock()之间的代码段称为资源的临界区(critical section)，是线程安全的，任何一个时间点都只能有一个goroutine执行这段区间的代码。
Mutex在大量并发的情况下，会造成锁等待，对性能的影响比较大。&lt;/p&gt;

&lt;p&gt;读写锁：
读写锁的读锁可以重入，在已经有读锁的情况下，可以任意加读锁。
在读锁没有全部解锁的情况下，写操作会阻塞直到所有读锁解锁。
写锁定的情况下，其他协程的读写都会被阻塞，直到写锁解锁。&lt;/p&gt;

&lt;p&gt;根据业务场景，按需进行加锁，尽量减少锁的粒度，提高性能。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;② 使用 sync.Map&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;go 原生的 map 不是线程安全的，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sync.Map&lt;/code&gt; 是线程安全的，读取，插入，删除也都保持着常数级的时间复杂度。
并且它通过空间换时间的方式，使用 read 和 dirty 两个 map 来进行读写分离，降低锁时间来提高效率。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sync.Map&lt;/code&gt; 适用于读多写少的场景，如果并发写多的场景，还是需要加锁的对于写多的场景，会导致 read map 缓存失效，
需要加锁，导致冲突变多；而且由于未命中 read map 次数过多，导致 dirty map 提升为 read map，这是一个 O(N) 的操作，会进一步降低性能。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;③ 使用 channel 通道传递数据&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;channel 是 goroutine 之间的通信方式，可以用来传递数据，也可以用来传递信号，比如结束信号，超时信号等。
go 的设计原则也是：通过通信来共享内存，而不是通过共享内存来通信。channel 也是线程安全的，可以用来解决数据竞争的问题。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;额外原则：&lt;/strong&gt; 如果有数据传递后，继续有进行处理，可以使用 channel，如果仅是赋值，无其他操作，直接加锁或者 sync.Map 简单易理解&lt;/p&gt;

&lt;h5 id=&quot;额外笔记&quot;&gt;额外笔记&lt;/h5&gt;
&lt;p&gt;数据竞争 (data race) 的发生条件是：当多个协程同时访问一个相同内存地址，并且至少有一个在进行写操作时，数据竞争意味着不确定的行为。
而不存在数据竞争不代表结果就是确定的。实际上，一个应用程序即使不存在数据竞争，但它的行为肯依赖于不可控的发生时间或执行顺序，这就是竞争条件 (race condition)&lt;/p&gt;

&lt;p&gt;参考链接：&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;a href=&quot;https://zhuanlan.zhihu.com/p/344834329&quot;&gt;深度解密go之 sync.Map&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://juejin.cn/post/7168271778484060167&quot;&gt;【Go基础篇】 彻底搞懂 RWMutex 实现原理&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://blog.csdn.net/windows_2015/article/details/108695948&quot;&gt;Go语言并发–传统锁与channel的选择&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;
    &lt;table&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;[Go 并发&lt;/td&gt;
          &lt;td&gt;数据竞争及竞争条件](https://blog.csdn.net/weixin_41335923/article/details/124266638)&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/li&gt;
&lt;/ol&gt;
</description>
        <pubDate>Thu, 04 May 2023 00:00:00 +0000</pubDate>
        <link>http://alpha2016.github.io/2023/05/04/go-%E5%8D%8F%E7%A8%8B-%E6%95%B0%E6%8D%AE%E7%AB%9E%E4%BA%89%E5%8F%8A%E5%AF%B9%E5%BA%94%E8%A7%A3%E5%86%B3%E6%96%B9%E6%B3%95/</link>
        <guid isPermaLink="true">http://alpha2016.github.io/2023/05/04/go-%E5%8D%8F%E7%A8%8B-%E6%95%B0%E6%8D%AE%E7%AB%9E%E4%BA%89%E5%8F%8A%E5%AF%B9%E5%BA%94%E8%A7%A3%E5%86%B3%E6%96%B9%E6%B3%95/</guid>
        
        <category>go</category>
        
        <category>map</category>
        
        <category>协程</category>
        
        <category>goroutine</category>
        
        <category>数据竞争</category>
        
        <category>读写锁 sync.RWMutex</category>
        
        <category>sync.Map</category>
        
        
      </item>
    
      <item>
        <title>Mysql distinct 和 order by 排序混淆的替代方案</title>
        <description>&lt;p&gt;场景是：从一堆学习记录中，去重并获取最近学习的几条课程ID，随手就能想到这样的一条SQL语句：&lt;/p&gt;
&lt;div class=&quot;language-sql highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;select&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;distinct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;table&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;order&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;by&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;updated_at&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;desc&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;limit&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;如果列为 a 的数据有很多条，就会发现最终取到的那条数据可能不是 updated_at 最近的那条数据，因为 distinct 有一次默认的排序，然后生成一个临时表，
然后 order by 无法从最开始的原始数据中进行排序，仅排序中间表，无法得出正确结果。改成 distinct a, updated_at 的话，
实际上又失去了 distinct 的意义了。&lt;/p&gt;

&lt;p&gt;方案一：
使用子查询方式，将结果先排序，当做一个表，然后去重保留最新的一条数据&lt;/p&gt;
&lt;div class=&quot;language-sql highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;select&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;distinct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;select&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;table&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;order&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;by&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;updated_at&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;desc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;limit&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;方案二：
借助 max 和 group by 特性直接取最大值，取值&lt;/p&gt;
&lt;div class=&quot;language-sql highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;select&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;updated_at&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;table&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;group&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;by&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;order&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;by&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;updated_at&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;desc&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;limit&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</description>
        <pubDate>Fri, 30 Dec 2022 00:00:00 +0000</pubDate>
        <link>http://alpha2016.github.io/2022/12/30/MySQL-distinct-%E5%92%8C-order-by-%E6%8E%92%E5%BA%8F%E6%B7%B7%E6%B7%86%E7%9A%84%E6%9B%BF%E4%BB%A3%E6%96%B9%E6%A1%88/</link>
        <guid isPermaLink="true">http://alpha2016.github.io/2022/12/30/MySQL-distinct-%E5%92%8C-order-by-%E6%8E%92%E5%BA%8F%E6%B7%B7%E6%B7%86%E7%9A%84%E6%9B%BF%E4%BB%A3%E6%96%B9%E6%A1%88/</guid>
        
        
      </item>
    
      <item>
        <title>Centos 系统简单排查流量异常问题</title>
        <description>&lt;p&gt;主要起因是服务器出现了流量异常，感觉也没操作什么，突然的流量异常，一点不科学，就简单排查了一下，记录个笔记&lt;/p&gt;

&lt;h5 id=&quot;第一步找到哪个端口产生的异常流量&quot;&gt;第一步：找到哪个端口产生的异常流量&lt;/h5&gt;
&lt;p&gt;需要提前安装 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;iftop&lt;/code&gt; 这个工具，用来分析网络实时流量。Mac 系统下可能需要 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;iftop -i en0&lt;/code&gt; 指定网卡，然后进入界面后 p 键打开展示端口。更多精确参数和使用，参考 &lt;a href=&quot;https://einverne.github.io/post/2016/06/iftop-to-monitor-network-traffic.html&quot;&gt;iftop 流量监控&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;centos 下直接用命令就行&lt;/p&gt;
&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;iftop &lt;span class=&quot;nt&quot;&gt;-PB&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;https://alpha2016.github.io/img/2021-09-10-iftop.png&quot; alt=&quot;iftop 效果图&quot; /&gt;&lt;/p&gt;

&lt;p&gt;然后能发现流量占用大的端口。例如发现 54626 端口不对劲&lt;/p&gt;

&lt;h5 id=&quot;第二步分析这个端口被哪个进程占用&quot;&gt;第二步：分析这个端口被哪个进程占用&lt;/h5&gt;
&lt;p&gt;直接使用命令分析端口&lt;/p&gt;
&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;lsof &lt;span class=&quot;nt&quot;&gt;-i&lt;/span&gt; 54626
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;分析发现进程ID 是 622&lt;/p&gt;

&lt;h5 id=&quot;第三步分析进程&quot;&gt;第三步：分析进程&lt;/h5&gt;
&lt;p&gt;直接使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ps aux&lt;/code&gt; 命令来分析，这个命令能看到很多信息：&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;USER：该 process 属于哪个用户&lt;/li&gt;
  &lt;li&gt;PID ：该 process 的进程识别码。&lt;/li&gt;
  &lt;li&gt;%CPU：该 process 使用掉的 CPU 资源百分比；&lt;/li&gt;
  &lt;li&gt;%MEM：该 process 所占用的实体内存百分比；&lt;/li&gt;
  &lt;li&gt;VSZ ：该 process 使用掉的虚拟内存量 (Kbytes)&lt;/li&gt;
  &lt;li&gt;RSS ：该 process 占用的固定的内存量 (Kbytes)&lt;/li&gt;
  &lt;li&gt;TTY ：该 process 是在那個终端上面运作&lt;/li&gt;
  &lt;li&gt;STAT：该进程目前的状态 (D/R/S/T/Z)
    &lt;ul&gt;
      &lt;li&gt;D 不可中断 uninterruptible sleep (usually IO)&lt;/li&gt;
      &lt;li&gt;R 运行 runnable (on run queue)&lt;/li&gt;
      &lt;li&gt;S 中断 sleeping&lt;/li&gt;
      &lt;li&gt;T 停止 traced or stopped&lt;/li&gt;
      &lt;li&gt;Z 僵死 a defunct (”zombie”) process&lt;/li&gt;
      &lt;li&gt;其它状态还包括W(无驻留页), &amp;lt;(高优先级进程), N(低优先级进程), L(内存锁页).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;START：该 process 被触发启动的时间；&lt;/li&gt;
  &lt;li&gt;TIME ：该 process 实际使用 CPU 运行的时间。&lt;/li&gt;
  &lt;li&gt;COMMAND：该程序的实际指令&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ps aux | &lt;span class=&quot;nb&quot;&gt;grep &lt;/span&gt;622
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;根据结果来分析什么程序引起的流量异常，之后是 kill 进程或者卸载对应软件，根据需求来就搞定了。&lt;/p&gt;

&lt;p&gt;最后恰饭 &lt;a href=&quot;https://www.aliyun.com/minisite/goods?userCode=0amqgcs9&quot;&gt;阿里云全系列产品/短信包特惠购买 中小企业上云最佳选择 阿里云内部优惠券&lt;/a&gt;&lt;/p&gt;
</description>
        <pubDate>Fri, 10 Sep 2021 00:00:00 +0000</pubDate>
        <link>http://alpha2016.github.io/2021/09/10/centos-%E7%B3%BB%E7%BB%9F%E7%AE%80%E5%8D%95%E6%8E%92%E6%9F%A5%E6%B5%81%E9%87%8F%E5%BC%82%E5%B8%B8%E9%97%AE%E9%A2%98/</link>
        <guid isPermaLink="true">http://alpha2016.github.io/2021/09/10/centos-%E7%B3%BB%E7%BB%9F%E7%AE%80%E5%8D%95%E6%8E%92%E6%9F%A5%E6%B5%81%E9%87%8F%E5%BC%82%E5%B8%B8%E9%97%AE%E9%A2%98/</guid>
        
        <category>Go</category>
        
        <category>PHP</category>
        
        <category>Centos</category>
        
        <category>iftop</category>
        
        <category>ps aux</category>
        
        <category>lsof</category>
        
        <category>流量异常排查</category>
        
        
      </item>
    
      <item>
        <title>PHP 实现简单多路归并排序大文件</title>
        <description>&lt;p&gt;算是一道常见的面试题引来的，有些大厂也喜欢把这个题当做面试题。&lt;/p&gt;

&lt;p&gt;题目：例如有一个 1g 的文件，里面存放这乱序不唯一的数字，如果利用 100m 完成整体排序？&lt;/p&gt;

&lt;p&gt;实现过程就是：&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;先将大文件逐行读取，每 10000 行为一组，然后&lt;strong&gt;排序后写入文件&lt;/strong&gt;中，文件名称类似 t1.txt, t2.txt … 这样的名称，直至读取和拆分完毕整个文件，&lt;/li&gt;
  &lt;li&gt;然后遍历所有文件，每个文件先读取第一行，&lt;strong&gt;放入临时排序数组 $tmpNums&lt;/strong&gt;，然后取其中最小值，&lt;strong&gt;放入临时存储数组 $nums&lt;/strong&gt;，同时记录当前位置的索引值 $idx&lt;/li&gt;
  &lt;li&gt;从最小值所在索引对应文件中取下一个数字，放入临时排序数组的当前索引中，然后继续重复第 2 步的操作，直到所有文件的所有内容读取完毕，则整体文件重新排序完毕。&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;以下是一个 PHP 多路归并排序 demo 代码，只是简单的骨架结构，&lt;strong&gt;例如 min 取最小值部分，可以扩展成定长最小堆实现，或者优先队列，能保存值和所在文件就行&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PHP 多路归并 demo 代码&lt;/strong&gt;：&lt;/p&gt;
&lt;div class=&quot;language-php highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;multiWaySort&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// 读取所有的文件描述符&lt;/span&gt;

    &lt;span class=&quot;nv&quot;&gt;$fds&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[];&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$dir&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;scandir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;./runtime/txt/&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;foreach&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$dir&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$file&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;.&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$file&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;..&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nv&quot;&gt;$fds&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;fopen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;./runtime/txt/&apos;&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;rb&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// 读取每个文件的第一行内容，放入临时排序数组&lt;/span&gt;

    &lt;span class=&quot;nv&quot;&gt;$tmpNums&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[];&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;foreach&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$fds&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$fd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;$tmpNums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;fgets&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$fd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;nv&quot;&gt;$nums&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[];&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$tmpNums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;// 最小值放入临时存储数组&lt;/span&gt;

        &lt;span class=&quot;nv&quot;&gt;$value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$tmpNums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;$nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  

        &lt;span class=&quot;c1&quot;&gt;// 读取最小值所在索引，对应的文件下一行内容&lt;/span&gt;

        &lt;span class=&quot;nv&quot;&gt;$idx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;array_search&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$tmpNums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;$next&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;fgets&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$fds&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$idx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;unset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$tmpNums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$idx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;unset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$fds&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$idx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nv&quot;&gt;$tmpNums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$idx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;// 临时存储数组到达一定数量追加写入文件一次&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;foreach&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$nums&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;nv&quot;&gt;$f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;fopen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;./runtime/result.txt&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;ab+&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
                &lt;span class=&quot;nb&quot;&gt;fwrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$value&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;PHP_EOL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;nv&quot;&gt;$nums&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[];&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4999999&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;nv&quot;&gt;$count&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;参考链接：&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.cnblogs.com/standby/p/9780910.html&quot;&gt;大文件排序/外存排序问题&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://blog.51cto.com/supercharles888/1351285&quot;&gt;实战 k 路归并排序&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;最后恰饭 &lt;a href=&quot;https://www.aliyun.com/minisite/goods?userCode=0amqgcs9&quot;&gt;阿里云全系列产品/短信包特惠购买 中小企业上云最佳选择 阿里云内部优惠券&lt;/a&gt;&lt;/p&gt;
</description>
        <pubDate>Tue, 06 Jul 2021 00:00:00 +0000</pubDate>
        <link>http://alpha2016.github.io/2021/07/06/PHP%E5%AE%9E%E7%8E%B0%E7%AE%80%E5%8D%95%E5%A4%9A%E8%B7%AF%E5%BD%92%E5%B9%B6%E6%8E%92%E5%BA%8F%E5%A4%A7%E6%96%87%E4%BB%B6/</link>
        <guid isPermaLink="true">http://alpha2016.github.io/2021/07/06/PHP%E5%AE%9E%E7%8E%B0%E7%AE%80%E5%8D%95%E5%A4%9A%E8%B7%AF%E5%BD%92%E5%B9%B6%E6%8E%92%E5%BA%8F%E5%A4%A7%E6%96%87%E4%BB%B6/</guid>
        
        <category>Go</category>
        
        <category>PHP</category>
        
        <category>微博面试题</category>
        
        <category>拆分大文件</category>
        
        <category>多路归并排序</category>
        
        <category>有限内存排序大文件</category>
        
        
      </item>
    
      <item>
        <title>入门级微服务架构</title>
        <description>&lt;p&gt;&lt;img src=&quot;https://alpha2016.github.io/img/2021-07-01-simple-micro-service.png&quot; alt=&quot;入门级微服务架构图&quot; title=&quot;极简版微服务架构图&quot; /&gt;&lt;/p&gt;

&lt;p&gt;这是一种极为简单的微服务架构，只是为了从业务上拆分，避免项目演进过程中，难以拆分。每个微服务都是一个单独的项目，不对外暴露接口，仅可被入口项目调用，每个微服务对应自己的数据库，redis db，相关配置文件，redis key 必须使用前缀，未来有大key产生容易分析问题，&lt;strong&gt;两个微服务之间可以互相调用接口，不可操作对方数据库，redis等，业务互相独立&lt;/strong&gt;。队列，日志和监控服务统一到第三方基础服务上，方便管理。&lt;/p&gt;

&lt;p&gt;其中所有的调用由网关，也是入口处发起，请求进来之后，需要网关层面解密现有签名，然后加上新的对应微服务的签名，在请求上加上 trace id，调用远程服务，获得结果后返回给请求发起方。&lt;/p&gt;

&lt;p&gt;微服务拆解之后，如果请求量不大，使用 http 协议调用完全足够，如果使用 tcp 等协议，会提升互相调用的响应速度，但是需要额外的注册中心来维护这些开放的地址，会需要额外的维护成本，http 协议的调用的基于路由组件就可以的，而每个微服务保留基础的路由组件，未来方便扩展成为其他形式调用。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;备注：&lt;/strong&gt; Rpc 是一种技术，远程调用技术，封装 http 请求就是这种技术的实现方式。
我们调用函数的本质是寻址函数地址，那么RPC的目标就是函数不在本地（本进程地址空间），但是从调用形式上又和调用地址空间内的函数一致。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;风险点：&lt;/strong&gt; 服务直接的互相调用可以用事件触发，用 http 请求来实现，在遇到 A 服务抖动的时候，B 服务持续调用，不经过限流或者降级等，容易引发服务间的雪崩，这个在项目发展中需要处理好，微服务架构的前期要点是细化拆解，后期要点是服务治理。&lt;/p&gt;

&lt;p&gt;参考链接：&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.zhihu.com/question/65502802&quot;&gt;知乎 微服务架构是什么&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://mp.ofweek.com/ee/a345673120686&quot;&gt;服务间调用与鉴权&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;最后恰饭 &lt;a href=&quot;https://www.aliyun.com/minisite/goods?userCode=0amqgcs9&quot;&gt;阿里云全系列产品/短信包特惠购买 中小企业上云最佳选择 阿里云内部优惠券&lt;/a&gt;&lt;/p&gt;
</description>
        <pubDate>Thu, 01 Jul 2021 00:00:00 +0000</pubDate>
        <link>http://alpha2016.github.io/2021/07/01/%E5%85%A5%E9%97%A8%E7%BA%A7%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84/</link>
        <guid isPermaLink="true">http://alpha2016.github.io/2021/07/01/%E5%85%A5%E9%97%A8%E7%BA%A7%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84/</guid>
        
        <category>Go</category>
        
        <category>PHP</category>
        
        <category>微服务</category>
        
        <category>rpc</category>
        
        <category>注册中心</category>
        
        
      </item>
    
      <item>
        <title>2021年php常见面试题和答案</title>
        <description>&lt;p&gt;&lt;a href=&quot;2021年php常见面试题及答案 pdf&quot;&gt;https://github.com/alpha2016/2021php-interview-notes&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;主要是2021年4-5月本人面试准备和面试中不断完善的知识点，更多是八股文，能应对基础知识类的考察，深入的研究需要日常的积累了，算法类的没有体现在文档中，基本算法类的能在LeetCode上看到，我遇到的除了一线大厂，其他不是很在乎算法的考核，能有思路或者学习过，就可以带过这个问题了。&lt;/p&gt;

&lt;p&gt;感觉面试考索引，联合索引，隔离级别，redis数据结构和底层实现，持久化还有他们两个的主从同步是最多的。&lt;/p&gt;

&lt;p&gt;© 原创文章，个人整理的知识点&lt;/p&gt;

&lt;p&gt;最后恰饭 &lt;a href=&quot;https://www.aliyun.com/minisite/goods?userCode=0amqgcs9&quot;&gt;阿里云全系列产品/短信包特惠购买 中小企业上云最佳选择 阿里云内部优惠券&lt;/a&gt;&lt;/p&gt;
</description>
        <pubDate>Sat, 08 May 2021 00:00:00 +0000</pubDate>
        <link>http://alpha2016.github.io/2021/05/08/2021%E5%B9%B4php%E5%B8%B8%E8%A7%81%E9%9D%A2%E8%AF%95%E9%A2%98%E5%92%8C%E7%AD%94%E6%A1%88/</link>
        <guid isPermaLink="true">http://alpha2016.github.io/2021/05/08/2021%E5%B9%B4php%E5%B8%B8%E8%A7%81%E9%9D%A2%E8%AF%95%E9%A2%98%E5%92%8C%E7%AD%94%E6%A1%88/</guid>
        
        <category>Go</category>
        
        <category>PHP</category>
        
        <category>面试题和答案</category>
        
        <category>redis</category>
        
        <category>innodb</category>
        
        
      </item>
    
      <item>
        <title>推荐几个不错的教程，极客时间专栏</title>
        <description>&lt;blockquote&gt;
  &lt;p&gt;教程小册都看过，价格不是很贵，质量超级高，大佬们觉得不错，可以访问以下链接，通过我的链接购买是8折优惠，我也能得到点返现，重点是学习。（滑稽.gif）,这些课程是一个程序员向上进阶的必经之路，我也是在这样的学习。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;最后恰饭 &lt;a href=&quot;https://www.aliyun.com/minisite/goods?userCode=0amqgcs9&quot;&gt;阿里云全系列产品/短信包特惠购买 中小企业上云最佳选择 阿里云内部优惠券&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;极客时间的数据结构与算法是极客上最火的课程，非常推荐，扫码可以8折购买，这个课绝对物超所值。&lt;/strong&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://time.geekbang.org/column/intro/126?tab=catalog&amp;amp;code=PwGMePbO-oBZQECwPc6vx0f5J6mjEiBj7gkZF9ig-ko%3D&amp;amp;utm_term=SPoster&quot;&gt;极客时间 数据结构与算法优惠购买&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://time.geekbang.org/column/intro/139?tab=catalog&amp;amp;code=diIl3Av7In7p-TmrXqDVZ3CuXDaXfqJR4Ey-HR7L5tw%3D&amp;amp;utm_term=SPoster&quot;&gt;极客时间 MYSQL 45讲优惠购买&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://time.geekbang.org/column/intro/112?code=Z12kgyvGlATwUJZwVZTiSRuyyxal9wmyNyjXtwLNPjY%3D&amp;amp;utm_term=SPoster&quot;&gt;极客时间 Go 语言核心36讲&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://time.geekbang.org/column/intro/234?tab=catalog&amp;amp;code=FGy9EiQL0ccqsDCGyqEqM-AwZj2vO3hJEyWNObhfHxc%3D&amp;amp;utm_term=SPoster&quot;&gt;极客时间 说透中台&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://time.geekbang.org/column/intro/192?tab=catalog&amp;amp;code=2DScTHuk5fm2TKSaqw-39RXQWV%2FC4-eoh2-mMbwMBM8%3D&amp;amp;utm_term=SPoster&amp;amp;page=A&quot;&gt;极客时间 SQL必知必会优惠购买&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://time.geekbang.org/column/intro/170?tab=catalog&amp;amp;code=xkVfFIKX0gorq65YYPQWy1D0XaqCAUAZkGJnnkky-9U%3D&amp;amp;utm_term=SPoster&quot;&gt;极客时间 深入浅出计算机组成原理&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://time.geekbang.org/column/intro/49?code=YEWOh%2FodHyTUXACj61pm1S2ubmY8ERg2%2FckvSJB%2F3II%3D&amp;amp;utm_term=SPoster&quot;&gt;极客时间 技术管理课&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://time.geekbang.org/column/intro/113?tab=catalog&amp;amp;code=F9swm9mmwsPWCzJmtqtLAXr9Uv%2FloJnODXBrpMdkzBU%3D&amp;amp;utm_term=SPoster&quot;&gt;极客时间 技术管理实战36讲&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://time.geekbang.org/course/intro/130?code=eh3BHyG3lG7AVgwxWXsSgvRJZROaofNh-bg7Fu7lHU4%3D&amp;amp;utm_term=SPoster&quot;&gt;极客时间 算法面试通关40讲&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://time.geekbang.org/column/intro/127?tab=catalog&amp;amp;code=MudPOF524YqieuztAOWIy29NsH8rJCJyfd2kXAMvMho%3D&amp;amp;utm_term=SPoster&quot;&gt;极客时间 如何设计一个秒杀系统&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://time.geekbang.org/column/intro/140?tab=catalog&amp;amp;code=FIjdLWufKFEuwdPl06Lkq8B1e8wRB76pIPQX%2Fw%2FMLkw%3D&amp;amp;utm_term=SPoster&quot;&gt;Linux 性能优化实战&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://segmentfault.com/ls/1650000011318558?r=bPqg5S&quot;&gt;周梦康的 PHP 进阶之路讲座&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;多学习，多赚钱，用上面几个码可以享受八折优惠。&lt;/p&gt;
</description>
        <pubDate>Thu, 01 Apr 2021 00:00:00 +0000</pubDate>
        <link>http://alpha2016.github.io/2021/04/01/%E6%8E%A8%E8%8D%90%E5%87%A0%E4%B8%AA%E4%B8%8D%E9%94%99%E7%9A%84%E6%95%99%E7%A8%8B-%E6%9E%81%E5%AE%A2%E6%97%B6%E9%97%B4%E4%B8%93%E6%A0%8F/</link>
        <guid isPermaLink="true">http://alpha2016.github.io/2021/04/01/%E6%8E%A8%E8%8D%90%E5%87%A0%E4%B8%AA%E4%B8%8D%E9%94%99%E7%9A%84%E6%95%99%E7%A8%8B-%E6%9E%81%E5%AE%A2%E6%97%B6%E9%97%B4%E4%B8%93%E6%A0%8F/</guid>
        
        <category>教程</category>
        
        <category>php 进阶之路</category>
        
        <category>付费课程返佣</category>
        
        <category>mysql</category>
        
        <category>数据结构与算法</category>
        
        <category>极客时间</category>
        
        <category>极客时间优惠买课</category>
        
        <category>mysql 45讲特价优惠</category>
        
        <category>Go 语言核心 36 讲</category>
        
        <category>说透中台</category>
        
        <category>管理实战36讲</category>
        
        <category>linux 性能优化实战</category>
        
        <category>算法面试通关40讲</category>
        
        
      </item>
    
      <item>
        <title>一些不常用的PHP字符类型检测函数</title>
        <description>&lt;p&gt;主要是 Ctype 扩展所提供的函数，用来检测在当前的区域设定下，一个字符或者字符串，是否仅包含指定类型的字符。此类函数用来替代简单的正则验证，效率高于正则验证。&lt;strong&gt;参数为字符串，如果给出一个 -128 到 255 之间(含)的 int, 将会被解释为该值对应的 ASCII 字符 (负值将加上 256 以支持扩展 ASCII 字符). 其它整数将会被解释为该值对应的十进制字符串。&lt;/strong&gt;&lt;/p&gt;

&lt;h5 id=&quot;ctype-系列函数&quot;&gt;Ctype 系列函数&lt;/h5&gt;

&lt;ul&gt;
  &lt;li&gt;ctype_alnum — 做字母和数字字符检测，仅包含 [A-Za-z0-9] 返回 true&lt;/li&gt;
  &lt;li&gt;ctype_alpha — 做纯字符检测，仅包含 [A-Za-z] 返回 true&lt;/li&gt;
  &lt;li&gt;ctype_cntrl — 做控制字符检测，\r \t \n 为控制字符，仅包含控制字符才返回 true&lt;/li&gt;
  &lt;li&gt;ctype_digit — 做纯数字检测，&lt;strong&gt;请看下面额外说明&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;ctype_graph — 做可打印字符串检测，空格除外，测试显示包含 \r \t \n 会返回 false，’asdf  ‘  同样会返回 false&lt;/li&gt;
  &lt;li&gt;ctype_lower — 做小写字符检测&lt;/li&gt;
  &lt;li&gt;ctype_print — 做可打印字符检测 ‘asdf  ‘ 字符串会返回 true&lt;/li&gt;
  &lt;li&gt;ctype_punct — 检测可打印的字符是不是不包含空白、数字和字母，其实就是全都是标点符号才返回 true，其他返回 false&lt;/li&gt;
  &lt;li&gt;ctype_space — 做空白字符检测，必须全为 ‘ ‘ 空字符串才返回 true，其他返回 false&lt;/li&gt;
  &lt;li&gt;ctype_upper — 做大写字母检测&lt;/li&gt;
  &lt;li&gt;ctype_xdigit — 检测字符串是否只包含十六进制字符，即只能包含十进制的树枝和 [A-Fa-f] 的字母，否则返回 false&lt;/li&gt;
&lt;/ul&gt;

&lt;h6 id=&quot;ctype_digit-额外说明&quot;&gt;ctype_digit 额外说明：&lt;/h6&gt;
&lt;p&gt;&lt;strong&gt;这个函数的参数要求是一个 string 这一点是非常有用的，因此当你传入一个 integer 的参数也许不能得到期望的结果。&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-php highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$strings&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;1820.20&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;10002&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;wsl!12&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;foreach&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$strings&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$testcase&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;ctype_digit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$testcase&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;The string &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$testcase&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; consists of all digits.&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;The string &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$testcase&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; does not consist of all digits.&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// The string 1820.20 does not consist of all digits.&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// The string 10002 consists of all digits.&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// The string wsl!12 does not consist of all digits.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;第二个示例：直接传入整型数字被转成 ASCII 码处理了&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;language-php highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$numeric_string&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;42&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$integer&lt;/span&gt;        &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;nb&quot;&gt;ctype_digit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$numeric_string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// true&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;ctype_digit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$integer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;// false (ASCII 42 是 * 字符)&lt;/span&gt;

&lt;span class=&quot;nb&quot;&gt;is_numeric&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$numeric_string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;// true&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;is_numeric&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$integer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;          &lt;span class=&quot;c1&quot;&gt;// true&lt;/span&gt;

&lt;span class=&quot;nb&quot;&gt;ctype_digit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;               &lt;span class=&quot;c1&quot;&gt;// false&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;ctype_digit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;256&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;               &lt;span class=&quot;c1&quot;&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;参考链接：&lt;a href=&quot;https://www.php.net/manual/zh/book.ctype.php&quot;&gt;PHP 字符类型检测&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;最后恰饭 &lt;a href=&quot;https://www.aliyun.com/minisite/goods?userCode=0amqgcs9&quot;&gt;阿里云全系列产品/短信包特惠购买 中小企业上云最佳选择 阿里云内部优惠券&lt;/a&gt;&lt;/p&gt;
</description>
        <pubDate>Sun, 21 Feb 2021 00:00:00 +0000</pubDate>
        <link>http://alpha2016.github.io/2021/02/21/%E4%B8%80%E4%BA%9B%E4%B8%8D%E5%B8%B8%E7%94%A8%E7%9A%84PHP%E5%AD%97%E7%AC%A6%E7%B1%BB%E5%9E%8B%E6%A3%80%E6%B5%8B%E5%87%BD%E6%95%B0/</link>
        <guid isPermaLink="true">http://alpha2016.github.io/2021/02/21/%E4%B8%80%E4%BA%9B%E4%B8%8D%E5%B8%B8%E7%94%A8%E7%9A%84PHP%E5%AD%97%E7%AC%A6%E7%B1%BB%E5%9E%8B%E6%A3%80%E6%B5%8B%E5%87%BD%E6%95%B0/</guid>
        
        <category>Go</category>
        
        <category>PHP</category>
        
        <category>ctype</category>
        
        <category>字符类型检测</category>
        
        
      </item>
    
      <item>
        <title>LeetCode 从滑动窗口到初级动态规划解决一类题</title>
        <description>&lt;h5 id=&quot;剑指-offer-57---ii-和为s的连续正数序列&quot;&gt;剑指 Offer 57 - II. 和为s的连续正数序列&lt;/h5&gt;
&lt;p&gt;输入一个正整数 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;target&lt;/code&gt; ，输出所有和为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;target&lt;/code&gt; 的连续正整数序列（至少含有两个数）。&lt;/p&gt;

&lt;p&gt;序列内的数字由小到大排列，不同序列按照首个数字从小到大排列。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;示例 1：&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;err&quot;&gt;输入：&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;target&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;
&lt;span class=&quot;err&quot;&gt;输出：&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;示例 2：&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;err&quot;&gt;输入：&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;target&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt;
&lt;span class=&quot;err&quot;&gt;输出：&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt;
&lt;span class=&quot;err&quot;&gt;``` &lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;限制：&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;

&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;target&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;^&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;

&lt;span class=&quot;err&quot;&gt;来源：力扣（&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LeetCode&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;）&lt;/span&gt;
&lt;span class=&quot;err&quot;&gt;链接：&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;https&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;//leetcode-cn.com/problems/he-wei-sde-lian-xu-zheng-shu-xu-lie-lcof&lt;/span&gt;

&lt;span class=&quot;cp&quot;&gt;##### 解题思路
&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;经典滑动窗口，直接限制最多滑到一半，能省很多计算。&lt;/span&gt;
&lt;span class=&quot;err&quot;&gt; &lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;代码实现：&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;
&lt;span class=&quot;err&quot;&gt;```&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;php&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Solution&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;cm&quot;&gt;/**
     * @param Integer $target
     * @return Integer[][]
     */&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;findContinuousSequence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[];&lt;/span&gt;

        &lt;span class=&quot;cp&quot;&gt;# 滑动窗口的右边界不能超过 target 的中值
&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;target&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;cp&quot;&gt;# 计算当前窗口内数字之和
&lt;/span&gt;
            &lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;curSum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;array_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;

            &lt;span class=&quot;cp&quot;&gt;# 若和小于目标，右指针向右移动，扩大窗口
&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;curSum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
                &lt;span class=&quot;cp&quot;&gt;# 若和大于目标，左指针向右移动，减小窗口
&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;elseif&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;curSum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;cp&quot;&gt;# 相等就把指针形成的窗口添加进输出列表中
&lt;/span&gt;            &lt;span class=&quot;cp&quot;&gt;# 别忘了，这里还要继续扩大寻找下一个可能的窗口
&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
                &lt;span class=&quot;cp&quot;&gt;# 这里用j+=1，i+=1，i+=2都可以的
&lt;/span&gt;
                &lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h5 id=&quot;剑指-offer-42-连续子数组的最大和&quot;&gt;剑指 Offer 42. 连续子数组的最大和&lt;/h5&gt;
&lt;p&gt;输入一个整型数组，数组中的一个或连续多个整数组成一个子数组。求所有子数组的和的最大值。&lt;/p&gt;

&lt;p&gt;要求时间复杂度为O(n)。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;示例1:&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;err&quot;&gt;输入&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nums&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;err&quot;&gt;输出&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;
&lt;span class=&quot;err&quot;&gt;解释&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;err&quot;&gt; 连续子数组 &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;的和最大，为 &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;。&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;来源：力扣（LeetCode）
链接：https://leetcode-cn.com/problems/lian-xu-zi-shu-zu-de-zui-da-he-lcof&lt;/p&gt;

&lt;h5 id=&quot;解题思路-1&quot;&gt;解题思路 1&lt;/h5&gt;
&lt;p&gt;继续滑动窗口，比当前最大值大则右移指针，比当前最大值小则左指针开始收缩。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;代码实现：&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;language-php highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Solution&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;cd&quot;&gt;/**
     * @param Integer[] $nums
     * @return Integer
     */&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;maxSubArray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;//sum 用于记录滑动窗口中所有数的和&lt;/span&gt;

        &lt;span class=&quot;nv&quot;&gt;$sum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$left&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;PHP_INT_MIN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;$count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$right&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$right&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$right&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nv&quot;&gt;$sum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;//每次右指针移动一格，就需要比较一次&lt;/span&gt;

            &lt;span class=&quot;nv&quot;&gt;$result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;//注意这里当 left==right 时，left 指针也要移&lt;/span&gt;

            &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$left&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$right&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$sum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;nv&quot;&gt;$sum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
                &lt;span class=&quot;nv&quot;&gt;$left&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h5 id=&quot;解题思路2-动态规划&quot;&gt;解题思路2 动态规划&lt;/h5&gt;
&lt;p&gt;&lt;strong&gt;动态规划解法：&lt;/strong&gt;
首先定义看给出的大问题是否能拆分成小问题，最直接的就是一些数组中求性质，可以看成一个个更小的数组所构成的小问题。这些小问题构成了了一个个小状态，要想清楚怎么从这些小状态走到大状态，进而解决大问题（这就是状态转移方程）。核心是：
    ○ 如何抽象，定义状态dp[i]
    ○ 状态转移方程的确定 dp[i]如何从之前的状态得到
    ○ 初始的一些状态是可以看出来的比如dp[0],dp[1]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;代码实现：&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;language-php highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Solution&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;cd&quot;&gt;/**
     * @param Integer[] $nums
     * @return Integer
     */&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;maxSubArray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;$count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        
        &lt;span class=&quot;nv&quot;&gt;$dp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;$max&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nv&quot;&gt;$dp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$dp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
            &lt;span class=&quot;nv&quot;&gt;$max&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$dp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;上题可以引申出：买卖股票的最佳时机 Ⅰ： https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/  和上面的类似，股价变化 [7,1,5,3,6,4] 可以转化成 [-6, 4, -2, 3, -2]，买入和卖出一次，可以转换成求连续子数组的最大和&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;代入一个 多个子序列最大和 的问题，例如 给定一个整数数组 nums ，找到一个或多个具有最大和的连续子数组（子数组最少包含一个元素），返回其最大和，若最大和小于0，则返回 0
    ○ 示例：[-2,1,-3,4,-1,2,1,-5,4]
    ○ 输出：12
    ○ 解释：连续子数组 [1]，[4]，[2,1]，[4] 的和最大，为 12。
实际上是这道题是从数组中取所有的正数，累加的结果就是最大和&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;继续引申出另一个类似问题：&lt;/p&gt;

&lt;h5 id=&quot;122-买卖股票的最佳时机-ii&quot;&gt;122. 买卖股票的最佳时机 II&lt;/h5&gt;
&lt;p&gt;给定一个数组，它的第 i 个元素是一支给定股票第 i 天的价格。&lt;/p&gt;

&lt;p&gt;设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易（多次买卖一支股票）。&lt;/p&gt;

&lt;p&gt;注意：你不能同时参与多笔交易（你必须在再次购买前出售掉之前的股票）。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;示例 1:&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;err&quot;&gt;输入&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;err&quot;&gt;输出&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;
&lt;span class=&quot;err&quot;&gt;解释&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;在第&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;天（股票价格&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;）的时候买入，在第&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;天（股票价格&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;）的时候卖出&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;这笔交易所能获得利润&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;。&lt;/span&gt;
&lt;span class=&quot;err&quot;&gt; &lt;/span&gt;    &lt;span class=&quot;err&quot;&gt;随后，在第&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;天（股票价格&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;）的时候买入，在第&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;天（股票价格&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;）的时候卖出&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;这笔交易所能获得利润&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;。&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;示例 2:&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;输入: [1,2,3,4,5]
输出: 4
解释: 在第 1 天（股票价格 = 1）的时候买入，在第 5 天 （股票价格 = 5）的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
     注意你不能在第 1 天和第 2 天接连购买股票，之后再将它们卖出。
     因为这样属于同时参与了多笔交易，你必须在再次购买前出售掉之前的股票。
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;示例 3:&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;err&quot;&gt;输入&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;err&quot;&gt;输出&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;err&quot;&gt;解释&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;在这种情况下&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;没有交易完成&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;所以最大利润为&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;。&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;来源：力扣（LeetCode）
链接：https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;代码实现：&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;language-php highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Solution&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;cd&quot;&gt;/**
     * @param Integer[] $prices
     * @return Integer
     */&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;maxProfit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$prices&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;$ans&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;$count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$prices&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$prices&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$prices&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;nv&quot;&gt;$ans&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$prices&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$prices&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$ans&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;参考链接：&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;a href=&quot;https://time.geekbang.org/course/intro/130?code=eh3BHyG3lG7AVgwxWXsSgvRJZROaofNh-bg7Fu7lHU4%3D&amp;amp;utm_term=SPoster&quot;&gt;极客时间 算法面试通关40讲&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;最后恰饭 &lt;a href=&quot;https://www.aliyun.com/minisite/goods?userCode=0amqgcs9&quot;&gt;阿里云全系列产品/短信包特惠购买 中小企业上云最佳选择 阿里云内部优惠券&lt;/a&gt;&lt;/p&gt;
</description>
        <pubDate>Wed, 23 Dec 2020 00:00:00 +0000</pubDate>
        <link>http://alpha2016.github.io/2020/12/23/LeetCode-%E4%BB%8E%E6%BB%91%E5%8A%A8%E7%AA%97%E5%8F%A3%E5%88%B0%E5%88%9D%E7%BA%A7%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%E8%A7%A3%E5%86%B3%E4%B8%80%E7%B1%BB%E9%A2%98/</link>
        <guid isPermaLink="true">http://alpha2016.github.io/2020/12/23/LeetCode-%E4%BB%8E%E6%BB%91%E5%8A%A8%E7%AA%97%E5%8F%A3%E5%88%B0%E5%88%9D%E7%BA%A7%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%E8%A7%A3%E5%86%B3%E4%B8%80%E7%B1%BB%E9%A2%98/</guid>
        
        <category>Go</category>
        
        <category>PHP</category>
        
        <category>LeetCode 01.05</category>
        
        <category>字符串比较</category>
        
        
      </item>
    
  </channel>
</rss>
