<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.9.3">Jekyll</generator><link href="https://vatalitywang.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://vatalitywang.github.io/" rel="alternate" type="text/html" /><updated>2023-03-18T08:55:28+00:00</updated><id>https://vatalitywang.github.io/feed.xml</id><title type="html">Vatality</title><subtitle>VatalityWang的个人博客</subtitle><author><name>VatalityWang</name></author><entry><title type="html">一个前端需求文档</title><link href="https://vatalitywang.github.io/2021/07/21/%E5%89%8D%E7%AB%AF%E9%9C%80%E6%B1%82%E6%96%87%E6%A1%A3/" rel="alternate" type="text/html" title="一个前端需求文档" /><published>2021-07-21T00:00:00+00:00</published><updated>2021-07-21T00:00:00+00:00</updated><id>https://vatalitywang.github.io/2021/07/21/%E5%89%8D%E7%AB%AF%E9%9C%80%E6%B1%82%E6%96%87%E6%A1%A3</id><content type="html" xml:base="https://vatalitywang.github.io/2021/07/21/%E5%89%8D%E7%AB%AF%E9%9C%80%E6%B1%82%E6%96%87%E6%A1%A3/">&lt;head&gt;
    &lt;script src=&quot;https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
    &lt;script type=&quot;text/x-mathjax-config&quot;&gt;
        MathJax.Hub.Config({
            tex2jax: {
            skipTags: ['script', 'noscript', 'style', 'textarea', 'pre'],
            inlineMath: [['$','$']]
            }
        });
    &lt;/script&gt;
&lt;/head&gt;

&lt;h1 id=&quot;1-总体功能&quot;&gt;1 总体功能&lt;/h1&gt;

&lt;h2 id=&quot;11-个体识别&quot;&gt;1.1 个体识别&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;一次性同时展示多个个体的信号片段。点击按钮显示下一批信号。右边同时更新显示识别结果和置信度。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;12-未知个体检测&quot;&gt;1.2 未知个体检测&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;检测结果中出现低置信度，进入未知个体检测阶段。右侧显示未知个体识别分析结果。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;13-个体特征隐射&quot;&gt;1.3 个体特征隐射&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;特征可视化窗体，实时更新。&lt;/li&gt;
&lt;/ul&gt;</content><author><name>VatalityWang</name></author><category term="学习" /><summary type="html"></summary></entry><entry><title type="html">操作系统</title><link href="https://vatalitywang.github.io/2021/05/01/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/" rel="alternate" type="text/html" title="操作系统" /><published>2021-05-01T00:00:00+00:00</published><updated>2021-05-01T00:00:00+00:00</updated><id>https://vatalitywang.github.io/2021/05/01/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F</id><content type="html" xml:base="https://vatalitywang.github.io/2021/05/01/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/">&lt;head&gt;
    &lt;script src=&quot;https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
    &lt;script type=&quot;text/x-mathjax-config&quot;&gt;
        MathJax.Hub.Config({
            tex2jax: {
            skipTags: ['script', 'noscript', 'style', 'textarea', 'pre'],
            inlineMath: [['$','$']]
            }
        });
    &lt;/script&gt;
&lt;/head&gt;

&lt;h1 id=&quot;1-进程间通信ipc方式&quot;&gt;1 进程间通信(IPC)方式&lt;/h1&gt;
&lt;h2 id=&quot;11-目的&quot;&gt;1.1 目的&lt;/h2&gt;
&lt;p&gt;实现数据共享。&lt;/p&gt;
&lt;h2 id=&quot;12-管道&quot;&gt;1.2 管道&lt;/h2&gt;
&lt;h3 id=&quot;121-特性&quot;&gt;1.2.1 特性&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;半双工，数据只能一个方向流动。双方通信时需要建立两个管道。&lt;/li&gt;
  &lt;li&gt;只能用于父子进程或者兄弟进程之间通信(具有亲缘关系的进程);&lt;/li&gt;
  &lt;li&gt;单独构成一种独立的文件系统，对于管道两端的进程来说就是一个文件；&lt;/li&gt;
  &lt;li&gt;数据读入与写出：一个进程向管道一端写内容，另一个进程从管道另一端读出。从管道缓冲区末尾写，从管道缓冲区头部读；
    &lt;h3 id=&quot;122-实质&quot;&gt;1.2.2 实质&lt;/h3&gt;
    &lt;p&gt;内核缓冲区。进程以先进先出方式读写/存取数据。该缓冲区可以看成一个循环队列，读写的位置都自动增长，不能随意改变。当缓冲区满的时候，有一定的机制控制相应的读进程或者写进程进入等待队列。&lt;/p&gt;
    &lt;h3 id=&quot;123-缺点局限&quot;&gt;1.2.3 缺点(局限)&lt;/h3&gt;
  &lt;/li&gt;
  &lt;li&gt;只支持单向数据流；&lt;/li&gt;
  &lt;li&gt;只能用于具有亲缘关系的进程之间；&lt;/li&gt;
  &lt;li&gt;管道的缓冲区是有限的（管道制存在于内存中，在管道创建时，为缓冲区分配一个页面大小）；&lt;/li&gt;
  &lt;li&gt;管道所传送的是无格式字节流，这就要求管道的读出方和写入方必须事先约定好数据的格式，比如多少字节算作一个消息（或命令、或记录）等等；
    &lt;h3 id=&quot;124-分类&quot;&gt;1.2.4 分类&lt;/h3&gt;
    &lt;h4 id=&quot;1241-匿名管道&quot;&gt;1.2.4.1 匿名管道&lt;/h4&gt;
    &lt;p&gt;没有名字，只能用于亲缘关系的进程间通信。&lt;/p&gt;
    &lt;h4 id=&quot;1242-有名管道fifo&quot;&gt;1.2.4.2 有名管道(FIFO)&lt;/h4&gt;
    &lt;p&gt;不同于匿名管道之处在于它提供了一个路径名与之关联，&lt;strong&gt;以有名管道的文件形式存在于文件系统中&lt;/strong&gt;，这样，&lt;strong&gt;即使与有名管道的创建进程不存在亲缘关系的进程，只要可以访问该路径，就能够彼此通过有名管道相互通信&lt;/strong&gt;，因此，通过有名管道不相关的进程也能交换数据。值的注意的是，有名管道严格遵循&lt;strong&gt;先进先出(first in first out)&lt;/strong&gt;,对匿名管道及有名管道的读总是从开始处返回数据，对它们的写则把数据添加到末尾。它们不支持诸如lseek()等文件定位操作。&lt;strong&gt;有名管道的名字存在于文件系统中，内容存放在内存中。&lt;/strong&gt;&lt;/p&gt;
    &lt;h3 id=&quot;125-总结&quot;&gt;1.2.5 总结&lt;/h3&gt;
  &lt;/li&gt;
  &lt;li&gt;管道是特殊类型的文件，在满足先入先出的原则条件下可以进行读写，但不能进行定位读写。&lt;/li&gt;
  &lt;li&gt;管道是特殊类型的文件，在满足先入先出的原则条件下可以进行读写，但不能进行定位读写。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;无名管道阻塞问题：&lt;/strong&gt;无名管道无需显示打开，创建时直接返回文件描述符，在读写时需要确定对方的存在，否则将退出。如果当前进程向无名管道的一端写数据，必须确定另一端有某一进程。如果写入无名管道的数据超过其最大值，写操作将阻塞，如果管道中没有数据，读操作将阻塞，如果管道发现另一端断开，将自动退出。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;有名管道阻塞问题：&lt;/strong&gt;有名管道在打开时需要确实对方的存在，否则将阻塞。即以读方式打开某管道，在此之前必须一个进程以写方式打开管道，否则阻塞。此外，可以以读写（O_RDWR）模式打开有名管道，即当前进程读，当前进程写，不会阻塞。
    &lt;h2 id=&quot;13-信号signal&quot;&gt;1.3 信号(Signal)&lt;/h2&gt;
    &lt;h3 id=&quot;131-定义&quot;&gt;1.3.1 定义&lt;/h3&gt;
  &lt;/li&gt;
  &lt;li&gt;信号是Linux系统中用于进程间互相通信或者操作的一种机制，信号可以在任何时候发给某一进程，而无需知道该进程的状态。&lt;/li&gt;
  &lt;li&gt;如果该进程当前并未处于执行状态，则该信号就有内核保存起来，直到该进程恢复执行并传递给它为止。&lt;/li&gt;
  &lt;li&gt;如果一个信号被进程设置为阻塞，则该信号的传递被延迟，直到其阻塞被取消是才被传递给进程。
    &lt;h3 id=&quot;132-常用信号&quot;&gt;1.3.2 常用信号&lt;/h3&gt;
  &lt;/li&gt;
  &lt;li&gt;SIGHUP：用户从终端注销，所有已启动进程都将收到该进程。系统缺省状态下对该信号的处理是终止进程。&lt;/li&gt;
  &lt;li&gt;SIGINT：程序终止信号。程序运行过程中，按Ctrl+C键将产生该信号。&lt;/li&gt;
  &lt;li&gt;SIGQUIT：程序退出信号。程序运行过程中，按Ctrl+\键将产生该信号。&lt;/li&gt;
  &lt;li&gt;SIGBUS和SIGSEGV：进程访问非法地址。&lt;/li&gt;
  &lt;li&gt;SIGFPE：运算中出现致命错误，如除零操作、数据溢出等。&lt;/li&gt;
  &lt;li&gt;SIGKILL：用户终止进程执行信号。shell下执行kill -9发送该信号。&lt;/li&gt;
  &lt;li&gt;SIGTERM：结束进程信号。shell下执行kill 进程pid发送该信号。&lt;/li&gt;
  &lt;li&gt;SIGALRM：定时器信号。&lt;/li&gt;
  &lt;li&gt;SIGCLD：子进程退出信号。如果其父进程没有忽略该信号也没有处理该信号，则子进程退出后将形成僵尸进程。
    &lt;h3 id=&quot;132-信号来源&quot;&gt;1.3.2 信号来源&lt;/h3&gt;
    &lt;p&gt;信号是软件层次上对中断机制的一种模拟，是一种异步通信方式，，信号可以在用户空间进程和内核之间直接交互，内核可以利用信号来通知用户空间的进程发生了哪些系统事件，信号事件主要有两个来源：&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;硬件来源：用户按键输入Ctrl+C退出、硬件异常如无效的存储访问等。&lt;/li&gt;
  &lt;li&gt;软件终止：终止进程信号、其他进程调用kill函数、软件异常产生信号。
    &lt;h3 id=&quot;133-生命周期和处理流程&quot;&gt;1.3.3 生命周期和处理流程&lt;/h3&gt;
  &lt;/li&gt;
  &lt;li&gt;信号被某个进程产生，并设置此信号传递的对象（一般为对应进程的pid），然后传递给操作系统；&lt;/li&gt;
  &lt;li&gt;操作系统根据接收进程的设置（是否阻塞）而选择性的发送给接收者，如果接收者阻塞该信号（且该信号是可以阻塞的），操作系统将暂时保留该信号，而不传递，直到该进程解除了对此信号的阻塞（如果对应进程已经退出，则丢弃此信号），如果对应进程没有阻塞，操作系统将传递此信号。&lt;/li&gt;
  &lt;li&gt;目的进程接收到此信号后，将根据当前进程对此信号设置的预处理方式，暂时终止当前代码的执行，保护上下文（主要包括临时寄存器数据，当前程序位置以及当前CPU的状态）、转而执行中断服务程序，执行完成后在回复到中断的位置。当然，对于抢占式内核，在中断返回时还将引发新的调度。
    &lt;h2 id=&quot;14-消息队列&quot;&gt;1.4 消息队列&lt;/h2&gt;
    &lt;h3 id=&quot;141-定义&quot;&gt;1.4.1 定义&lt;/h3&gt;
  &lt;/li&gt;
  &lt;li&gt;消息队列是存放在内核中的消息链表，每个消息队列由消息队列标识符表示。&lt;/li&gt;
  &lt;li&gt;与管道（无名管道：只存在于内存中的文件；命名管道：存在于实际的磁盘介质或者文件系统）不同的是消息队列存放在内核中，只有在内核重启(即，操作系统重启)或者显示地删除一个消息队列时，该消息队列才会被真正的删除。&lt;/li&gt;
  &lt;li&gt;另外与管道不同的是，消息队列在某个进程往一个队列写入消息之前，并不需要另外某个进程在该队列上等待消息的到达。
    &lt;h3 id=&quot;142-特点&quot;&gt;1.4.2 特点&lt;/h3&gt;
  &lt;/li&gt;
  &lt;li&gt;消息队列是消息的链表,具有特定的格式,存放在内存中并由消息队列标识符标识；&lt;/li&gt;
  &lt;li&gt;消息队列允许一个或多个进程向它写入与读取消息；&lt;/li&gt;
  &lt;li&gt;管道和消息队列的通信数据都是先进先出的原则；&lt;/li&gt;
  &lt;li&gt;消息队列可以实现消息的随机查询,消息不一定要以先进先出的次序读取,也可以按消息的类型读取.比FIFO更有优势；&lt;/li&gt;
  &lt;li&gt;消息队列克服了信号承载信息量少，管道只能承载无格式字 节流以及缓冲区大小受限等缺。
    &lt;h3 id=&quot;143-分类&quot;&gt;1.4.3 分类&lt;/h3&gt;
  &lt;/li&gt;
  &lt;li&gt;POSIX消息队列&lt;/li&gt;
  &lt;li&gt;System V消息队列
系统V消息队列目前被大量使用。系统V消息队列是随内核持续的，只有在内核重起或者人工删除时，该消息队列才会被删除。
    &lt;h2 id=&quot;15-共享内存&quot;&gt;1.5 共享内存&lt;/h2&gt;
  &lt;/li&gt;
  &lt;li&gt;使得多个进程可以可以直接读写同一块内存空间，是最快的可用IPC形式。是针对其他通信机制运行效率较低而设计的。&lt;/li&gt;
  &lt;li&gt;为了在多个进程间交换信息，内核专门留出了一块内存区，可以由需要访问的进程将其映射到自己的私有地址空间。进程就可以直接读写这一块内存而不需要进行数据的拷贝，从而大大提高效率。&lt;/li&gt;
  &lt;li&gt;由于多个进程共享一段内存，因此需要依靠某种同步机制（如信号量）来达到进程间的同步及互斥。
    &lt;h2 id=&quot;16-信号量&quot;&gt;1.6 信号量&lt;/h2&gt;
    &lt;p&gt;信号量是一个计数器，用于多进程对共享数据的访问，信号量的意图在于进程间同步。&lt;/p&gt;
    &lt;h3 id=&quot;161-操作&quot;&gt;1.6.1 操作&lt;/h3&gt;
  &lt;/li&gt;
  &lt;li&gt;创建一个信号量：这要求调用者指定初始值，对于二值信号量来说，它通常是1，也可是0。&lt;/li&gt;
  &lt;li&gt;等待一个信号量：该操作会测试这个信号量的值，如果小于0，就阻塞。也称为P操作。&lt;/li&gt;
  &lt;li&gt;挂出一个信号量：该操作将信号量的值加1，也称为V操作。
    &lt;h3 id=&quot;162-实现&quot;&gt;1.6.2 实现&lt;/h3&gt;
    &lt;p&gt;为了正确地实现信号量，信号量值的测试及减1操作应当是原子操作。为此，信号量通常是在内核中实现的。&lt;/p&gt;
    &lt;h3 id=&quot;163-分类&quot;&gt;1.6.3 分类&lt;/h3&gt;
    &lt;p&gt;linux中三种：&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;Posix（可移植性操作系统接口）有名信号量（使用Posix IPC名字标识）；&lt;/li&gt;
  &lt;li&gt;Posix基于内存的信号量（存放在共享内存区中）；&lt;/li&gt;
  &lt;li&gt;System V信号量（在内核中维护）。
这三种信号量都可用于进程间或线程间的同步。
    &lt;h3 id=&quot;164-信号量与普通整型变量的区别&quot;&gt;1.6.4 信号量与普通整型变量的区别&lt;/h3&gt;
  &lt;/li&gt;
  &lt;li&gt;信号量是非负整型变量，除了初始化之外，它只能通过两个标准原子操作：wait(semap) , signal(semap) ; 来进行访问；&lt;/li&gt;
  &lt;li&gt;操作也被成为PV原语（P来源于荷兰语proberen”测试”，V来源于荷兰语verhogen”增加”，P表示通过的意思，V表示释放的意思），而普通整型变量则可以在任何语句块中被访问；
    &lt;h3 id=&quot;164-信号量与互斥量之间的区别&quot;&gt;1.6.4 信号量与互斥量之间的区别&lt;/h3&gt;
  &lt;/li&gt;
  &lt;li&gt;互斥量用于线程的互斥，信号量用于线程的同步。这是互斥量和信号量的根本区别，也就是互斥和同步之间的区别。互斥：是指某一资源同时只允许一个访问者对其进行访问，具有唯一性和排它性。但互斥无法限制访问者对资源的访问顺序，即访问是无序的。同步：是指在互斥的基础上（大多数情况），通过其它机制实现访问者对资源的有序访问。在大多数情况下，同步已经实现了互斥，特别是所有写入资源的情况必定是互斥的。少数情况是指可以允许多个访问者同时访问资源。&lt;/li&gt;
  &lt;li&gt;互斥量值只能为0/1，信号量值可以为非负整数。
也就是说，一个互斥量只能用于一个资源的互斥访问，它不能实现多个资源的多线程互斥问题。信号量可以实现多个同类资源的多线程互斥和同步。当信号量为单值信号量是，也可以完成一个资源的互斥访问。&lt;/li&gt;
  &lt;li&gt;互斥量的加锁和解锁必须由同一线程分别对应使用，信号量可以由一个线程释放，另一个线程得到。
    &lt;h2 id=&quot;17-套接字socket&quot;&gt;1.7 套接字(socket)&lt;/h2&gt;
    &lt;p&gt;套接字是一种通信机制，凭借这种机制，客户/服务器（即要进行通信的进程）系统的开发工作既可以在本地单机上进行，也可以跨网络进行。也就是说它可以让不在同一台计算机但通过网络连接计算机上的进程进行通信。套接字是支持TCP/IP的网络通信的基本操作单元，可以看做是不同主机之间的进程进行双向通信的端点，简单的说就是通信的两方的一种约定，用套接字中的相关函数来完成通信过程。&lt;/p&gt;
    &lt;h3 id=&quot;171-特性&quot;&gt;1.7.1 特性&lt;/h3&gt;
    &lt;p&gt;套接字的特性由3个属性确定，它们分别是：域、端口号、协议类型。&lt;/p&gt;
    &lt;h4 id=&quot;1711-套接字的域&quot;&gt;1.7.1.1 套接字的域&lt;/h4&gt;
    &lt;p&gt;它指定套接字通信中使用的网络介质，最常见的套接字域有两种：一是AF_INET，它指的是Internet网络。当客户使用套接字进行跨网络的连接时，它就需要用到服务器计算机的IP地址和端口来指定一台联网机器上的某个特定服务，所以在使用socket作为通信的终点，服务器应用程序必须在开始通信之前绑定一个端口，服务器在指定的端口等待客户的连接。另一个域AF_UNIX，表示UNIX文件系统，它就是文件输入/输出，而它的地址就是文件名。&lt;/p&gt;
    &lt;h4 id=&quot;1711-套接字的端口号&quot;&gt;1.7.1.1 套接字的端口号&lt;/h4&gt;
    &lt;p&gt;每一个基于TCP/IP网络通讯的程序(进程)都被赋予了唯一的端口和端口号，端口是一个信息缓冲区，用于保留Socket中的输入/输出信息，端口号是一个16位无符号整数，范围是0-65535，以区别主机上的每一个程序（端口号就像房屋中的房间号），低于256的端口号保留给标准应用程序，比如pop3的端口号就是110，每一个套接字都组合进了IP地址、端口，这样形成的整体就可以区别每一个套接字。&lt;/p&gt;
    &lt;h4 id=&quot;1711-套接字协议类型&quot;&gt;1.7.1.1 套接字协议类型&lt;/h4&gt;
    &lt;p&gt;因特网提供三种通信机制。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;流套接字&lt;/strong&gt;，流套接字在域中通过TCP/IP连接实现，同时也是AF_UNIX中常用的套接字类型。流套接字提供的是一个有序、可靠、双向字节流的连接，因此发送的数据可以确保不会丢失、重复或乱序到达，而且它还有一定的出错后重新发送的机制。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;数据报套接字&lt;/strong&gt;，它不需要建立连接和维持一个连接，它们在域中通常是通过UDP/IP协议实现的。它对可以发送的数据的长度有限制，数据报作为一个单独的网络消息被传输,它可能会丢失、复制或错乱到达，UDP不是一个可靠的协议，但是它的速度比较高，因为它并一需要总是要建立和维持一个连接。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;原始套接字&lt;/strong&gt;，原始套接字允许对较低层次的协议直接访问，比如IP、 ICMP协议，它常用于检验新的协议实现，或者访问现有服务中配置的新设备，因为RAW SOCKET可以自如地控制Windows下的多种协议，能够对网络底层的传输机制进行控制，所以可以应用原始套接字来操纵网络层和传输层应用。比如，我们可以通过RAW SOCKET来接收发向本机的ICMP、IGMP协议包，或者接收TCP/IP栈不能够处理的IP包，也可以用来发送一些自定包头或自定协议的IP包。网络监听技术很大程度上依赖于SOCKET_RAW。
      原始套接字与标准套接字的区别在于：
      原始套接字可以读写内核没有处理的IP数据包，而流套接字只能读取TCP协议的数据，数据报套接字只能读取UDP协议的数据。因此，如果要访问其他协议发送数据必须使用原始套接字。
    &lt;h3 id=&quot;173-建立流程&quot;&gt;1.7.3 建立流程&lt;/h3&gt;
    &lt;p&gt;服务端：&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;首先服务器应用程序用系统调用socket来创建一个套接字，它是系统分配给该服务器进程的类似文件描述符的资源，它不能与其他的进程共享。&lt;/li&gt;
  &lt;li&gt;然后，服务器进程会给套接字起个名字，我们使用系统调用bind来给套接字命名。然后服务器进程就开始等待客户连接到这个套接字。&lt;/li&gt;
  &lt;li&gt;接下来，系统调用listen来创建一个队列并将其用于存放来自客户的进入连接。&lt;/li&gt;
  &lt;li&gt;最后，服务器通过系统调用accept来接受客户的连接。它会创建一个与原有的命名套接不同的新套接字，这个套接字只用于与这个特定客户端进行通信，而命名套接字（即原先的套接字）则被保留下来继续处理来自其他客户的连接（建立客户端和服务端的用于通信的流，进行通信）。
客户端：&lt;/li&gt;
  &lt;li&gt;客户应用程序首先调用socket来创建一个未命名的套接字，然后将服务器的命名套接字作为一个地址来调用connect与服务器建立连接。&lt;/li&gt;
  &lt;li&gt;客户应用程序首先调用socket来创建一个未命名的套接字，然后将服务器的命名套接字作为一个地址来调用connect与服务器建立连接。&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;2-死锁&quot;&gt;2 死锁&lt;/h1&gt;
&lt;p&gt;死锁是进程死锁的简称。若干进程竞争有限资源，又推进顺序不当，从而构成无限期循环等待的局面。&lt;/p&gt;

&lt;h1 id=&quot;3-unix-io&quot;&gt;3 unix I/O&lt;/h1&gt;
&lt;h2 id=&quot;31-阻塞式io&quot;&gt;3.1 阻塞式I/O&lt;/h2&gt;
&lt;p&gt;I/O操作如果不能立即获得返回，就一直在那里等待。&lt;/p&gt;

&lt;h2 id=&quot;32-异步io&quot;&gt;3.2 异步I/O&lt;/h2&gt;
&lt;p&gt;进程告诉内核：当描述符准备好可以进行I/O时，用一个信号通知它。&lt;/p&gt;
&lt;h2 id=&quot;33-非阻塞式io&quot;&gt;3.3 非阻塞式I/O&lt;/h2&gt;
&lt;p&gt;发起一个操作后并不需要等待，可以立即得到一个结果。诸如open,write,read这样的I/O操作。如果操作不能完成，立即出错返回，表示该操作若继续执行将阻塞。
设置非阻塞I/O的方法：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;调用open，设置O_NOBLOCK参数。&lt;/li&gt;
  &lt;li&gt;对于已经打开的文件描述符，调用fctnl函数，由该函数打开O_NOBLOCK标志位。
    &lt;h2 id=&quot;33-io多路转接io-multiplexing&quot;&gt;3.3 I/O多路转接(I/O multiplexing)&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;又称事件驱动型I/O。单个进程可以处理多个网络连接的I/O。I/O 多路复用的特点是通过一种机制一个进程能同时等待多个文件描述符，而这些文件描述符（套接字描述符）其中的任意一个进入读就绪状态，select/epoll函数就可以返回。&lt;/p&gt;

&lt;h3 id=&quot;331-select&quot;&gt;3.3.1 select&lt;/h3&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;cp&quot;&gt;#include&amp;lt;sys/select.h&amp;gt;
&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;maxfdp1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fd_set&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;restrict&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;readfds&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fd_set&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;restrict&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;writefds&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fd_set&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;restrict&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;exceptfds&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;timeval&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;restrict&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tvptr&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;ul&gt;
  &lt;li&gt;tvptr 指定愿意等待的时间长度，通常以秒/微妙为单位。&lt;/li&gt;
  &lt;li&gt;readfds，writefds，exceptfds指向描述符集的指针。分别指：关心的可读，可写，异常描述符集合。&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;maxfdp1:最大文件描述符编号加1，在三个描述符集中找出最大的描述符编号值，然后加1。unix系统中FD_SETSIZE指最大文件描述符为1024.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;返回值：
    &lt;ul&gt;
      &lt;li&gt;-1：表示出错；&lt;/li&gt;
      &lt;li&gt;0：指定的时间过了，没有描述符准备好；&lt;/li&gt;
      &lt;li&gt;正数：已准备好的描述符数量。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&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;对于读写和异常条件，普通的文件描述符总是返回准备好。
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;332-poll&quot;&gt;3.3.2 poll&lt;/h3&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;cp&quot;&gt;#include&amp;lt;poll.h&amp;gt;
&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;poll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;pollfd&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fdarry&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[],&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nfds_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nfds&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;timeout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//返回值：准备就绪的文件描述符数目；若超时，返回0；若出错，返回-1。&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;不是为每个条件（可读，可写和异常条件）构造一个描述符集，而是构造一个pollfd结构的数组，每个数组元素指定一个描述符编号以及我们对该描述符感兴趣的条件。&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;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pollfd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fd&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;kt&quot;&gt;short&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;events&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//感兴趣的fd上的事件&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;short&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;revents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//fd上发生的事件&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;ul&gt;
  &lt;li&gt;fdarry: pollfd 数组；&lt;/li&gt;
  &lt;li&gt;nfds：pollfd 数组大小；&lt;/li&gt;
  &lt;li&gt;timeout:愿意等待的时间。
    &lt;ul&gt;
      &lt;li&gt;-1：永远等待；&lt;/li&gt;
      &lt;li&gt;0：不等；&lt;/li&gt;
      &lt;li&gt;正数：指定的描述符之一准备好或者timeout时间到达，立即返回。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&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;一个描述符是否阻塞不会影响poll是否阻塞。
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;333-epoll&quot;&gt;3.3.3 epoll&lt;/h3&gt;

&lt;p&gt;epoll是在2.6内核中提出的，是之前的select和poll的增强版本。相对于select和poll来说，epoll更加灵活，没有描述符限制。epoll使用一个文件描述符管理多个描述符，将用户关系的文件描述符的事件存放到内核的一个事件表中，这样在用户空间和内核空间的copy只需一次。&lt;/p&gt;

&lt;h3 id=&quot;334-总结&quot;&gt;3.3.4 总结&lt;/h3&gt;

&lt;p&gt;如果处理的连接数不是很高的话，使用select/epoll的web server不一定比使用multi-threading + blocking IO的web server性能更好，可能延迟还更大。select/epoll的优势并不是对于单个连接能处理得更快，而是在于能处理更多的连接。）&lt;/p&gt;
&lt;h1 id=&quot;4-内存池&quot;&gt;4 内存池&lt;/h1&gt;
&lt;p&gt;平常我们使用new、malloc在堆区申请一块内存，但由于每次申请的内存大小不一样就会产生很多内存碎片，造成不好管理与浪费的情况。内存池则是在真正使用内存之前，先申请分配一定数量的、大小相等(一般情况下)的内存块留作备用。当有新的内存需求时，就从内存池中分出一部分内存块，若内存块不够再继续申请新的内存。这样做的一个显著优点是尽量避免了内存碎片，使得内存分配效率得到提升。&lt;/p&gt;

&lt;h1 id=&quot;5-进程池线程池&quot;&gt;5 进程池&amp;amp;&amp;amp;线程池&lt;/h1&gt;
&lt;p&gt;这两个问题有一定的相似度，在面向对象程序编程中，对象的创建与析构都是一个较为复杂的过程，较费时间，所以为了提高程序的运行效率尽可能减少创建和销毁对象的次数，特别是一些很耗资源的对象创建和销毁。所以我们可以创建一个进程池（线程池）,预先放一些进程（线程）进去,要用的时候就直接调用,用完之后再把进程归还给进程池,省下创建删除进程的时间,不过当然就需要额外的开销了。利用线程池与进程池可以使管理进程与线程的工作交给系统管理，不需要程序员对里面的线程、进程进行管理。&lt;/p&gt;
&lt;h2 id=&quot;51-线程池作用&quot;&gt;5.1 线程池作用&lt;/h2&gt;
&lt;p&gt;1、需要大量的线程来完成任务，且完成任务的时间比较短。 WEB服务器完成网页请求这样的任务，使用线程池技术是非常合适的。因为单个任务小，而任务数量巨大，你可以想象一个热门网站的点击次数。但对于长时间的任务，比如一个Telnet连接请求，线程池的优点就不明显了。因为Telnet会话时间比线程的创建时间大多了。
2、对性能要求苛刻的应用，比如要求服务器迅速响应客户请求。
3、接受突发性的大量请求，但不至于使服务器因此产生大量线程的应用。突发性大量客户请求，在没有线程池情况下，将产生大量线程，虽然理论上大部分操作系统线程数目最大值不是问题，短时间内产生大量线程可能使内存到达极限，并出现”OutOfMemory”的错误。&lt;/p&gt;

&lt;h2 id=&quot;52-线程池优点&quot;&gt;5.2 线程池优点&lt;/h2&gt;
&lt;p&gt;多线程技术主要解决处理器单元内多个线程执行的问题，它可以显著减少处理器单元的闲置时间，增加处理器单元的吞吐能力。&lt;/p&gt;

&lt;h1 id=&quot;6-reactor与proactor&quot;&gt;6 Reactor与Proactor&lt;/h1&gt;

&lt;p&gt;&lt;img src=&quot;/images/blog/image-20210819203933950.png&quot; alt=&quot;image-20210819203933950&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/blog/image-20210819204504062.png&quot; alt=&quot;image-20210819204504062&quot; /&gt;&lt;/p&gt;</content><author><name>VatalityWang</name></author><category term="学习" /><summary type="html"></summary></entry><entry><title type="html">数据库</title><link href="https://vatalitywang.github.io/2021/05/01/%E6%95%B0%E6%8D%AE%E5%BA%93/" rel="alternate" type="text/html" title="数据库" /><published>2021-05-01T00:00:00+00:00</published><updated>2021-05-01T00:00:00+00:00</updated><id>https://vatalitywang.github.io/2021/05/01/%E6%95%B0%E6%8D%AE%E5%BA%93</id><content type="html" xml:base="https://vatalitywang.github.io/2021/05/01/%E6%95%B0%E6%8D%AE%E5%BA%93/">&lt;head&gt;
    &lt;script src=&quot;https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
    &lt;script type=&quot;text/x-mathjax-config&quot;&gt;
        MathJax.Hub.Config({
            tex2jax: {
            skipTags: ['script', 'noscript', 'style', 'textarea', 'pre'],
            inlineMath: [['$','$']]
            }
        });
    &lt;/script&gt;
&lt;/head&gt;

&lt;h1 id=&quot;1-sql-vs-mysql&quot;&gt;1 sql vs mysql&lt;/h1&gt;

&lt;p&gt;sql是structured query language的缩写，是一种专门用来与数据库通信的语言。&lt;/p&gt;

&lt;p&gt;mysql是一种数据库管理系统DBMS(database management system)。&lt;/p&gt;

&lt;h1 id=&quot;2-常用命令&quot;&gt;2 常用命令&lt;/h1&gt;

&lt;p&gt;show databases:显示所有数据库；&lt;/p&gt;

&lt;p&gt;show tables:显示当前可用数据库中可用表的列表；&lt;/p&gt;

&lt;p&gt;show columns from customers:显示表列；返回一个表，包字段名，数据类型，是否允许null，键信息，默认信息以及其他信息。等价于describe customers.&lt;/p&gt;

&lt;p&gt;show status:显示广泛的状态信息。&lt;/p&gt;

&lt;p&gt;show grants:显示授予用户的安全权限。&lt;/p&gt;

&lt;p&gt;select 列名 from 表名：从某个表中查询某个列。&lt;/p&gt;
&lt;h1 id=&quot;3-数据过滤&quot;&gt;3 数据过滤&lt;/h1&gt;
&lt;h2 id=&quot;31--in操作符&quot;&gt;3.1  IN操作符&lt;/h2&gt;

&lt;p&gt;IN操作符用来指定条件范围，范围中的每个条件都可以匹配，用圆括号来匹配合法的条件清单，条件之间用逗号分隔。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;功能与OR相当；&lt;/li&gt;
  &lt;li&gt;在使用长的合法清单选项时，IN操作符的语法更清晰直观；&lt;/li&gt;
  &lt;li&gt;IN操作符计算次序更容易管理；&lt;/li&gt;
  &lt;li&gt;比OR操作符清单执行更快。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;其他操作符：or,and,not。&lt;/p&gt;

&lt;h1 id=&quot;4-通配符过滤&quot;&gt;4 通配符过滤&lt;/h1&gt;

&lt;p&gt;创建比较特定数据的搜索模式。&lt;/p&gt;

&lt;h2 id=&quot;41-通配符wildcard&quot;&gt;4.1 通配符(wildcard)&lt;/h2&gt;

&lt;p&gt;用来匹配值的一部分的特色字符，用于创建特定数据的搜素模式。使用通配符必须使用LIKE操作符。&lt;/p&gt;
&lt;h2 id=&quot;42-搜索模式&quot;&gt;4.2 搜索模式&lt;/h2&gt;

&lt;p&gt;由字面值、通配符或者两者组合构成的搜索条件。&lt;/p&gt;
&lt;h2 id=&quot;43-两类通配符&quot;&gt;4.3 两类通配符&lt;/h2&gt;
&lt;h3 id=&quot;431-任何字符出现任意次0个1个或者多个&quot;&gt;4.3.1 %：任何字符出现任意次，0个，1个或者多个。&lt;/h3&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;err&quot;&gt;查找出所有以词&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;jet&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;起头的产品。&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;select&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prod_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prod_name&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;products&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;where&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prod_name&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;like&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'jet%'&lt;/span&gt;  
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;432-_匹配单个字符&quot;&gt;4.3.2 _:匹配单个字符。&lt;/h2&gt;

&lt;p&gt;使用注意：&lt;/p&gt;

&lt;p&gt;如果其他操作符可以达到相同的目的，不要使用通配符。&lt;/p&gt;

&lt;p&gt;不要把通配符置于搜索模式的开始处，因为搜索起来会很慢。&lt;/p&gt;

&lt;p&gt;注意通配符的位置不要放错。&lt;/p&gt;

&lt;h1 id=&quot;5-正则表达式&quot;&gt;5 正则表达式&lt;/h1&gt;

&lt;h2 id=&quot;51-基本字符匹配&quot;&gt;5.1 基本字符匹配&lt;/h2&gt;
&lt;p&gt;在sql语句中增加REGEXP关键字。&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;检索prod_name包含文本1000的所有行。
select prod_name FROM products WHERE prod_name REGEXP '1000' ORDER BY prod_name;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;52-or匹配&quot;&gt;5.2 or匹配&lt;/h2&gt;

&lt;table&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;：两个之一；&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;[]:匹配几个之一；中括号中可以是数字，也可以是某一范围的字符，如：[0-4]。-:表示范围。&lt;/p&gt;

&lt;table&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;\:转义。用于表达具有特殊含义的字符，比如‘.’,’&lt;/td&gt;
      &lt;td&gt;’,’-‘。\\:匹配‘\’；&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;53-匹配多个实例&quot;&gt;5.3 匹配多个实例&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;*&lt;/th&gt;
      &lt;th&gt;0个或者多个匹配&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;+&lt;/td&gt;
      &lt;td&gt;1个或者多个匹配（等于{1，}）&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;？&lt;/td&gt;
      &lt;td&gt;0个或者1个匹配P（等价于{0，1}）&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;{n}&lt;/td&gt;
      &lt;td&gt;指定数目的匹配&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;{n,}&lt;/td&gt;
      &lt;td&gt;不少于指定数目的匹配&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;{m,n}&lt;/td&gt;
      &lt;td&gt;匹配数目的范围(m不超过255)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;54-定位符&quot;&gt;5.4 定位符&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&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;$&lt;/td&gt;
      &lt;td&gt;文本的结尾&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;[[:&amp;lt;:]]&lt;/td&gt;
      &lt;td&gt;词的开始&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;[[:&amp;gt;:]]&lt;/td&gt;
      &lt;td&gt;词的结尾&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&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;prod_name&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;products&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;where&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prod_name&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;REGEXP&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'^[0-9&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\\&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;.]'&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;prod_name&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;err&quot;&gt;匹配以数字或者&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;&quot;.&quot;&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;h2 id=&quot;55-like和regexp的区别&quot;&gt;5.5 LIKE和REGEXP的区别&lt;/h2&gt;

&lt;p&gt;like匹配整个字符串；&lt;/p&gt;

&lt;p&gt;regexp匹配子串。&lt;/p&gt;
&lt;h1 id=&quot;6-创建计算字段&quot;&gt;6 创建计算字段&lt;/h1&gt;
&lt;p&gt;把数据库中的字段转换为期望输出的格式&lt;/p&gt;
&lt;h2 id=&quot;61-concatenate&quot;&gt;6.1 concatenate&lt;/h2&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;n&quot;&gt;Concat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vend_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;' ('&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vend_country&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;')'&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;n&quot;&gt;vendors&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;vend_name&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;h2 id=&quot;62-使用别名&quot;&gt;6.2 使用别名&lt;/h2&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;Concat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vend_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;' ('&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vend_country&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;')'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;AS&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vend_title&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vendors&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;vend_name&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;h2 id=&quot;63-算术计算&quot;&gt;6.3 算术计算&lt;/h2&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;prod_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;quantity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item_price&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;quantity&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;item_price&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;AS&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expand_price&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;orderitems&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;WHERE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;order_num&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;20005&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;h1 id=&quot;7-数据处理函数&quot;&gt;7 数据处理函数&lt;/h1&gt;
&lt;h2 id=&quot;71-文本处理&quot;&gt;7.1 文本处理&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&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;Length&lt;/td&gt;
      &lt;td&gt;返回串的长度&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Left&lt;/td&gt;
      &lt;td&gt;返回串左边的字符&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Locate&lt;/td&gt;
      &lt;td&gt;找出串的一个子串&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Lower&lt;/td&gt;
      &lt;td&gt;将串转换为小写&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;LTrim&lt;/td&gt;
      &lt;td&gt;去掉串左边的空格&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Right&lt;/td&gt;
      &lt;td&gt;返回串右边的字符&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;RTrim&lt;/td&gt;
      &lt;td&gt;去掉串右边的空格&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Soundex&lt;/td&gt;
      &lt;td&gt;返回串的SOUNDEX值&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;SubString&lt;/td&gt;
      &lt;td&gt;返回子串的字符&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Upper&lt;/td&gt;
      &lt;td&gt;将串转换为大写&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;72-日期处理函数&quot;&gt;7.2 日期处理函数&lt;/h2&gt;
&lt;p&gt;mysql中的日期格式：yyyy-mm-dd。
Date 只包含年月日。 
Time 包含年月日，时分秒。&lt;/p&gt;

&lt;h2 id=&quot;73-数值处理函数&quot;&gt;7.3 数值处理函数&lt;/h2&gt;
&lt;p&gt;Abs()；Cos();Exp();Mod();Pi();Rand();Sin();Sqrt();Tan();&lt;/p&gt;

&lt;h1 id=&quot;8-分组数据&quot;&gt;8 分组数据&lt;/h1&gt;
&lt;h2 id=&quot;81-group-by&quot;&gt;8.1 group by&lt;/h2&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;o&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;从产品列表中统计所有供应商的产品总数，并以供应商&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;分组。&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ven_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;COUNT&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;k&quot;&gt;AS&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_prods&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;products&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;vend_id&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;h2 id=&quot;82-having&quot;&gt;8.2 having&lt;/h2&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;o&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;列出至少有两个订单的所有顾客&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cust_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;COUNT&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;k&quot;&gt;AS&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;orders&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;orders&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;cust_id&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;HAVING&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;COUNT&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;o&quot;&gt;&amp;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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;where和having的区别&quot;&gt;WHERE和HAVING的区别&lt;/h3&gt;
&lt;p&gt;where是对行值数据进行筛选，是在分组前；group by在数据分组后进行过滤。where排除的行不包含在分组中。where可能影响having的分组结果。&lt;/p&gt;

&lt;h2 id=&quot;83-order-by&quot;&gt;8.3 order by&lt;/h2&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;o&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;检索总计订单价格大于等于&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;的订单号和总计订单价格&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;order_num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;SUM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;quantity&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;item_price&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;AS&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ordertotal&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;orderitems&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;order_num&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;HAVING&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;SUM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;quantity&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;item_price&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;50&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;ordertotal&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;h1 id=&quot;9-子查询&quot;&gt;9 子查询&lt;/h1&gt;
&lt;h2 id=&quot;91-利用子查询进行过滤&quot;&gt;9.1 利用子查询进行过滤&lt;/h2&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;o&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;查询购买商品&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TNT2&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;的客户信息。&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cust_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cust_contact&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;customers&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;WHERE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cust_id&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;IN&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;cust_id&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;orders&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;WHERE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;order_num&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;IN&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;order_name&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;orderitems&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;WHERE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prod_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'TNT2'&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;h2 id=&quot;92-计算字段作为子查询&quot;&gt;9.2 计算字段作为子查询&lt;/h2&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;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;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;）从&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;customers&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;n&quot;&gt;orders&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;中检索每个客户的订单总数。&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cust_item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cust_state&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;k&quot;&gt;COUNT&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;k&quot;&gt;FROM&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;orders&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;WHERE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;orders&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cust_id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;customers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cust_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;AS&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;orders&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;customers&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;cust_name&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;h1 id=&quot;自动增量auto_increment&quot;&gt;自动增量(auto_increment)&lt;/h1&gt;

&lt;p&gt;某些表列需要唯一值，比如订单编号，雇员ID，顾客ID。mysql自动为表中新增加的行分配下一个可用编号，不用手动分配。这个功能就是自动增量。&lt;/p&gt;

&lt;h1 id=&quot;mysql数据库索引结构&quot;&gt;mysql数据库索引结构&lt;/h1&gt;

&lt;p&gt;数据库查询是数据库最主要的功能之一。各种查找算法都基于特定数据结构，数据库中的数据本身的组织结构不可能完全满足各种数据结构。数据维护满足特定查找算法的数据结构，这些数据结构以某种方式(引用)指向数据，称这种引用为索引。&lt;/p&gt;

&lt;p&gt;大部分数据库系统及文件系统都采用B树或者B+树作为索引结构。&lt;/p&gt;

&lt;p&gt;B树：&lt;/p&gt;

&lt;p&gt;B+树：&lt;/p&gt;

&lt;h1 id=&quot;8-事务隔离级别&quot;&gt;8 事务隔离级别&lt;/h1&gt;

&lt;h1 id=&quot;9-mysql中按粒度的锁分类&quot;&gt;9 mysql中按粒度的锁分类&lt;/h1&gt;
&lt;h2 id=&quot;91-表级锁&quot;&gt;9.1 表级锁&lt;/h2&gt;
&lt;p&gt;对当前操作的整张表加锁,实现简单，加锁快，但并发能力低。&lt;/p&gt;
&lt;h2 id=&quot;92-行锁&quot;&gt;9.2 行锁&lt;/h2&gt;
&lt;p&gt;锁住某一行，如果表存在索引，那么记录锁是锁在索引上的，如果表没有索引，那么 InnoDB 会
创建一个隐藏的聚簇索引加锁。行级锁能大大减少数据库操作的冲突。其加锁粒度最小，并发度高，但加锁的开销也最大，加锁慢，会出现死锁。&lt;/p&gt;
&lt;h2 id=&quot;93-gap锁&quot;&gt;9.3 Gap锁&lt;/h2&gt;
&lt;p&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;幻读是指当事务不是独立执行时发生的一种现象。事务A读取与搜索条件相匹配的若干行。事务B以插入或删除行等方式来修改事务A的结果集，然后再提交。幻读是指当事务不是独立执行时发生的一种现象，例如第一个事务对一个表中的数据进行了修改，比如这种修改涉及到表中的“全部数据行”。同时，第二个事务也修改这个表中的数据，这种修改是向表中插入“一行新数据”。那么，以后就会发生操作第一个事务的用户发现表中还存在没有修改的数据行，就好象发生了幻觉一样.一般解决幻读的方法是增加范围锁RangeS，锁定检索范围为只读，这样就避免了幻读。
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;h2 id=&quot;94-next-key-lock&quot;&gt;9.4 Next-key Lock&lt;/h2&gt;
&lt;p&gt;行锁+gap锁。&lt;/p&gt;

&lt;h1 id=&quot;10-如何解决数据库死锁&quot;&gt;10 如何解决数据库死锁&lt;/h1&gt;

&lt;p&gt;预先检测到死锁的循环依赖，并立即返回一个错误 ；
当查询的时间达到锁等待超时的设定后放弃锁请求。&lt;/p&gt;

&lt;h1 id=&quot;11-乐观锁悲观锁&quot;&gt;11 乐观锁、悲观锁&lt;/h1&gt;
&lt;p&gt;乐观锁：对于数据冲突保持一种乐观态度，操作数据时不会对操作的数据进行加锁，只有到数据提交的时候才通过一种机制来验证数据是否存在冲突。
悲观锁：对于数据冲突保持一种悲观态度，在修改数据之前把数据锁住，然后再对数据进行读写，在它释放锁之前任何人都不能对其数据进行操作，直到前面一个人把锁释放后下一个人才可对数据进行加锁，然后才可以对数据进行操作，一般数据库本身锁的机制都是基于悲观锁的机制实现的。
应用建议：
读的多，冲突几率小，乐观锁。
写的多，冲突几率大，悲观锁。&lt;/p&gt;
&lt;h1 id=&quot;12-什么是索引&quot;&gt;12 什么是索引&lt;/h1&gt;
&lt;p&gt;索引是存储引擎中用于快速找到记录的一种数据结构。在关系型数据库中，索引具体是一种对数据库中一列或多列的值进行排序的存储结构。
引入目的：为了提高数据查询的效率。索引对数据库查询良好的性能非常关键，当表中数据量越来越大，索引对性能的影响越重要。&lt;/p&gt;

&lt;h1 id=&quot;13-mysql常见索引类型&quot;&gt;13 mysql常见索引类型&lt;/h1&gt;
&lt;h2 id=&quot;131-数据结构角度&quot;&gt;13.1 数据结构角度&lt;/h2&gt;
&lt;ul&gt;
  &lt;li&gt;B-Tree&lt;/li&gt;
  &lt;li&gt;哈希&lt;/li&gt;
  &lt;li&gt;R-Tree&lt;/li&gt;
  &lt;li&gt;全文索引
    &lt;h2 id=&quot;132-物理存储角度&quot;&gt;13.2 物理存储角度&lt;/h2&gt;
  &lt;/li&gt;
  &lt;li&gt;主键索引（聚簇索引）：叶子节点存的是整行的数据。&lt;/li&gt;
  &lt;li&gt;非主键索引（二级索引）：叶子节点存的主键的值。
    &lt;h1 id=&quot;14-b-tree与b-tree&quot;&gt;14 B-Tree与B+ Tree&lt;/h1&gt;
    &lt;p&gt;B-Tree 是一种自平衡的多叉树。每个节点都存储关键字值。其左子节点的关键字值小于该节点关键字值，且右子节点的关键字值大于或等于该节点关键字值。
B+树也是是一种自平衡的多叉树。其基本定义与B树相同，不同点在于数据只出现在叶子节点，所有叶子节点增加了一个链指针，方便进行范围查询。
B+树中间节点不存放数据，所以同样大小的磁盘页上可以容纳更多节点元素，访问叶子节据也具有更好的缓存命中率。并且数据顺序排列并且相连，所以便于区间查找和搜索。
B树每一个节点都包含key和value，查询效率比B+树高。&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;https://segmentfault.com/a/1190000020416577&lt;/p&gt;

&lt;h2 id=&quot;14-mysql主从复制&quot;&gt;14 mysql主从复制&lt;/h2&gt;
&lt;h3 id=&quot;141-功能&quot;&gt;14.1 功能&lt;/h3&gt;
&lt;p&gt;数据多处自动备份；增加数据库安全性；进行读写分离(即只在MySQL主库上写，只在MySQL从库上读，以减少数据库压力，提高性能),提升数据库库负载性能。&lt;/p&gt;
&lt;h3 id=&quot;142-流程&quot;&gt;14.2 流程&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;在事务完成之前，主库在binlog上记录这些改变，完成binlog写入过程后，主库通知存储引擎提交事物。&lt;/li&gt;
  &lt;li&gt;从库将主库的binlog复制到对应的中继日志，即开辟一个I/O工作线程，I/O线程在主库上打开一个普通的连接，然后开始binlog dump process，将这些事件写入中继日志。从主库的binlog中读取事件，如果已经读到最新了，线程进入睡眠并等待ma主库产生新的事件。&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;15-redis&quot;&gt;15 redis&lt;/h1&gt;
&lt;h2 id=&quot;151-安装&quot;&gt;15.1 安装&lt;/h2&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;$ sudo apt update
$ sudo apt install redis-server
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;h2 id=&quot;152-启动&quot;&gt;15.2 启动&lt;/h2&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;sudo service redis-server start
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;h2 id=&quot;153-redis-单线程为什么快&quot;&gt;15.3 redis 单线程为什么快？&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;串行处理多个socket连接请求，省去了线程切换的开销；&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;纯内存操作；&lt;/li&gt;
  &lt;li&gt;核心是基于非阻塞的IO多路复用机制。&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;16-mysql&quot;&gt;16 mysql&lt;/h1&gt;

&lt;h2 id=&quot;161-安装&quot;&gt;16.1 安装&lt;/h2&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;sudo apt-get install mysql-server mysql-client
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;162-测试是否安装成功&quot;&gt;16.2 测试是否安装成功&lt;/h2&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;sudo netstat -tap | grep mysql
或者
mysql -uroot -p
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;16-redis数据库教程&quot;&gt;16 Redis数据库教程&lt;/h1&gt;

&lt;h2 id=&quot;161-当今企业架构分析&quot;&gt;16.1 当今企业架构分析&lt;/h2&gt;

&lt;p&gt;定位也是一种数据，音乐，热榜等&lt;/p&gt;

&lt;p&gt;mysql不够用，数据量很大，变化很快；&lt;/p&gt;

&lt;h2 id=&quot;162-nosql&quot;&gt;16.2 NoSql&lt;/h2&gt;

&lt;p&gt;Not Only Sql；&lt;/p&gt;

&lt;p&gt;关系型数据库：表格，行，列。&lt;/p&gt;

&lt;p&gt;泛指非关系型数据库，随着web2.0的诞生！传统的关系型数据库难以对付web2.0时代！尤其是超大规模高并发的社区。Redis是目前发展最迅速的NoSql数据库。&lt;/p&gt;

&lt;h3 id=&quot;1621-nosql-优点&quot;&gt;16.2.1 NoSql 优点&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;方便扩展，数据之间没有关系。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;大数据，高性能（redis 读 80000次/秒，学11万次/秒，NoSql的缓存记录级，是一种细粒度的缓存，性能比较高)。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&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;---传统RDBMS
-结构化组织；
-SQL;
-数据和关系都存在单独的表中；
-操作上，数据定义语言；
-严格的一致性；
-基础的事务。
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&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;NoSql
-不仅仅是数据；
-没有固定查询语言；
-键值对存储，列存储，文档存储，图形数据库(社交关系)。
-最终一致性。
-CAP定理和BASE(异地多活)
-高性能，高可用，高可扩展。
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
    &lt;h3 id=&quot;1623-了解&quot;&gt;16.2.3 了解&lt;/h3&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;大数据时代的3v.&lt;/p&gt;

    &lt;p&gt;海量Volume&lt;/p&gt;

    &lt;p&gt;多样Variety&lt;/p&gt;

    &lt;p&gt;实时Veiocity&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;3高&lt;/p&gt;

    &lt;p&gt;高并发&lt;/p&gt;

    &lt;p&gt;高可扩（水平拆分）&lt;/p&gt;

    &lt;p&gt;高性能&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;163-如果你未来相当一个架构师没有什么是加一层解决不了的&quot;&gt;16.3 如果你未来相当一个架构师，没有什么是加一层解决不了的&lt;/h2&gt;

&lt;p&gt;技术文章：阿里云的这群疯子。&lt;/p&gt;

&lt;p&gt;了解：淘宝早年就去IOE。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/blog/image-20210805225139388.png&quot; alt=&quot;image-20210805225139388&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/blog/image-20210805225241530.png&quot; alt=&quot;image-20210805225241530&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/blog/image-20210831143816237.png&quot; alt=&quot;image-20210805230843584&quot; /&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Neo4j&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img src=&quot;/images/blog/image-20210805231142830.png&quot; alt=&quot;image-20210805231142830.png&quot; /&gt;&lt;/p&gt;
&lt;h2 id=&quot;164-redis-vs-memcached&quot;&gt;16.4 Redis vs memcached&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/images/blog/redisVsMemcache.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;</content><author><name>VatalityWang</name></author><category term="学习" /><summary type="html"></summary></entry><entry><title type="html">网络</title><link href="https://vatalitywang.github.io/2021/05/01/%E7%BD%91%E7%BB%9C/" rel="alternate" type="text/html" title="网络" /><published>2021-05-01T00:00:00+00:00</published><updated>2021-05-01T00:00:00+00:00</updated><id>https://vatalitywang.github.io/2021/05/01/%E7%BD%91%E7%BB%9C</id><content type="html" xml:base="https://vatalitywang.github.io/2021/05/01/%E7%BD%91%E7%BB%9C/">&lt;head&gt;
    &lt;script src=&quot;https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
    &lt;script type=&quot;text/x-mathjax-config&quot;&gt;
        MathJax.Hub.Config({
            tex2jax: {
            skipTags: ['script', 'noscript', 'style', 'textarea', 'pre'],
            inlineMath: [['$','$']]
            }
        });
    &lt;/script&gt;
&lt;/head&gt;

&lt;h1 id=&quot;1-tcp&quot;&gt;1 tcp&lt;/h1&gt;

&lt;h2 id=&quot;11-为什么不能两次握手&quot;&gt;1.1 为什么不能两次握手&lt;/h2&gt;

&lt;p&gt;自我解释：客户端发出的请求可能因为忘了延迟，过了一段时间才到达服务器，服务器收到连接请求后，给客户端发送确认消息；此时客户端因为长时间未收到服务器的确认信息，认为服务器异常。则不会处理服务器很晚才到达的确认信息，也就不会再进行第三次握手。如果两次握手，服务器就认为已经建立连接了，实际是连接并未成功建立。&lt;/p&gt;

&lt;p&gt;谢希仁版《计算机网络》：&lt;/p&gt;

&lt;p&gt;“为了防止已失效的连接请求报文段突然又传送到了服务端，因而产生错误”。&lt;/p&gt;

&lt;p&gt;防止旧复用链接的初始化导致问题。&lt;/p&gt;

&lt;p&gt;SYN需要对方确认，要消耗序列号。ACK报文不需要对方确认不消耗序列号。&lt;/p&gt;

&lt;h2 id=&quot;12-为什么要四次挥手&quot;&gt;1.2 为什么要四次挥手&lt;/h2&gt;

&lt;p&gt;当服务端收到客户端的FIN数据包后，服务端数据可能还没有发送完，四次挥手可以防止这种情况发生。&lt;/p&gt;

&lt;h2 id=&quot;13-往返时延rtt&quot;&gt;1.3 往返时延(RTT)&lt;/h2&gt;
&lt;p&gt;RTT(Round-Trip Time): 往返时延。计网中的一个重要的性能指标，表示从发送端发送数据开始到发送端接收到来自数据接送端的确认(接收到数据后立即发送确认)，总共经历的时延。&lt;/p&gt;

&lt;p&gt;组成：链路传播时间+末端系统的处理时间+路由器缓存中的排队和处理时间。前两部分作为一个TCP链接相对固定，路由器缓存中的排队和处理时间会随网络拥塞程度的变化而变化。&lt;/p&gt;

&lt;h2 id=&quot;14-超时重传&quot;&gt;1.4 超时重传&lt;/h2&gt;
&lt;p&gt;接收方每收到一个包就向发送方返回一个ACK,表示自己已经收到了这段数据。如果发送方一段时间内没有收到接收方返回的ACK就知道很可能是数据包丢失了，就重发该数据包，直到收到ACK为止。
	RTO(Retransmission Time Out):重传超时。往往大于2倍的RTT。每个数据包都有相应的计时其，一旦超过RTO而没收到ACK，就会重发该数据包。没收到ACK的数据包都会存在重传缓冲区里面，等到ACK后从缓冲区中删除。超时意味着拥塞，一旦发生这种情况，TCP重传对应数据段，还会降低当前数据的发送速率，因为TCP认为当前网络发生了拥塞。&lt;/p&gt;

&lt;p&gt;tcp面试灵魂12问： https://segmentfault.com/a/1190000023565467&lt;/p&gt;

&lt;h2 id=&quot;15-通过哪些方式保证可靠性&quot;&gt;1.5 通过哪些方式保证可靠性？&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;应用数据被分割成TCP认为最合适发送的数据块；&lt;/li&gt;
  &lt;li&gt;确认机制：发送报文后，等待确认；&lt;/li&gt;
  &lt;li&gt;重发机制：没有收到确认，将重发数据段；&lt;/li&gt;
  &lt;li&gt;保持它首部和数据的校验和。确认数据的准确性；&lt;/li&gt;
  &lt;li&gt;排序，丢弃重复的，流量控制。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;16-tcp与udp的区别&quot;&gt;1.6 TCP与UDP的区别&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;TCP面向连接，UDP面向无连接；&lt;/li&gt;
  &lt;li&gt;TCP面向报文，UDP面向字节流；&lt;/li&gt;
  &lt;li&gt;TCP提供可靠传输服务（数据顺序，正确性），UDP传输不可靠；&lt;/li&gt;
  &lt;li&gt;TCP传输协议慢，UDP协议传输速度快；&lt;/li&gt;
  &lt;li&gt;TCP协议对系统资源要求多(头部开销大)，UDP协议要求少。&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;2-如何在udp的基础上实现一个相对可靠的传输协议&quot;&gt;2 如何在UDP的基础上实现一个相对可靠的传输协议？&lt;/h1&gt;

&lt;h1 id=&quot;3-mslmaximum-segment-lifetime&quot;&gt;3 MSL(Maximum segment lifetime)&lt;/h1&gt;
&lt;p&gt;最大分段寿命是一个TCP 分段可以存在于互联网系统中的最大时间。它被任意地定义为两分钟长。&lt;/p&gt;

&lt;h1 id=&quot;4-lanlocal--area-network&quot;&gt;4 LAN(Local  Area Network)&lt;/h1&gt;
&lt;p&gt;局域网：将小区域各种通信设备互连在一起的通信网络。
典型特性：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;高速率（0.1M~100Mbps）&lt;/li&gt;
  &lt;li&gt;短距离 (0.1km~25km)&lt;/li&gt;
  &lt;li&gt;低误码率（$10^{-8}~10^{-11}$）&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;5-网络各个层的设备&quot;&gt;5 网络各个层的设备&lt;/h1&gt;
&lt;ul&gt;
  &lt;li&gt;应用层：网关，是最复杂的网络互连设备，仅用于两个高层协议不同的网络互连。网关的结构也和路由器类似，不同的是互连层。网关既可以用于广域网互连，也可以用于局域网互连 。&lt;/li&gt;
  &lt;li&gt;传输层：网关&lt;/li&gt;
  &lt;li&gt;网络层(路由选择，存储转发)：路由器，交换机。&lt;/li&gt;
  &lt;li&gt;数据链路层：交换机，网桥。&lt;/li&gt;
  &lt;li&gt;物理层：集线器：纯硬件设备，主要用来连接计算机等网络终端。中继器：在比特级别对网络进行再生和重定时，从而使得它们能够传输更远的距离。&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;6-tcpip协议栈各种协议&quot;&gt;6 TCP/IP协议栈(各种协议)&lt;/h1&gt;

&lt;h2 id=&quot;61-应用层&quot;&gt;6.1 应用层&lt;/h2&gt;

&lt;p&gt;http,https;&lt;/p&gt;

&lt;p&gt;ftp(文件传输协议);&lt;/p&gt;

&lt;p&gt;Telnet: 远程登录协议，用于&lt;a href=&quot;https://zh.wikipedia.org/wiki/網際網路&quot;&gt;互联网&lt;/a&gt;及&lt;a href=&quot;https://zh.wikipedia.org/wiki/局域网&quot;&gt;局域网&lt;/a&gt;中，使用&lt;a href=&quot;https://zh.wikipedia.org/wiki/虛擬終端機&quot;&gt;虚拟终端&lt;/a&gt;的形式，提供双向、以文字字符串为主的&lt;a href=&quot;https://zh.wikipedia.org/wiki/命令列介面&quot;&gt;命令行接口&lt;/a&gt;交互功能。缺点：传统Telnet会话所传输的资料并未&lt;a href=&quot;https://zh.wikipedia.org/wiki/加密&quot;&gt;加密&lt;/a&gt;，账号和密码等敏感资料容易会被窃听，因此很多服务器都会屏蔽Telnet服务，改用更安全的&lt;a href=&quot;https://zh.wikipedia.org/wiki/Secure_Shell&quot;&gt;SSH&lt;/a&gt;。&lt;/p&gt;

&lt;p&gt;相关邮件协议：SMTP，IMAP，POP，MIME；&lt;/p&gt;

&lt;p&gt;NTP,DHCP;&lt;/p&gt;

&lt;p&gt;DNS 域名解析系统，基于UDP；&lt;/p&gt;

&lt;p&gt;SSH:安全外壳协议，基于TCP，使用22号端口。&lt;/p&gt;

&lt;h2 id=&quot;62-传输层&quot;&gt;6.2 传输层&lt;/h2&gt;

&lt;p&gt;tcp,udp;&lt;/p&gt;

&lt;h2 id=&quot;63-网络层&quot;&gt;6.3 网络层&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;ICMP&lt;/p&gt;

    &lt;p&gt;Internet Control Message Protocol 因特网控制报文协议。它是&lt;a href=&quot;https://baike.baidu.com/item/TCP%2FIP协议簇&quot;&gt;TCP/IP协议簇&lt;/a&gt;的一个子协议，用于在IP&lt;a href=&quot;https://baike.baidu.com/item/主机/455151&quot;&gt;主机&lt;/a&gt;、&lt;a href=&quot;https://baike.baidu.com/item/路由&quot;&gt;路由&lt;/a&gt;器之间传递控制消息。控制消息是指&lt;a href=&quot;https://baike.baidu.com/item/网络通&quot;&gt;网络通&lt;/a&gt;不通、&lt;a href=&quot;https://baike.baidu.com/item/主机/455151&quot;&gt;主机&lt;/a&gt;是否可达、&lt;a href=&quot;https://baike.baidu.com/item/路由/363497&quot;&gt;路由&lt;/a&gt;是否可用等网络本身的消息。这些控制消息虽然并不传输用户数据，但是对于用户数据的传递起着重要的作用。ICMP实际上是IP的一个组成部分，必须由每个IP模块实现。ping 和 tracert是两个常用网络管理命令，ping 用来测试网络可达性，tracert 用来显示到达目的主机的路径。ping和 tracert 都利用 ICMP 协议来实现网络功能，它们是把网络协议应用到日常网络管理的典型实例；&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;IP ：Internet 协议。通过路由选择将下一条IP封装后交给接口层。IP数据报是无连接服务。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;ARP：地址解析协议，主要根据ip地址找到对应的MAC硬件地址；&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;RARP：作用与ARP相反；&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;IGMP：网路群组管理协议。&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;64-数据链路层&quot;&gt;6.4 数据链路层&lt;/h2&gt;

&lt;p&gt;WI-FI(IEEE 802.11)；PPP(Point to Point Protocol)为点对点连接上传输多协议数据包提供了一种标准方法。&lt;/p&gt;

&lt;h1 id=&quot;7-ping-过程&quot;&gt;7 ping 过程&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;主机广播ARP请求，查询被ping主机的mac地址；&lt;/li&gt;
  &lt;li&gt;被ping主机应答ARP请求，向发起ping请求的主机单向应答自己的MAC地址；&lt;/li&gt;
  &lt;li&gt;知道了MAC地址后，开始真正地进行ping请求；&lt;/li&gt;
  &lt;li&gt;两台电脑由此开始相互应答。&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;关于ARP缓存&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
  &lt;li&gt;为了防止ARP过期，结束后重新更新下ARP缓存，保证下次请求能去往正确的路径(ARP过期会出现一次错误，从而影响测试准确性)。&lt;/li&gt;
  &lt;li&gt;ping命令的响应时间是根据请求包和响应包的时间戳计算出来的，一次ARP过程也是消耗时间的 。提前更新ARP缓存可以节省ping的ARP时间。&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;8-关于重定向报文&quot;&gt;8 关于重定向报文&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;由路由器产生；&lt;/li&gt;
  &lt;li&gt;为主机而不是路由器使用。&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;9-tcp中的异常情况断连&quot;&gt;9 TCP中的异常情况(断连)&lt;/h1&gt;

&lt;p&gt;&lt;img src=&quot;/images/blog/2021-08-16_095915.png&quot; alt=&quot;2021-08-16_095915.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/blog/image-20210816100720402.png&quot; alt=&quot;image-20210816100720402&quot; /&gt;&lt;/p&gt;

&lt;h1 id=&quot;10-tcp中的四个定时器&quot;&gt;10 tcp中的四个定时器&lt;/h1&gt;

&lt;p&gt;&lt;img src=&quot;/images/blog/image-20210816101941934.png&quot; alt=&quot;image-20210816101941934&quot; /&gt;&lt;/p&gt;

&lt;h1 id=&quot;tcpip设计中的重要理念&quot;&gt;tcp/ip设计中的重要理念&lt;/h1&gt;

&lt;h2 id=&quot;端到端&quot;&gt;端到端&lt;/h2&gt;

&lt;p&gt;重要的功能（差错控制，加密，交付确认）不会在大型系统的底层实现，底层功能不应以完美为目标。&lt;/p&gt;

&lt;h2 id=&quot;命运共享&quot;&gt;命运共享&lt;/h2&gt;

&lt;p&gt;必要的状态放在通信端点，以维护一个活动的通信关联（如：虚拟连接）。&lt;/p&gt;</content><author><name>VatalityWang</name></author><category term="学习" /><summary type="html"></summary></entry><entry><title type="html">algorithm summary</title><link href="https://vatalitywang.github.io/2021/03/05/algorithm-summary/" rel="alternate" type="text/html" title="algorithm summary" /><published>2021-03-05T00:00:00+00:00</published><updated>2021-03-05T00:00:00+00:00</updated><id>https://vatalitywang.github.io/2021/03/05/algorithm-summary</id><content type="html" xml:base="https://vatalitywang.github.io/2021/03/05/algorithm-summary/">&lt;head&gt;
    &lt;script src=&quot;https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
    &lt;script type=&quot;text/x-mathjax-config&quot;&gt;
        MathJax.Hub.Config({
            tex2jax: {
            skipTags: ['script', 'noscript', 'style', 'textarea', 'pre'],
            inlineMath: [['$','$']]
            }
        });
    &lt;/script&gt;
&lt;/head&gt;

&lt;h1 id=&quot;1-分治&quot;&gt;1 分治&lt;/h1&gt;

&lt;h2 id=&quot;11-思想定义&quot;&gt;1.1 思想定义&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;原问题可分解为几个规模较小，但类似于原问题的子问题；&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;递归地求解这些子问题；&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;合并子问题的解得到原问题的解。&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;12-举例&quot;&gt;1.2 举例&lt;/h2&gt;

&lt;h3 id=&quot;121--归并排序&quot;&gt;1.2.1  归并排序&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;分解待排序的n个元素为各有n/2个元素的两个子序列。&lt;/li&gt;
  &lt;li&gt;使用归并排序递归地排序两个子序列。&lt;/li&gt;
  &lt;li&gt;合并两个已排序的子序列产生最终的排序。&lt;/li&gt;
&lt;/ul&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;cp&quot;&gt;#include&amp;lt;vector&amp;gt;
#include&amp;lt;iostream&amp;gt;
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt;  &lt;span class=&quot;nf&quot;&gt;merge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&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;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;temp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;temp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;clear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
            &lt;span class=&quot;kt&quot;&gt;int&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;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&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;n&quot;&gt;q&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;k&quot;&gt;while&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&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;n&quot;&gt;r&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;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&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;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;temp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;push_back&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&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;k&quot;&gt;else&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;temp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;push_back&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&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;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;q&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;n&quot;&gt;temp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;push_back&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&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;k&quot;&gt;while&lt;/span&gt;&lt;span class=&quot;p&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;n&quot;&gt;q&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;n&quot;&gt;temp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;push_back&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&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;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&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;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;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;temp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&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;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;temp&lt;/span&gt;&lt;span class=&quot;p&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;n&quot;&gt;temp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;clear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;temp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shrink_to_fit&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;return&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;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;mergesort&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;low&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;high&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;n&quot;&gt;low&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;high&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;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mid&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;low&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;high&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;n&quot;&gt;mergesort&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;low&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;mergesort&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mid&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;n&quot;&gt;high&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;merge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;low&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;high&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;h1 id=&quot;2-动态规划&quot;&gt;2 动态规划&lt;/h1&gt;

&lt;h2 id=&quot;21-基本思想&quot;&gt;2.1 基本思想&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;刻画最优解的结构；&lt;/li&gt;
  &lt;li&gt;递归定义最优解的值；&lt;/li&gt;
  &lt;li&gt;计算最优解的值；
    &lt;ul&gt;
      &lt;li&gt;两种方式，自底向上或者自顶向下。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;利用计算出的信息刻画最优解。&lt;/li&gt;
  &lt;li&gt;思考：局部最大不一定是全局最大？&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;套路&quot;&gt;套路&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;定义dp数组及其下标含义；&lt;/li&gt;
  &lt;li&gt;确定递推公式；&lt;/li&gt;
  &lt;li&gt;初始化dp数组；&lt;/li&gt;
  &lt;li&gt;确定dp数组遍历顺序；&lt;/li&gt;
  &lt;li&gt;debug：打印dp数组变化情况。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;22-典型问题&quot;&gt;2.2 典型问题&lt;/h2&gt;

&lt;h3 id=&quot;221-背包问题&quot;&gt;2.2.1 背包问题&lt;/h3&gt;

&lt;h4 id=&quot;2211-0-1背包&quot;&gt;2.2.1.1 0-1背包&lt;/h4&gt;

&lt;h5 id=&quot;条件&quot;&gt;条件&lt;/h5&gt;

&lt;p&gt;V:背包容量（可装物品的总重量）;&lt;/p&gt;

&lt;p&gt;N: 物品个数；&lt;/p&gt;

&lt;p&gt;C[N]: 物品重量；&lt;/p&gt;

&lt;p&gt;V[N]: 一个物品放入背包后所获得的价值；&lt;/p&gt;

&lt;h5 id=&quot;问题&quot;&gt;问题&lt;/h5&gt;

&lt;p&gt;求如何放哪些物品使得获得的总价值最大。&lt;/p&gt;

&lt;h5 id=&quot;转移方程&quot;&gt;转移方程&lt;/h5&gt;

&lt;p&gt;$F[i][j]$:容量为j的背包放入前i个物品可以获得的最大价值为:不放第i个物品时所获得的最大价值加上放第i个物品时所获得的最大价值二者之间的最大值。&lt;/p&gt;

&lt;p&gt;$F[i][j]=max(F[i-1][j],F[i-1][j-C[i]]+V[i])$&lt;/p&gt;

&lt;h3 id=&quot;222-零钱兑换&quot;&gt;2.2.2 零钱兑换&lt;/h3&gt;

&lt;h2 id=&quot;23-一些关键词&quot;&gt;2.3 一些关键词&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;最长&lt;/strong&gt;:  &lt;a href=&quot;https://leetcode-cn.com/problems/longest-increasing-subsequence&quot;&gt;300. 最长递增子序列&lt;/a&gt;；==&lt;a href=&quot;https://leetcode-cn.com/problems/longest-palindromic-substring&quot;&gt;5. 最长回文子串&lt;/a&gt;==；&lt;a href=&quot;https://leetcode-cn.com/problems/longest-palindromic-subsequence&quot;&gt;516. 最长回文子序列&lt;/a&gt;；&lt;a href=&quot;https://leetcode-cn.com/problems/longest-common-subsequence&quot;&gt;1143. 最长公共子序列&lt;/a&gt;；&lt;a href=&quot;https://leetcode.cn/problems/maximum-length-of-repeated-subarray/&quot;&gt;718. 最长重复子数组&lt;/a&gt;；&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;最大：&lt;/strong&gt;&lt;a href=&quot;https://leetcode-cn.com/problems/lian-xu-zi-shu-zu-de-zui-da-he-lcof&quot;&gt;剑指 Offer 42. 连续子数组的最大和&lt;/a&gt;；&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;最小&lt;/strong&gt;：&lt;a href=&quot;https://leetcode-cn.com/problems/triangle&quot;&gt;120. 三角形最小路径和&lt;/a&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;p&gt;&lt;strong&gt;子串&lt;/strong&gt;：&lt;a href=&quot;https://leetcode-cn.com/problems/palindromic-substrings/&quot;&gt;647. 回文子串&lt;/a&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;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;countSubstrings_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&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;kt&quot;&gt;int&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;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&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;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;//dp[i][j]: s[i:j]是否为回文串&lt;/span&gt;
        
        &lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;false&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;n&quot;&gt;i&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;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&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;n&quot;&gt;dp&lt;/span&gt;&lt;span class=&quot;p&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;n&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;nb&quot;&gt;true&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;for&lt;/span&gt;&lt;span class=&quot;p&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;n&quot;&gt;n&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;n&quot;&gt;i&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;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;c1&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;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&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;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&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;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&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;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]){&lt;/span&gt;
                    &lt;span class=&quot;c1&quot;&gt;// 相差不超过1，子串长度为1或者2&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;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;=&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;n&quot;&gt;dp&lt;/span&gt;&lt;span class=&quot;p&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;n&quot;&gt;j&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;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
                        &lt;span class=&quot;n&quot;&gt;num&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;c1&quot;&gt;// 长度超过2&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;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dp&lt;/span&gt;&lt;span class=&quot;p&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;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;n&quot;&gt;num&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;dp&lt;/span&gt;&lt;span class=&quot;p&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;n&quot;&gt;j&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;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;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;n&quot;&gt;num&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;blockquote&gt;
  &lt;p&gt;遍历顺序的思考：&lt;/p&gt;

  &lt;p&gt;​	dp[i][j] 依赖于dp[i+1][j-1]&lt;/p&gt;

  &lt;p&gt;​	即：i+1-&amp;gt;i  j-1-&amp;gt;j 则遍历顺序为从下往上，从左往右。&lt;/p&gt;

  &lt;p&gt;​    以一个长度为3的字符串为例：&lt;/p&gt;

  &lt;p&gt;&lt;img src=&quot;/images/blog/image-20210827102739277.png&quot; alt=&quot;image-20210827102739277&quot; /&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;24-一维二维&quot;&gt;2.4 一维/二维&lt;/h2&gt;

&lt;p&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;dp[i]
i 表示下标范围0~i。如连续子数组的最大和。
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&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;dp[i][j]
i,j可以表示范围。如最长回文子序列，最长公共子序列。
i,j分别表示，i表示一种变量，j表示另外一种变量。如背包问题中分别表示物品编号范围和背包容量。

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;25-注意事项&quot;&gt;2.5 注意事项&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;dp数组下标及其含义；&lt;/li&gt;
  &lt;li&gt;递推公式；&lt;/li&gt;
  &lt;li&gt;dp数组如何初始化；&lt;/li&gt;
  &lt;li&gt;遍历顺序；&lt;/li&gt;
  &lt;li&gt;调试：打印dp数组。&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;3-贪心&quot;&gt;3 贪心&lt;/h1&gt;

&lt;h2 id=&quot;31-基本思想&quot;&gt;3.1 基本思想&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;找出问题的最优子结构；&lt;/li&gt;
  &lt;li&gt;设计一个递归算法来求解问题；&lt;/li&gt;
  &lt;li&gt;在每个决策点做出当前看来是最优的决策，并且做出当前决策以后只剩下一个待求解子问题。&lt;/li&gt;
  &lt;li&gt;这种启发式策略并不能总是找到最优解，但是对某些问题确实可以求得最优解。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;32-实例&quot;&gt;3.2 实例&lt;/h2&gt;

&lt;p&gt;哈夫曼编码&lt;/p&gt;

&lt;h1 id=&quot;4-图拓扑排序活动安排&quot;&gt;4 图拓扑排序(活动安排)&lt;/h1&gt;

&lt;h2 id=&quot;41-基本思想&quot;&gt;4.1 基本思想&lt;/h2&gt;

&lt;p&gt;图遍历算法的一种，基于图中节点的先后顺序，来遍历节点。没有先后关系的节点可以任意排序。&lt;/p&gt;

&lt;h2 id=&quot;42-求最小生成树&quot;&gt;4.2 求最小生成树&lt;/h2&gt;

&lt;h3 id=&quot;421-prim选顶点&quot;&gt;4.2.1 Prim（选顶点）&lt;/h3&gt;

&lt;p&gt;每一步都会为一棵生长中的树添加一条==边==，该树最开始只有一个&lt;a href=&quot;https://zh.wikipedia.org/wiki/顶点_(图论)&quot;&gt;顶点&lt;/a&gt;，然后会添加V-1条边。每次总是添加生长中的树和树中除该生长的树以外的部分形成的切分具有最小权值的横切边。&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;时间复杂度：&lt;/p&gt;

  &lt;p&gt;邻接矩阵：o($v^2$)&lt;/p&gt;

  &lt;p&gt;邻接表：o(elog(e))&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;422-kruskal-选边&quot;&gt;4.2.2 Kruskal （选边）&lt;/h3&gt;

&lt;p&gt;按照边的==权重==顺序（从小到大）将边加入生成树中，但是若加入该边会与生成树形成环则不加入该边。直到树中含有v-1条边为止。这些边组成的就是该图的最小生成树。&lt;/p&gt;

&lt;h1 id=&quot;5-二叉树遍历查找&quot;&gt;5 二叉树遍历/查找&lt;/h1&gt;

&lt;h2 id=&quot;51-深度优先&quot;&gt;5.1 深度优先&lt;/h2&gt;

&lt;h3 id=&quot;511-递归&quot;&gt;5.1.1 递归&lt;/h3&gt;

&lt;h3 id=&quot;512-非递归&quot;&gt;5.1.2 非递归&lt;/h3&gt;

&lt;h2 id=&quot;52-广度优先&quot;&gt;5.2 广度优先&lt;/h2&gt;

&lt;h3 id=&quot;521-递归&quot;&gt;5.2.1 递归&lt;/h3&gt;

&lt;h3 id=&quot;522-非递归&quot;&gt;5.2.2 非递归&lt;/h3&gt;

&lt;h1 id=&quot;6-二分&quot;&gt;6 二分&lt;/h1&gt;

&lt;h2 id=&quot;基本思想&quot;&gt;基本思想&lt;/h2&gt;

&lt;p&gt;利用排序，减少查找次数，每次缩小整个元素一半的查找氛围，时间复杂度：o(n)—&amp;gt;log(n)；&lt;/p&gt;

&lt;h2 id=&quot;61-元素全部有序&quot;&gt;6.1 元素全部有序&lt;/h2&gt;

&lt;h2 id=&quot;62-元素部分有序&quot;&gt;6.2 元素部分有序&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://leetcode-cn.com/problems/valid-triangle-number&quot;&gt;611. 有效三角形的个数&lt;/a&gt;&lt;/p&gt;

&lt;h1 id=&quot;7-排序&quot;&gt;7 排序&lt;/h1&gt;
&lt;h2 id=&quot;71-稳定排序&quot;&gt;7.1 稳定排序&lt;/h2&gt;
&lt;p&gt;插入，冒泡，归并。&lt;/p&gt;
&lt;h2 id=&quot;72-非稳定排序&quot;&gt;7.2 非稳定排序&lt;/h2&gt;
&lt;p&gt;快排，堆排，希尔，直接选择。&lt;/p&gt;

&lt;p&gt;==快排+链表==&lt;/p&gt;

&lt;h1 id=&quot;8-回溯法模板&quot;&gt;8 回溯法模板&lt;/h1&gt;

&lt;h2 id=&quot;基本思想-1&quot;&gt;基本思想&lt;/h2&gt;

&lt;p&gt;暴力式搜索所有可能，遇见满足条件的搜索结果，存入结果集中，最后返回结果集。&lt;/p&gt;

&lt;p&gt;常用实现形式：循环里套递归。&lt;/p&gt;

&lt;p&gt;剪枝：根据所需结果，调整遍历范围，减少循环迭代次数，递归次数，加快算法结果。&lt;/p&gt;

&lt;h2 id=&quot;81-枚举子集&quot;&gt;8.1 枚举子集&lt;/h2&gt;

&lt;p&gt;相当于1个篓子，里面有n个数字，选n次，分别选择0，1，2，3，…，n个数字，求所有不同的选择情况。明显什么都不选有一种情况，每次选1个有n种情况，每次选两个有$c^2_n$种情况。。。&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;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;dfs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cur&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&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;n&quot;&gt;cur&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&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;c1&quot;&gt;// ...&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&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;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;push_back&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cur&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;dfs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cur&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;n&quot;&gt;n&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;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pop_back&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;n&quot;&gt;dfs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cur&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;n&quot;&gt;n&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;h2 id=&quot;81-全排列&quot;&gt;8.1 全排列&lt;/h2&gt;

&lt;p&gt;相当于1个篓子，里面有n个数字，选n次作为一次选择情况，求所有不同的选择情况。&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;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;collection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cur&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;used&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;n&quot;&gt;cur&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&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;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;push_back&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cur&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;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;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&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;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;used&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&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;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;used&lt;/span&gt;&lt;span class=&quot;p&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;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;n&quot;&gt;cur&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;push_back&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nums&lt;/span&gt;&lt;span class=&quot;p&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;c1&quot;&gt;//标记已使用&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;used&lt;/span&gt;&lt;span class=&quot;p&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;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;n&quot;&gt;collection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cur&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;used&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;n&quot;&gt;cur&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pop_back&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;used&lt;/span&gt;&lt;span class=&quot;p&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;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;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;h2 id=&quot;82-电话号码的字母组合&quot;&gt;8.2 电话号码的字母组合&lt;/h2&gt;

&lt;p&gt;相当于n个篓子，每个里面有若个数字，各个数字互不相同，每次只能从篓子里面选一个数字，求能选的数字的所有组合。因为每次只能从一个篓子里面选数字，不存在一次组合中需要从一个篓子里面取两次数字，因此不需要像全排列那样记录已经选过的，防止出现一个数字被选两次的情况。&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;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;num_dfs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;total_str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cur&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;n&quot;&gt;cur&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&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;total_str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()){&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;push_back&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cur&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;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;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;total_str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&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;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;it&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;total_str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]){&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;cur&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;it&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
     
                &lt;span class=&quot;n&quot;&gt;num_dfs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;total_str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&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;n&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cur&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;cur&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pop_back&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;p&quot;&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;83-组合总和&quot;&gt;8.3 组合总和&lt;/h2&gt;

&lt;p&gt;相当于1个篓子，里面有n个数字，任意选取(一个数字可以选择多次，当然可以不选)，求所有使得选择的数字和总和为某一给定目标和的数字选择情况。==是带条件的子集，组合选择问题==。&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;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;add_elment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;candidates&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&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;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;final_res&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;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;candidates&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&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;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;n&quot;&gt;target&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;n&quot;&gt;final_res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;push_back&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;res&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;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;c1&quot;&gt;//不选当前:从后往前回溯，进行本次选择前先选择下一个位置&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;add_elment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;candidates&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&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;n&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;final_res&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;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;n&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;candidates&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&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;n&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;push_back&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;candidates&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;add_elment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;candidates&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&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;n&quot;&gt;candidates&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;final_res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pop_back&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;h2 id=&quot;84-目标和&quot;&gt;8.4 目标和&lt;/h2&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;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sumdfs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&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;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;total&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;n&quot;&gt;index&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;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&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;n&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;o&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;n&quot;&gt;total&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;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;sumdfs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&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;n&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sum&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;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;total&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;n&quot;&gt;sumdfs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&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;n&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sum&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;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;total&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;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;h1 id=&quot;9-tf-idf&quot;&gt;9 TF-IDF&lt;/h1&gt;

&lt;p&gt;TF-IDF(term frequency-inverse document frequency) 词频-逆文档频率，一种统计方法。基于思想：&lt;strong&gt;字词的重要性随着它在文件中出现的次数成正比增加，但同时会随着它在语料库中出现的频率成反比下降。总结就是, 一个词语在一篇文章中出现次数越多, 同时在所有文档中出现次数越少, 越能够代表该文章。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;一个衡量某一个单词对一篇文档重要性的指标。&lt;/p&gt;

&lt;p&gt;$TF_w=\frac{w在文件/文档中出现的次数}{文档总数N}$&lt;/p&gt;

&lt;p&gt;$IDF=log\frac{语料/文件库所有文件总数}{包含词条w的文档数+1}$&lt;/p&gt;

&lt;p&gt;$TF-IDF=TF*IDF$&lt;/p&gt;

&lt;h1 id=&quot;10-字符串如何去重&quot;&gt;10 字符串如何去重&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;关心字符串顺序，直接比较。&lt;/li&gt;
  &lt;li&gt;如果不关心字符串顺序
    &lt;ul&gt;
      &lt;li&gt;用一个26位的数组统计字符串中每个字符出现的次数，再比较即可；&lt;/li&gt;
      &lt;li&gt;将字符串按字典序排序后比较。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;利用set。&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;11-一些思路&quot;&gt;11 一些思路&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;两次遍历
    &lt;ul&gt;
      &lt;li&gt;记录一些跟某个位置元素前后元素相关信息，便于后续操作。&lt;/li&gt;
      &lt;li&gt;相关信息：元素出现次数。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;先排序后遍历
    &lt;ul&gt;
      &lt;li&gt;便于找最大，最小，中位数（最中间两个数值的平均数）。&lt;/li&gt;
      &lt;li&gt;排序后方便利用元素的大小信息，省去不必要的遍历次数。
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;https://leetcode-cn.com/problems/3sum&quot;&gt;15. 三数之和&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;https://leetcode-cn.com/problems/4sum&quot;&gt;18. 四数之和&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;一边遍历一遍统计
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;https://leetcode-cn.com/problems/subarray-sum-equals-k&quot;&gt;560. 和为K的子数组&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://leetcode-cn.com/problems/two-sum&quot;&gt;1. 两数之和&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;12-哈希&quot;&gt;12 哈希&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;建立映射，常数时间定位。
    &lt;ul&gt;
      &lt;li&gt;元素到出现次数&lt;/li&gt;
      &lt;li&gt;元素到元素
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;https://leetcode-cn.com/problems/subarray-sum-equals-k&quot;&gt;560. 和为K的子数组&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;</content><author><name>VatalityWang</name></author><category term="学习" /><summary type="html"></summary></entry><entry><title type="html">常用脚本操作</title><link href="https://vatalitywang.github.io/2021/02/04/%E5%B8%B8%E7%94%A8%E8%84%9A%E6%9C%AC%E6%93%8D%E4%BD%9C/" rel="alternate" type="text/html" title="常用脚本操作" /><published>2021-02-04T00:00:00+00:00</published><updated>2021-02-04T00:00:00+00:00</updated><id>https://vatalitywang.github.io/2021/02/04/%E5%B8%B8%E7%94%A8%E8%84%9A%E6%9C%AC%E6%93%8D%E4%BD%9C</id><content type="html" xml:base="https://vatalitywang.github.io/2021/02/04/%E5%B8%B8%E7%94%A8%E8%84%9A%E6%9C%AC%E6%93%8D%E4%BD%9C/">&lt;head&gt;
    &lt;script src=&quot;https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
    &lt;script type=&quot;text/x-mathjax-config&quot;&gt;
        MathJax.Hub.Config({
            tex2jax: {
            skipTags: ['script', 'noscript', 'style', 'textarea', 'pre'],
            inlineMath: [['$','$']]
            }
        });
    &lt;/script&gt;
&lt;/head&gt;

&lt;h1 id=&quot;1-matlab-常用脚本操作&quot;&gt;1 matlab 常用脚本操作&lt;/h1&gt;

&lt;p&gt;路径拼接：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strcat();&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;全文件夹文件读取：
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fulldir();&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;打开文件并读取内容：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fid=fopen(filename);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;data=fread(fid,'double'): % 第二个参数为数据格式&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fclose(fid);&lt;/code&gt;&lt;/p&gt;</content><author><name>VatalityWang</name></author><category term="学习" /><summary type="html"></summary></entry><entry><title type="html">data structure</title><link href="https://vatalitywang.github.io/2021/01/29/data-stracture/" rel="alternate" type="text/html" title="data structure" /><published>2021-01-29T00:00:00+00:00</published><updated>2021-01-29T00:00:00+00:00</updated><id>https://vatalitywang.github.io/2021/01/29/data-stracture</id><content type="html" xml:base="https://vatalitywang.github.io/2021/01/29/data-stracture/">&lt;head&gt;
    &lt;script src=&quot;https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
    &lt;script type=&quot;text/x-mathjax-config&quot;&gt;
        MathJax.Hub.Config({
            tex2jax: {
            skipTags: ['script', 'noscript', 'style', 'textarea', 'pre'],
            inlineMath: [['$','$']]
            }
        });
    &lt;/script&gt;
&lt;/head&gt;

&lt;h1 id=&quot;1-散列表&quot;&gt;1 散列表&lt;/h1&gt;

&lt;h2 id=&quot;11-定义&quot;&gt;1.1 定义&lt;/h2&gt;

&lt;p&gt;实现字典操作的一种有效数据结构。用一个长度与实际存储的关键字数目成比例的数组来存储元素。不把关键字作为下标，而是根据关键字计算出下标。&lt;/p&gt;

&lt;h2 id=&quot;12-冲突解决&quot;&gt;1.2 冲突解决&lt;/h2&gt;

&lt;h3 id=&quot;121--链接法解决冲突&quot;&gt;1.2.1  链接法解决冲突&lt;/h3&gt;

&lt;p&gt;&lt;img src=&quot;/images/blog/散列表.png&quot; alt=&quot;散列表&quot; /&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;双向链表加快插入和删除，最坏情况下都是O(1);&lt;/li&gt;
  &lt;li&gt;给定一个能存放n个元素的，具有m个槽位的散列表T,装载因子$\alpha$=n/m。&lt;/li&gt;
  &lt;li&gt;简单均匀散列假设下，链接法解决冲突，不成功和成功查找平均时间都为$\theta(1+\alpha)$.&lt;/li&gt;
  &lt;li&gt;如果散列表中槽数和元素个数成正比，则$n=\theta(m)$,$\alpha=n/m=O(m)/m=O(1)$,从而查找操作平均需要常数时间。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;122--散列函数hk&quot;&gt;1.2.2  散列函数h(k)&lt;/h3&gt;

&lt;h4 id=&quot;1221-什么是好的散列函数&quot;&gt;1.2.2.1 什么是好的散列函数&lt;/h4&gt;

&lt;p&gt;近似满足均匀假设，每个关键字被等可能散列到m个槽位中。&lt;/p&gt;

&lt;h4 id=&quot;1222-常用散列函数&quot;&gt;1.2.2.2 常用散列函数&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;除法散列&lt;/p&gt;

    &lt;p&gt;h(k)=k&lt;em&gt;mod&lt;/em&gt;m&lt;/p&gt;

    &lt;p&gt;m一般取不太接近2的整数幂的素数。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;乘法散列&lt;/p&gt;
    &lt;h1 id=&quot;2-b-树&quot;&gt;2 B 树&lt;/h1&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img src=&quot;/images/blog/400px-B-tree.svg.png&quot; alt=&quot;img&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;21-定义&quot;&gt;2.1 定义&lt;/h2&gt;
&lt;p&gt;一棵B树是具有一下性质的有根树(自平衡多叉树)：
每个节点x具有下面属性：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;x.n 节点关键字个数；&lt;/li&gt;
  &lt;li&gt;x.n个关键字以非降序排列；&lt;/li&gt;
  &lt;li&gt;x.leaf bool类型指示是否是叶子节点；&lt;/li&gt;
  &lt;li&gt;每个内部x具有x.n+1个指向其他孩子的指针，x中的关键字对各子树中的关键字范围加以分隔；&lt;/li&gt;
  &lt;li&gt;每个叶节点具有相同的深度，即树的高度h;&lt;/li&gt;
  &lt;li&gt;每个节点包含的关键字个数有上界和下界。用t(t&amp;gt;=2)来表示B树的最小度数(minmum num);&lt;/li&gt;
  &lt;li&gt;除根节点外每个节点必须至少有t-1个关键字；除根节点外每个内部节点至少有t个孩子，如果树非空，则根节点至少有一个关键字；&lt;/li&gt;
  &lt;li&gt;每个节点至多包含2t-1个关键字。因此，一个内部节点至多有2t个孩子。当一个节点恰好有2t-个关键字时，称该节点是满的。
                                                                                                                                                                                                                      ——《算法导论》 p279-280&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;21-高度&quot;&gt;2.1 高度&lt;/h2&gt;

&lt;p&gt;一棵最小度数为t&amp;gt;=2的,含有n(&amp;gt;=1)个关键字的，高度为h的B树满足：&lt;/p&gt;

&lt;p&gt;$h&amp;lt;=log_t \frac{n+1}{2}$&lt;/p&gt;

&lt;h1 id=&quot;3-b-树&quot;&gt;3 B+ 树&lt;/h1&gt;

&lt;p&gt;自平衡多叉树。基本定义与B树相同，不同点是数据只出现在叶子节点上，所有叶子节点间增加了一个链指针方便进行范围查询。&lt;/p&gt;

&lt;p&gt;可视化网址：https://www.cs.usfca.edu/~galles/visualization/BPlusTree.html&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/blog/v2-5f069fd820637db1b877fdd6799a2b67_720w.jpg&quot; alt=&quot;img&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/blog/v2-9644d1a1f83d3e45da779f2e63c35d55_720w.jpg&quot; alt=&quot;img&quot; /&gt;&lt;/p&gt;

&lt;h1 id=&quot;4-b树与b树的区别&quot;&gt;4 B树与B+树的区别&lt;/h1&gt;
&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;B+树的层级更少：相较于B树B+每个非叶子节点存储的关键字数更多，树的层级更少所以查询数据更快；因为B+ 树非叶子节点上是不存储数据的，仅存储键值，而 B 树节点中不仅存储键值，也会存储数据。之所以这么做是因为在数据库中页的大小是固定的，InnoDB 中页的默认大小是 16KB。如果不存储数据，那么就会存储更多的键值，相应的树的阶数(节点的子节点树)就会更大，树就会更矮更胖，如此一来我们查找数据进行磁盘的 IO 次数又会再次减少，数据查询的效率也会更快。另外，B+ 树的阶数是等于键值的数量的，如果我们的 B+ 树一个节点可以存储 1000 个键值，那么 3 层 B+ 树可以存储 1000×1000×1000=10 亿个数据。&lt;/p&gt;

    &lt;p&gt;一般根节点是常驻内存的，所以一般我们查找 10 亿数据，只需要 2 次磁盘 IO。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;B+树查询速度更稳定：B+所有关键字数据地址都存在叶子节点上，所以每次查找的次数都相同所以查询速度要比B树更稳定;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;B+树天然具备排序功能：B+树所有的叶子节点数据构成了一个有序链表，在查询大小区间的数据时候更方便，数据紧密性很高，缓存的命中率也会比B树高。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;B+树全节点遍历更快：B+树遍历整棵树只需要遍历所有的叶子节点即可，，而不需要像B树一样需要对每一层进行遍历，这有利于数据库做全表扫描。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;B树相对于B+树的优点是，如果经常访问的数据离根节点很近，而B树的非叶子节点本身存有关键字其数据的地址，所以这种数据检索的时候会要比B+树快。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;参考：&lt;a href=&quot;https://zhuanlan.zhihu.com/p/27700617&quot;&gt;平衡二叉树、B树、B+树、B*树 理解其中一种你就都明白了&lt;/a&gt;，&lt;a href=&quot;https://segmentfault.com/a/1190000020416577&quot;&gt;[面试官问你B树和B+树，就把这篇文章丢给他]&lt;/a&gt;&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;5-链式前向星&quot;&gt;5 链式前向星&lt;/h1&gt;

&lt;h2 id=&quot;51-存图数据结构&quot;&gt;5.1 存图数据结构&lt;/h2&gt;

&lt;h3 id=&quot;511邻接矩阵&quot;&gt;5.1.1邻接矩阵&lt;/h3&gt;

&lt;p&gt;空间：o(n*n)，n为节点个数。适合于稠密图($n\ast n \approx m$)。 时间：o(n*n)。&lt;/p&gt;

&lt;h3 id=&quot;512-邻接表&quot;&gt;5.1.2 邻接表&lt;/h3&gt;

&lt;p&gt;空间：o(m+n)，适合于边稀疏图。时间：o(n+m)。&lt;/p&gt;

&lt;h3 id=&quot;513-链式前向星&quot;&gt;5.1.3 链式前向星&lt;/h3&gt;

&lt;p&gt;空间：o(n+m)，处于以上二者之间的折中。时间：o(n+m)。&lt;/p&gt;

&lt;h4 id=&quot;5131-数据结构定义&quot;&gt;5.1.3.1 数据结构定义&lt;/h4&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;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Edge&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;to&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;next&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;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;edge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;maxn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;//边集&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;5132-初始化步骤&quot;&gt;5.1.3.2 初始化步骤&lt;/h4&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;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;init&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;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;kt&quot;&gt;int&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;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&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;n&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;p&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;o&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;n&quot;&gt;cnt&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;c1&quot;&gt;//图的边编号起始点&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;add_edge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;//加边，u起点，v终点，w边权&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;edge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cnt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&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;n&quot;&gt;edge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cnt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;w&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w&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;n&quot;&gt;edge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cnt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;next&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;//以u为起点上一条边的编号，也就是与这个边起点相同的上一条边的编号&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u&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;cnt&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;c1&quot;&gt;//更新以u为起点上一条边的编号&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;h4 id=&quot;5133-示例&quot;&gt;5.1.3.3 示例&lt;/h4&gt;

&lt;p&gt;&lt;img src=&quot;/images/blog/image-20210830165913189.png&quot; alt=&quot;image-20210830165913189&quot; /&gt;&lt;/p&gt;

&lt;p&gt;各个成分数组步骤如下：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/blog/image-20210830170034140.png&quot; alt=&quot;image-20210830170034140&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/blog/image-20210830170049377.png&quot; alt=&quot;image-20210830170049377&quot; /&gt;&lt;/p&gt;

&lt;p&gt;参考：&lt;a href=&quot;https://blog.csdn.net/sugarbliss/article/details/86495945&quot;&gt;链式前向星–最通俗易懂的讲解&lt;/a&gt;&lt;/p&gt;

&lt;h1 id=&quot;6-线段树&quot;&gt;6 线段树&lt;/h1&gt;</content><author><name>VatalityWang</name></author><category term="学习" /><summary type="html"></summary></entry><entry><title type="html">specific emitter identification</title><link href="https://vatalitywang.github.io/2021/01/25/specific-emitter-identification/" rel="alternate" type="text/html" title="specific emitter identification" /><published>2021-01-25T00:00:00+00:00</published><updated>2021-01-25T00:00:00+00:00</updated><id>https://vatalitywang.github.io/2021/01/25/specific-emitter-identification</id><content type="html" xml:base="https://vatalitywang.github.io/2021/01/25/specific-emitter-identification/">&lt;head&gt;
    &lt;script src=&quot;https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
    &lt;script type=&quot;text/x-mathjax-config&quot;&gt;
        MathJax.Hub.Config({
            tex2jax: {
            skipTags: ['script', 'noscript', 'style', 'textarea', 'pre'],
            inlineMath: [['$','$']]
            }
        });
    &lt;/script&gt;
&lt;/head&gt;

&lt;h1 id=&quot;1-domain-adaption&quot;&gt;1 Domain adaption&lt;/h1&gt;

&lt;p&gt;迁移学习常见问题之一，域不同但任务相同，且源域数据有标签而目标域数据没有标签或者很少标签。&lt;/p&gt;

&lt;h1 id=&quot;2-traditional-signal-process&quot;&gt;2 traditional signal process&lt;/h1&gt;

&lt;h1 id=&quot;3-few-shot&quot;&gt;3 few shot&lt;/h1&gt;

&lt;h1 id=&quot;4-adversarial&quot;&gt;4 adversarial&lt;/h1&gt;

&lt;h2 id=&quot;41-discriminative-adversarial-networks-for-specific-emitter-identification&quot;&gt;4.1 Discriminative adversarial networks for specific emitter identification&lt;/h2&gt;

&lt;h3 id=&quot;411-核心思想&quot;&gt;4.1.1 核心思想&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;同一类型的不同个体包含的特征有意调制和无意调制两种。&lt;/li&gt;
  &lt;li&gt;有意调制（IMI）是相同的特征，而无意调制(UMI)特征是不同的。&lt;/li&gt;
  &lt;li&gt;无意调制是指纹特征的来源。&lt;/li&gt;
  &lt;li&gt;提出对抗判别模型DAN，学习UMI和IMI之间清晰的决策边界。从而分离出IMI，减少UMI提取过程中的负担。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img src=&quot;/images/blog/image-20210125205437232.png&quot; alt=&quot;image-20210125205437232&quot; /&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;圆：两个辐射源个体的共有特征。&lt;/li&gt;
  &lt;li&gt;三角：两个辐射源个体的独有特征。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;412-整体网络架构&quot;&gt;4.1.2 整体网络架构&lt;/h3&gt;

&lt;p&gt;&lt;img src=&quot;/images/blog/image-20210125205925767.png&quot; alt=&quot;image-20210125205925767&quot; /&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;/li&gt;
&lt;/ul&gt;</content><author><name>VatalityWang</name></author><category term="学习" /><summary type="html"></summary></entry><entry><title type="html">paper reading</title><link href="https://vatalitywang.github.io/2021/01/17/paper-reading/" rel="alternate" type="text/html" title="paper reading" /><published>2021-01-17T00:00:00+00:00</published><updated>2021-01-17T00:00:00+00:00</updated><id>https://vatalitywang.github.io/2021/01/17/paper-reading</id><content type="html" xml:base="https://vatalitywang.github.io/2021/01/17/paper-reading/">&lt;head&gt;
    &lt;script src=&quot;https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
    &lt;script type=&quot;text/x-mathjax-config&quot;&gt;
        MathJax.Hub.Config({
            tex2jax: {
            skipTags: ['script', 'noscript', 'style', 'textarea', 'pre'],
            inlineMath: [['$','$']]
            }
        });
    &lt;/script&gt;
&lt;/head&gt;

&lt;h1 id=&quot;1-metacgan-a-novel-gan-model-for-generating-high-quality-and-diversity-images-with-few-training-data&quot;&gt;1 MetaCGAN A Novel GAN Model for Generating high quality and diversity images with few training data&lt;/h1&gt;

&lt;p&gt;&lt;img src=&quot;/images/blog/image-20210117094956295.png&quot; alt=&quot;image-20210117094956295&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;发表&quot;&gt;发表&lt;/h2&gt;

&lt;p&gt;ICJNN A 会,参考&lt;a href=&quot;https://www.twblogs.net/a/5b7b02482b7177539c24ae9a/?lang=zh-cn&quot;&gt;计算机类顶级会议排名+投稿经验&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;核心思想&quot;&gt;核心思想&lt;/h2&gt;

&lt;p&gt;基于迁移学习，让generator学习到其他类样本的先验信息，迁移到新类上，利用MetaNet生成CGAN反卷积层的权重，以辅助生成质量更高的多样性样本。&lt;/p&gt;

&lt;h2 id=&quot;难点&quot;&gt;难点&lt;/h2&gt;
&lt;p&gt;代码实现。&lt;/p&gt;

&lt;h2 id=&quot;对比&quot;&gt;对比&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;DCGAN 大量数据&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;WGAN  大量数据&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;CGAN  新类少量，旧类大量&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;数据集&quot;&gt;数据集&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;MNIST&lt;/p&gt;

    &lt;p&gt;0,1,2,3,4作为训练基本类,5,6,7,8,9作为新类,每类只包含10张图片.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Fashion MNIST&lt;/p&gt;

    &lt;p&gt;t-shirt  trouser  pullover  dress  coat  作为基本类,sandal  shirt  sneaker  bag  ankle  boot  作为新类.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;CelebA&lt;/p&gt;

    &lt;p&gt;女人作为基本类,男人作为新类,每类50张&lt;/p&gt;

    &lt;h2 id=&quot;训练算法&quot;&gt;训练算法&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img src=&quot;/images/blog/image-20210117145330833.png&quot; alt=&quot;image-20210117145330833&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;tricks&quot;&gt;Tricks&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;使用instance Batch normalization,参考:&lt;a href=&quot;https://www.cnblogs.com/wanghui-garcia/p/10877700.html&quot;&gt;pytorch常用normalization函数&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;评估&quot;&gt;评估&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;定性&lt;/p&gt;

    &lt;p&gt;对比生成图片的质量&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;定量&lt;/p&gt;

    &lt;p&gt;IS(inception score)=$e^{(E_x[D_{KL}(P(y \mid x) \parallel  p(y))])}$&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;x:生成的图像&lt;/li&gt;
      &lt;li&gt;y:训练好的分类器去预测x属于某一个类别的概率所获得的向量&lt;/li&gt;
      &lt;li&gt;p(y):边缘分布&lt;/li&gt;
      &lt;li&gt;$D_{KL}$:kl散度&lt;/li&gt;
      &lt;li&gt;
        &lt;table&gt;
          &lt;tbody&gt;
            &lt;tr&gt;
              &lt;td&gt;p(y&lt;/td&gt;
              &lt;td&gt;x)低意味着决策边界很紧凑,p(y)高意味着图像的高度多样性&lt;/td&gt;
            &lt;/tr&gt;
          &lt;/tbody&gt;
        &lt;/table&gt;
      &lt;/li&gt;
      &lt;li&gt;通常生成图片的多样性可被类之间的决策边界的可区分性所表征.如果两张图片高概率不相似,则应被分为不同的类.&lt;/li&gt;
    &lt;/ul&gt;

    &lt;p&gt;AMT(Amazon Mechanical Turk )&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;评估CelebA上生成的图像.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;2-generative-latent-implicit-conditional-optimization-when-learning-from-small-sample&quot;&gt;2 Generative Latent Implicit Conditional Optimization when Learning from Small Sample&lt;/h1&gt;

&lt;p&gt;&lt;img src=&quot;/images/blog/image-20210117150514932.png&quot; alt=&quot;image-20210117150514932&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;发表-1&quot;&gt;发表&lt;/h2&gt;

&lt;p&gt;ICPR A 会,参考&lt;a href=&quot;https://www.twblogs.net/a/5b7b02482b7177539c24ae9a/?lang=zh-cn&quot;&gt;计算机类顶级会议排名+投稿经验&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;核心思想-1&quot;&gt;核心思想&lt;/h2&gt;

&lt;p&gt;提出GLICO(Generative Latent Implicit Conditional Optimization),学习一个训练空间到潜在空间的隐射,生成器从潜在空间向量中生成图片.不依赖于大量未标记图片,GLICO只需要小的带标签数据点集.实际上,合成新的图片每类只需要5到10张在总共10类样本中,不需要任何先验信息.最后用提出的方法从潜在空间中利用球面插值采样用生成器生成新的图片,生成的图片可以提升分类精度.&lt;/p&gt;

&lt;h2 id=&quot;问题提出&quot;&gt;问题提出&lt;/h2&gt;

&lt;p&gt;1 严格的小样本设置,学习者每一类只能访问到小数量样本(通常5~10),类别数也不多.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;半监督&lt;/p&gt;

    &lt;p&gt;学习者学习到大量无标签数据.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;小样本场景&lt;/p&gt;

    &lt;p&gt;学习者可以接触到大量带标签的各个类别数据,只是这些类别的数据不参与当前分类任务.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;大多数小样本算法依赖于迁移学习,从大量带标签训练样本中,大多半监督学习算法从无标签数据中迁移知识.&lt;/p&gt;

&lt;p&gt;2 大多数针对小数据样本场景设计的方法不能利用外围数据来进行迁移学习,可通过施加强的先验到模型中来解决这个问题,但是在许多场景下由于未知域以及这样的先验知识不可得.&lt;/p&gt;

&lt;p&gt;​	数据增强可能会有用,利用&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;有关数据的弱先验,如半监督学习,传导学习.&lt;/li&gt;
  &lt;li&gt;自训练方法&lt;/li&gt;
  &lt;li&gt;生成模型&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3 GAN为何不能用?&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;需要大量数据来进行有效训练.&lt;/li&gt;
  &lt;li&gt;小样本场景下表现差.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;方法解决&quot;&gt;方法解决&lt;/h2&gt;
&lt;p&gt;​	在潜在空间中进行优化.为每一个数据点分别学习一个潜在空间码.在潜在空间中,让内类样本尽量相互靠拢,类间样本尽量远离.通过一个由已知多标签数据训练的分类器.&lt;/p&gt;

&lt;h2 id=&quot;损失函数&quot;&gt;损失函数&lt;/h2&gt;

&lt;p&gt;$L_{percep} (x_i,z_i;\theta)=\sum_{j} \lambda_j\parallel \xi_j(G_{\theta}([z_i,\varepsilon]))-\xi_j(x_i)  \parallel _1 $&lt;/p&gt;

&lt;h3 id=&quot;感知损失函数&quot;&gt;感知损失函数&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;$z_i\epsilon Z$ ,$Z$:单位球面,在$R^d$ 空间中,表示隐层空间.&lt;/li&gt;
  &lt;li&gt;$x_i \epsilon X \mid  X_i \epsilon R^{3\ast H \ast W}$  ,表示训练集中的样本.&lt;/li&gt;
  &lt;li&gt;$G_\theta$:生成器.&lt;/li&gt;
  &lt;li&gt;$\xi_j(x_i)$ :卷积网络的输出层.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img src=&quot;/images/blog/image-20210117172632460.png&quot; alt=&quot;image-20210117172632460&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;优化目标&quot;&gt;优化目标&lt;/h3&gt;

&lt;p&gt;$min_\theta \sum_{i=1}^{n}[min_{z_i\epsilon Z}L_{percep}(x_i,z_i;\theta)]$&lt;/p&gt;

&lt;h3 id=&quot;分类损失&quot;&gt;分类损失&lt;/h3&gt;

&lt;h1 id=&quot;3-domain-adaptation-through-synthesis-for-unsupervised-person-re-identification&quot;&gt;3 Domain Adaptation through Synthesis for Unsupervised Person Re-identification&lt;/h1&gt;

&lt;h2 id=&quot;发表-2&quot;&gt;发表&lt;/h2&gt;

&lt;p&gt;ECCV 2018&lt;/p&gt;

&lt;h2 id=&quot;核心思想-2&quot;&gt;核心思想&lt;/h2&gt;

&lt;p&gt;提出用基于渲染的合成数据加基于cycle-gan的域迁移来合成新的数据集，用于后续模型微调重识别网络。以实现未知光照条件下的行人重识别。&lt;/p&gt;

&lt;h2 id=&quot;潜在挑战&quot;&gt;潜在挑战&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;训练合成数据时需要克服合成数据分布和真实数据分布之间的差异。网络可能只学习到合成数据的特征却没有学到真实数据的特征。&lt;/li&gt;
  &lt;li&gt;解决：域适应，合成数据看成源域，真实数据看出目标域
    &lt;ul&gt;
      &lt;li&gt;使合成数据更真实。&lt;/li&gt;
      &lt;li&gt;最小化源域和目标域数据之间的域迁移。&lt;/li&gt;
      &lt;li&gt;给出合成数据和域转换，产生在目标域的训练数据，用于微调，以提升性能。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;做法&quot;&gt;做法&lt;/h2&gt;

&lt;h3 id=&quot;域适应&quot;&gt;域适应&lt;/h3&gt;

&lt;p&gt;&lt;img src=&quot;/images/blog/image-20210131095757582.png&quot; alt=&quot;image-20210131095757582&quot; /&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;光照推理&lt;/li&gt;
  &lt;li&gt;域转化&lt;/li&gt;
  &lt;li&gt;微调&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;选择一张目标域中的无标签图片，通过光照推理从合成域中选择最接近目标域光照的图片，然后把图片通过转化使源域（合成域）中的图片更像目标域中的图片，但是仍然保留了已知的身份信息，用生成的图片来微调重识别网络。&lt;/p&gt;

&lt;p&gt;频谱能量分析；&lt;/p&gt;

&lt;p&gt;提取出来的深度特征的名字？&lt;/p&gt;</content><author><name>VatalityWang</name></author><category term="学习" /><summary type="html"></summary></entry><entry><title type="html">集成学习</title><link href="https://vatalitywang.github.io/2021/01/14/%E9%9B%86%E6%88%90%E5%AD%A6%E4%B9%A0/" rel="alternate" type="text/html" title="集成学习" /><published>2021-01-14T00:00:00+00:00</published><updated>2021-01-14T00:00:00+00:00</updated><id>https://vatalitywang.github.io/2021/01/14/%E9%9B%86%E6%88%90%E5%AD%A6%E4%B9%A0</id><content type="html" xml:base="https://vatalitywang.github.io/2021/01/14/%E9%9B%86%E6%88%90%E5%AD%A6%E4%B9%A0/">&lt;head&gt;
    &lt;script src=&quot;https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
    &lt;script type=&quot;text/x-mathjax-config&quot;&gt;
        MathJax.Hub.Config({
            tex2jax: {
            skipTags: ['script', 'noscript', 'style', 'textarea', 'pre'],
            inlineMath: [['$','$']]
            }
        });
    &lt;/script&gt;
&lt;/head&gt;

&lt;h1 id=&quot;1-预备&quot;&gt;1 预备&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;方差&lt;/p&gt;

    &lt;p&gt;方差度量了同样大小的训练集的变动所导致的学习性能的变化，即刻画了数据扰动所造成的影响。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;偏差&lt;/p&gt;

    &lt;p&gt;偏差度量了学习算法的期望预测与真实结果的偏离程度，即刻画了学习算法本身的拟合能力。&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;2-定义&quot;&gt;2 定义&lt;/h1&gt;

&lt;p&gt;通过构建多个学习器来完成学习任务，提升机器学习性能。&lt;/p&gt;

&lt;p&gt;个体学习器应该好而不同。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;有一定准确性&lt;/li&gt;
  &lt;li&gt;各个学习器之间具有差异&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;3-分类&quot;&gt;3 分类&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;bagging 减少方差&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;boosting 减少偏差&lt;/p&gt;

    &lt;p&gt;将弱学习器提升为强学习器。&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;先从初始训练集训练出一个基学习器&lt;/li&gt;
      &lt;li&gt;根据基学习器的表现对样本分布进行调整，使先前基学习器分错的训练样本在后续获得更多的关注。&lt;/li&gt;
      &lt;li&gt;基于调整后的样本分布训练下一个基学习器。&lt;/li&gt;
      &lt;li&gt;重复进行直到基学习器达到事先指定的值T。&lt;/li&gt;
      &lt;li&gt;最终将这T个基学习器进行加权求和得到最终学习器.e&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;stacking 提升预测结果&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;串行集成&lt;/p&gt;

    &lt;p&gt;adaboost,利用基础模型之间的依赖，给错分样本一个较大的权重来提升性能。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;并行集成&lt;/p&gt;

    &lt;p&gt;并行生成基础模型，如随机森林。利用基础模型的独立性，通过平均能够较大地降低误差。&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;</content><author><name>VatalityWang</name></author><category term="学习" /><summary type="html"></summary></entry></feed>