<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:media="http://search.yahoo.com/mrss/"><channel><title><![CDATA[栗子在尝试]]></title><description><![CDATA[♬ ♪ ～(￣▽￣～) (～￣▽￣)～ ♫♬]]></description><link>https://blog.fluere.cloud/</link><image><url>https://blog.fluere.cloud/favicon.png</url><title>栗子在尝试</title><link>https://blog.fluere.cloud/</link></image><generator>Ghost 4.27</generator><lastBuildDate>Wed, 11 Feb 2026 04:38:21 GMT</lastBuildDate><atom:link href="https://blog.fluere.cloud/rss/" rel="self" type="application/rss+xml"/><ttl>60</ttl><item><title><![CDATA[如何理解数据库事务的一致性]]></title><description><![CDATA[<p>&#x603B;&#x800C;&#x8A00;&#x4E4B;&#xFF0C;&#x590D;&#x4E60;&#x7684;&#x65F6;&#x5019;&#x53D1;&#x73B0;&#x4E86;&#x67D0;&#x4E9B;&#x5BF9;&#x4E8B;&#x52A1;&#x4E00;&#x81F4;&#x6027; (Consistency) &#x6BD4;&#x8F83;&#x7384;&#x4E4E;&#x7684;&#x4E2D;&#x6587;&#x89E3;&#x91CA;&#xFF0C;&#x7B80;&#x5355;&#x641C;&#x4E86;&#x4E0B;&#xFF0C;&#x53D1;&#x73B0;&#x5927;&#x90E8;&#x5206;&#x7B54;&#x6848;&#x90FD;&#x662F;&#x90A3;&#x4E48;&#x89E3;</p>]]></description><link>https://blog.fluere.cloud/what-does-consistency-mean-in-database-transactions/</link><guid isPermaLink="false">61812fb5c76a54582562ec4b</guid><category><![CDATA[吐槽 (?)]]></category><category><![CDATA[基础复习]]></category><category><![CDATA[数据库]]></category><dc:creator><![CDATA[栗子℃]]></dc:creator><pubDate>Tue, 02 Nov 2021 14:19:10 GMT</pubDate><content:encoded><![CDATA[<p>&#x603B;&#x800C;&#x8A00;&#x4E4B;&#xFF0C;&#x590D;&#x4E60;&#x7684;&#x65F6;&#x5019;&#x53D1;&#x73B0;&#x4E86;&#x67D0;&#x4E9B;&#x5BF9;&#x4E8B;&#x52A1;&#x4E00;&#x81F4;&#x6027; (Consistency) &#x6BD4;&#x8F83;&#x7384;&#x4E4E;&#x7684;&#x4E2D;&#x6587;&#x89E3;&#x91CA;&#xFF0C;&#x7B80;&#x5355;&#x641C;&#x4E86;&#x4E0B;&#xFF0C;&#x53D1;&#x73B0;&#x5927;&#x90E8;&#x5206;&#x7B54;&#x6848;&#x90FD;&#x662F;&#x90A3;&#x4E48;&#x89E3;&#x91CA;&#x7684;&#x2026;&#x2026;&#x51B3;&#x5B9A;&#x6765;&#x4E2A;&#x901A;&#x4FD7;&#x7248;&#x672C;&#xFF1F;</p><p>&#x7B80;&#x5355;&#x641C;&#x5230;&#x770B;&#x5230;&#x7684;&#x7248;&#x672C;&#x3002;&#x3002;&#x3002;</p><blockquote>&#x7248;&#x672C;1&#xFF1A;&#x4E8B;&#x52A1;&#x6267;&#x884C;&#x524D;&#x540E;&#x6570;&#x636E;&#x5E93;&#x7684;&#x72B6;&#x6001;&#x4FDD;&#x5B58;&#x4E00;&#x81F4;&#x3002;&#xFF08;&#x67D0;&#x516C;&#x4F17;&#x53F7;&#x590D;&#x4E60;&#x8D44;&#x6599;&#xFF09;</blockquote><p>&#x611F;&#x60F3;&#xFF1A; &#xFF1F;&#xFF1F;&#xFF1F;</p><blockquote>&#x7248;&#x672C;2&#xFF1A;&#x51E0;&#x4E2A;&#x5E76;&#x884C;&#x6267;&#x884C;&#x7684;&#x4E8B;&#x52A1;&#xFF0C;&#x5176;&#x6267;&#x884C;&#x7ED3;&#x679C;&#x5FC5;&#x987B;&#x4E0E;&#x6309;&#x67D0;&#x4E00;&#x987A;&#x5E8F;&#x4E32;&#x884C;&#x6267;&#x884C;&#x7684;&#x7ED3;&#x679C;&#x76F8;&#x4E00;&#x81F4;&#x3002;&#xFF08;&#x767E;&#x5EA6;&#x767E;&#x79D1;&#xFF09;</blockquote><p>&#x611F;&#x60F3;&#xFF1A;&#x611F;&#x89C9;&#x8FD9;&#x4E2A;&#x89E3;&#x91CA;&#x66F4;&#x504F;&#x5411;&#x9694;&#x79BB;&#x6027;&#xFF1F;</p><blockquote>&#x7248;&#x672C;3&#xFF1A;&#x4E00;&#x81F4;&#x6027;&#x662F;&#x6307;&#x4E8B;&#x52A1;&#x5FC5;&#x987B;&#x4F7F;&#x6570;&#x636E;&#x5E93;&#x4ECE;&#x4E00;&#x4E2A;&#x4E00;&#x81F4;&#x6027;&#x72B6;&#x6001;&#x53D8;&#x6362;&#x5230;&#x53E6;&#x4E00;&#x4E2A;&#x4E00;&#x81F4;&#x6027;&#x72B6;&#x6001;&#x3002;</blockquote><p>&#x611F;&#x60F3;&#xFF1A;&#x542C;&#x541B;&#x4E00;&#x5E2D;&#x8BDD;&#xFF0C;&#x5982;&#x542C;&#x4E00;&#x5E2D;&#x8BDD;</p><blockquote>&#x7248;&#x672C;4&#xFF1A;&#x6570;&#x636E;&#x5E93;&#x5904;&#x7406;&#x524D;&#x540E;&#x7ED3;&#x679C;&#x5E94;&#x4E0E;&#x5176;&#x6240;&#x62BD;&#x8C61;&#x7684;&#x5BA2;&#x89C2;&#x4E16;&#x754C;&#x4E2D;&#x771F;&#x5B9E;&#x72B6;&#x51B5;&#x4FDD;&#x6301;&#x4E00;&#x81F4;&#x3002;</blockquote><p>&#x611F;&#x60F3;&#xFF1A;&#x597D;&#x50CF;&#x6709;&#x70B9;&#x660E;&#x767D;&#xFF0C;&#x6709;&#x51E0;&#x4E2A;&#x7248;&#x672C;&#x662F;&#x4ECE;&#x8FD9;&#x4E2A;&#x89D2;&#x5EA6;&#x8BF4;&#x7684;&#xFF08;&#x5F88;&#x591A;&#x90FD;&#x662F;&#x94F6;&#x884C;&#x8D26;&#x6237;&#x5B58;&#x53D6;&#x6B3E;&#x7684;&#x4F8B;&#x5B50;&#xFF0C;&#x8F6C;&#x8D26;&#x524D;&#x540E;&#x94B1;&#x5E94;&#x8BE5;&#x5B88;&#x6052;&#xFF0C;&#x4E14;&#x4E0D;&#x80FD;&#x8F6C;&#x8D85;&#x51FA;&#x4F59;&#x989D;&#x7684;&#x94B1;&#x8BA9;&#x4F59;&#x989D;&#x53D8;&#x6210;&#x8D1F;&#x6570;&#x7B49;&#x7B49;&#xFF09;&#x3002;&#x4E00;&#x81F4;&#x6027;&#x6709;&#x65F6;&#x4E5F;&#x88AB;&#x79F0;&#x4E3A;&#x6B63;&#x786E;&#x6027; (Correctness)&#xFF0C;&#x5373;&#x64CD;&#x4F5C;&#x524D;&#x540E;&#xFF0C;&#x6570;&#x636E;&#x5E93;&#x8BB0;&#x5F55;&#x7684;&#x6570;&#x636E;&#x662F;&#x6B63;&#x786E;&#x3001;&#x7B26;&#x5408;&#x671F;&#x671B;&#x7684;&#x3002;</p><p>&#x7D22;&#x6027;&#x8FDE;&#x82F1;&#x6587;&#x7684;&#x7248;&#x672C;&#x4E5F;&#x4E00;&#x8D77;&#x67E5;&#x4E86;&#xFF1A;</p><blockquote><a href="https://en.wikipedia.org/wiki/Consistency_(database_systems)">Consistency</a> ensures that a transaction can only bring the database from one valid state to another, maintaining database <a href="https://en.wikipedia.org/wiki/Invariant_(computer_science)">invariants</a>: any data written to the database must be valid according to all defined rules, including <a href="https://en.wikipedia.org/wiki/Integrity_constraints">constraints</a>, <a href="https://en.wikipedia.org/wiki/Cascading_rollback">cascades</a>, <a href="https://en.wikipedia.org/wiki/Database_trigger">triggers</a>, and any combination thereof. This prevents database corruption by an illegal transaction, but does not guarantee that a transaction is <em>correct</em>. <a href="https://en.wikipedia.org/wiki/Referential_integrity">Referential integrity</a> guarantees the <a href="https://en.wikipedia.org/wiki/Unique_key">primary key</a> &#x2013; <a href="https://en.wikipedia.org/wiki/Foreign_key">foreign key</a> relationship. &#xFF08;Wikipedia&#xFF09;</blockquote><p>&#x4FDD;&#x969C;&#x53EA;&#x4F1A;&#x4ECE;&#x4E00;&#x4E2A;&#x6709;&#x6548;/&#x6B63;&#x786E;&#x7684;&#x72B6;&#x6001;&#xFF0C;&#x8F6C;&#x53D8;&#x6210;&#x53E6;&#x4E00;&#x4E2A;&#x6709;&#x6548;/&#x6B63;&#x786E;&#x7684;&#x72B6;&#x6001;&#x3002;</p><p>&#x90A3;&#x4E48;&#x4EC0;&#x4E48;&#x662F;&#x4E0D;&#x6B63;&#x786E;/&#x65E0;&#x6548;&#x5462;&#xFF1F;&#x5C31;&#x662F;&#x8FDD;&#x53CD;&#x4E86;&#x4EFB;&#x4F55;&#x5DF2;&#x5B9A;&#x4E49;&#x7684;&#x89C4;&#x5219;&#x7684;&#x60C5;&#x51B5;&#xFF0C;&#x5305;&#x62EC;&#x8FDD;&#x53CD;&#x7EA6;&#x675F;&#x6761;&#x4EF6;&#x3001;&#x7EA7;&#x8054;&#x3001;&#x89E6;&#x53D1;&#x5668;&#x7B49;&#x4E0D;&#x5408;&#x6CD5;&#x64CD;&#x4F5C;&#x3002;&#x5982;&#x8BD5;&#x56FE;&#x63D2;&#x5165;&#x91CD;&#x590D;&#x7684;&#x4E3B;&#x952E;&#xFF08;&#x6570;&#x636E;&#x5E93;&#x4E0D;&#x4F1A;&#x8BA9;&#x4F60;&#x8FD9;&#x4E48;&#x505A;&#x7684;&#xFF09;&#xFF0C;&#x8FDD;&#x53CD;&#x5916;&#x952E;&#x7EA6;&#x675F;&#xFF0C;&#x8FDD;&#x53CD;unique&#x7B49;&#x7B49;&#x3002;&#x5728;&#x5E72;&#x4E86;&#x8FD9;&#x79CD;&#x4E8B;&#x4E4B;&#x540E;&#x4F1A;&#x53D1;&#x751F;&#x4EC0;&#x4E48;&#x5462;&#xFF1F;Commit&#x662F;&#x4E0D;&#x4F1A;commit&#x4E86;&#xFF0C;&#x57FA;&#x672C;&#x90FD;&#x662F;&#x88AB;catch&#x3001;&#x62A5;&#x9519;&#x3001;rollback&#x4E86;&#x3002;&#x800C;&#x65E2;&#x7136;&#x975E;&#x6CD5;&#x64CD;&#x4F5C;&#x88AB;&#x56DE;&#x6EDA;&#xFF0C;&#x4E5F;&#x5C31;&#x4E0D;&#x4F1A;&#x8D70;&#x5411;&#x4E00;&#x4E2A;&#x4E0D;&#x6B63;&#x786E;/&#x65E0;&#x6548;&#x7684;&#x72B6;&#x6001;&#xFF0C;&#x800C;&#x662F;&#x56DE;&#x5230;&#x539F;&#x5148;&#x7684;&#x6709;&#x6548;&#x72B6;&#x6001;&#x3002;</p><p>&#x53E6;&#x4E00;&#x4E2A;&#x89D2;&#x5EA6;&#x6765;&#x8BF4;&#xFF0C;&#x53EA;&#x6709;&#x64CD;&#x4F5C;&#x5168;&#x90FD;&#x5408;&#x6CD5;&#xFF0C;&#x7B26;&#x5408;&#x7EA6;&#x675F;&#xFF0C;&#x624D;&#x53EF;&#x80FD;&#x88AB;&#x63D0;&#x4EA4;&#xFF0C;&#x800C;&#x5728;&#x6B64;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x65B0;&#x7684;&#x72B6;&#x6001;&#x4E5F;&#x662F;&#x7B26;&#x5408;&#x7EA6;&#x675F;&#x3001;&#x6709;&#x6548;&#x7684;&#x3002;</p><p>&#x800C;&#x5404;&#x79CD;&#x7EA6;&#x675F;&#x3001;&#x7EA7;&#x8054;&#x3001;&#x89E6;&#x53D1;&#x5668;&#x3001;&#x81EA;&#x5B9A;&#x4E49;&#x7684;&#x5404;&#x79CD;&#x68C0;&#x67E5;&#xFF08;if&#x4E4B;&#x7C7B;&#x7684;&#xFF0C;&#x4E0D;&#x901A;&#x8FC7;&#x5C31;rollback&#x4E4B;&#x7C7B;&#x7684;&#xFF09;&#xFF0C;&#x4E5F;&#x90FD;&#x662F;&#x4E3A;&#x4E86;&#x8BA9;&#x6570;&#x636E;&#x66F4;&#x52A0;&#x201C;&#x6B63;&#x786E;&#x201D;&#xFF0C;&#x4E0E;&#x6570;&#x636E;&#x8981;&#x53CD;&#x6620;&#x7684;&#x4E8B;&#x5B9E;&#x76F8;&#x7B26;&#x5408;&#x3001;&#x4E00;&#x81F4;&#x3002;&#x5927;&#x6982;&#x4E5F;&#x5C31;&#x662F;&#x8FD9;&#x6837;&#x4E86;&#x5427;&#x3002;</p><p></p><p>&#x7EE7;&#x7EED;&#x7684;&#x590D;&#x4E60;&#x2026;&#x2026;</p><h2 id="%E4%BA%8B%E5%8A%A1-transaction">&#x4E8B;&#x52A1; Transaction</h2><p>&#x7ECF;&#x5E38;&#x4EE5;&#x4E0B;&#x9762;&#x8FD9;&#x79CD;&#x5F62;&#x5F0F;&#x5B58;&#x5728;&#x7684;&#xFF0C;&#x6EE1;&#x8DB3;ACID&#x7684;&#x6570;&#x636E;&#x5E93;&#x64CD;&#x4F5C;&#xFF08;&#x4E00;&#x7CFB;&#x5217;&#x64CD;&#x4F5C;&#xFF09;&#x3002;</p><pre><code class="language-SQL">BEGIN TRY 
	BEGIN TRANSACTION 
		-- &#x4E8B;&#x52A1;&#x7684;&#x64CD;&#x4F5C;&#x5199;&#x8FD9;&#x91CC; -- 
	COMMIT TRANSACTION 
END TRY 
BEGIN CATCH 
	IF (@@TRANCOUNT &gt; 0) 
    	-- &#x5904;&#x7406;&#x9519;&#x8BEF;&#x3001;&#x5199;&#x9519;&#x8BEF;&#x65E5;&#x5FD7;&#x7B49;&#x64CD;&#x4F5C; -- 
 		ROLLBACK TRANSACTION 
    END IF 
END CATCH</code></pre><p>&#x53EA;&#x6709;&#x987A;&#x5229;&#x8FD0;&#x884C;&#x8FC7;commit&#x624D;&#x771F;&#x6B63;&#x6539;&#x52A8;&#x4E86;&#x6570;&#x636E;&#x5E93;&#x4FE1;&#x606F;&#xFF0C;rollback&#x7684;&#x8BDD;&#x4E00;&#x5207;&#x8FD8;&#x539F;&#x3002;</p><h2 id="acid">ACID</h2><p>&#x5176;&#x4E2D;AID&#x6CA1;&#x4EC0;&#x4E48;&#x4E89;&#x8BAE;&#xFF0C;&#x5C24;&#x5176;&#x662F;A&#x548C;D&#x3002;C&#x4E0A;&#x9762;&#x89E3;&#x91CA;&#x4E86;&#x3002;</p><ul><li>&#x539F;&#x5B50;&#x6027; (Atomicity)&#xFF1A;&#x540C;&#x4E00;&#x4E2A;&#x4E8B;&#x52A1;&#x91CC;&#x7684;&#x64CD;&#x4F5C;&#x4E0D;&#x53EF;&#x5206;&#x5272;&#xFF0C;&#x8981;&#x4E48;&#x5168;&#x90E8;&#x6267;&#x884C; (commit&#x6389;)&#xFF0C;&#x8981;&#x4E48;&#x5168;&#x90E8;&#x4E0D;&#x6267;&#x884C; (rollback&#x6389;)&#x3002;&#x4E0D;&#x4F1A;&#x51FA;&#x73B0;&#x4E00;&#x534A;&#x6267;&#x884C;&#x4E86;&#xFF0C;&#x4E00;&#x534A;&#x6CA1;&#x6267;&#x884C;&#x7684;&#x60C5;&#x51B5;&#xFF0C;&#x5373;&#x4FBF;&#x6267;&#x884C;&#x5230;&#x534A;&#x622A;&#x65AD;&#x7535;&#x4E86;&#x4E5F;&#x4E0D;&#x4F1A;&#x3002;</li><li>&#x9694;&#x79BB;&#x6027; (Isolation)&#xFF1A;&#x6BCF;&#x4E2A;&#x4E8B;&#x52A1;&#x72EC;&#x7ACB;&#x6267;&#x884C;&#xFF0C;&#x591A;&#x4E2A;&#x5E76;&#x53D1;&#x4E8B;&#x52A1;&#x4E92;&#x4E0D;&#x5E72;&#x6270;&#x3002;</li><li>&#x6301;&#x4E45;&#x6027; (Durability) : &#x4E8B;&#x52A1;&#x63D0;&#x4EA4;&#x540E;&#xFF0C;&#x5BF9;&#x7CFB;&#x7EDF;&#x7684;&#x5F71;&#x54CD;&#x662F;&#x6301;&#x4E45;&#x7684;&#x3002;&#x5DF2;&#x7ECF;&#x6539;&#x52A8;&#x7684;&#x6570;&#x636E;&#x4E0D;&#x4F1A;&#x4E22;&#x5931;&#xFF0C;&#x5373;&#x4FBF;&#x6570;&#x636E;&#x5E93;&#x51FA;&#x73B0;&#x6545;&#x969C;&#x3002;</li></ul><p>&#x9694;&#x79BB;&#x6027;&#x7684;&#x8BDD;&#xFF0C;&#x8FD8;&#x5B58;&#x5728;&#x9694;&#x79BB;&#x7B49;&#x7EA7;&#xFF1A;</p><ul><li>&#x8BFB;&#x672A;&#x63D0;&#x4EA4;&#xFF08;Read Uncommitted&#xFF09;: &#x4E00;&#x4E2A;&#x4E8B;&#x52A1;&#x8BFB;&#x53D6;&#x4E86;&#x53E6;&#x4E00;&#x4E2A;&#x4E8B;&#x52A1;&#x8FD8;&#x672A;&#x63D0;&#x4EA4;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x800C;&#x7B2C;&#x4E8C;&#x4E2A;&#x4E8B;&#x52A1;&#x4E4B;&#x540E;&#x53EF;&#x80FD;&#x56DE;&#x6EDA;&#x4E86;&#x5E76;&#x6CA1;&#x6267;&#x884C;&#x8FD9;&#x4E2A;&#x66F4;&#x65B0;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;<strong>&#x810F;&#x8BFB;</strong>&#x3002;</li><li>&#x8BFB;&#x5DF2;&#x63D0;&#x4EA4;&#xFF08;Read Committed&#xFF09;: &#x53EA;&#x4F1A;&#x8BFB;&#x53D6;&#x5176;&#x4ED6;&#x4E8B;&#x52A1;&#x5DF2;&#x7ECF;&#x63D0;&#x4EA4;&#x7684;&#x6570;&#x636E;&#x3002;&#x4F46;&#x662F;&#x4F9D;&#x7136;&#x53EF;&#x80FD;&#x53D1;&#x751F;&#x201C;&#x4E0D;&#x53EF;&#x91CD;&#x590D;&#x8BFB;&#x201D;&#x3002;&#x4E00;&#x4E2A;&#x4E8B;&#x52A1;&#x8BFB;&#x53D6;&#x67D0;&#x6570;&#x636E;&#x540E;&#xFF0C;&#x53E6;&#x4E00;&#x4E2A;&#x4E8B;&#x52A1;&#x4FEE;&#x6539;&#x4E86;&#x8BE5;&#x6570;&#x636E;&#x3002;&#x90A3;&#x4E48;&#x7B2C;&#x4E00;&#x4E2A;&#x4E8B;&#x52A1;&#x91CC;&#xFF0C;&#x518D;&#x6B21;&#x5C1D;&#x8BD5;&#x8BFB;&#x8FD9;&#x4E2A;&#x6570;&#x636E;&#x65F6;&#x5C31;&#x8BFB;&#x4E0D;&#x5230;&#x539F;&#x6765;&#x7684;&#x503C;&#x4E86;&#x3002;</li><li>&#x53EF;&#x91CD;&#x590D;&#x8BFB; &#xFF08;Repeatable Read&#xFF09;: &#x5728;&#x4E00;&#x4E2A;&#x4E8B;&#x52A1;&#x4E2D;&#xFF0C;&#x65E0;&#x8BBA;&#x8BFB;&#x540C;&#x4E00;&#x4E2A;&#x6570;&#x636E;&#x51E0;&#x6B21; (&#x4E14;&#x65E0;&#x8BBA;&#x4E2D;&#x9014;&#x5176;&#x4ED6;&#x4E8B;&#x52A1;&#x662F;&#x5426;&#x4FEE;&#x6539;&#x4E86;&#x5B83;&#xFF09;&#xFF0C;&#x8BFB;&#x53D6;&#x5230;&#x7684;&#x503C;&#x90FD;&#x4E00;&#x6837;&#x3002;&#x4F46;&#x662F;&#x53EF;&#x80FD;&#x51FA;&#x73B0;<strong>&#x5E7B;&#x8BFB;</strong>&#xFF0C;&#x5982;A&#x4E8B;&#x52A1;&#x67E5;&#x8BE2;id=123&#xFF0C;&#x8FD4;&#x56DE;0&#x884C;&#xFF0C;B&#x4E8B;&#x52A1;&#x63D2;&#x5165;id=123&#xFF0C;A&#x67E5;&#x8BE2;id=123&#xFF0C;&#x4F9D;&#x7136;&#x6CA1;&#x6709;&#x7ED3;&#x679C;&#xFF0C;A&#x63D2;&#x5165;id=123&#x5931;&#x8D25;&#xFF08;A&#x8BA4;&#x4E3A;id=123&#x4E0D;&#x5B58;&#x5728;&#xFF0C;&#x4F46;&#x5374;&#x65E0;&#x6CD5;&#x63D2;&#x5165;&#xFF09;&#x3002;</li><li>&#x53EF;&#x4E32;&#x884C;&#x5316;&#xFF08;Serializable&#xFF09;&#xFF1A;&#x628A;&#x591A;&#x4E2A;&#x5E76;&#x53D1;&#x4E8B;&#x52A1;&#x4E00;&#x4E2A;&#x4E00;&#x4E2A;&#x4E32;&#x884C;&#x6267;&#x884C;&#xFF0C;&#x4E0D;&#x4F1A;&#x6709;&#x4EFB;&#x4F55;&#x4E0A;&#x8FF0;&#x95EE;&#x9898;&#xFF0C;&#x4F46;&#x901F;&#x5EA6;&#x5F88;&#x6162;&#x2026;&#x2026;</li></ul><p></p><p></p><p>Ref&#xFF1A;</p><ul><li><a href="https://www.cnblogs.com/huanghai223/archive/2011/12/12/2284436.html">https://www.cnblogs.com/huanghai223/archive/2011/12/12/2284436.html</a></li><li><a href="https://en.wikipedia.org/wiki/ACID">https://en.wikipedia.org/wiki/ACID</a></li><li><a href="https://baike.baidu.com/item/%E6%95%B0%E6%8D%AE%E5%BA%93%E4%BA%8B%E5%8A%A1/9744607">https://baike.baidu.com/item/&#x6570;&#x636E;&#x5E93;&#x4E8B;&#x52A1;/9744607</a></li><li><a href="https://zhuanlan.zhihu.com/p/174554969">https://zhuanlan.zhihu.com/p/174554969</a></li><li><a href="https://www.zhihu.com/question/31346392">https://www.zhihu.com/question/31346392</a></li><li><a href="https://blog.csdn.net/c0d5r/article/details/110680789">https://blog.csdn.net/c0d5r/article/details/110680789</a></li><li><a href="https://cloud.tencent.com/developer/article/1593481">https://cloud.tencent.com/developer/article/1593481</a></li><li></li></ul><p></p>]]></content:encoded></item><item><title><![CDATA[设计模式 Design Pattern]]></title><description><![CDATA[<p>&#x8BBE;&#x8BA1;&#x6A21;&#x5F0F;&#x662F;&#x6307;&#x524D;&#x4EBA;&#x4ECE;&#x5B9E;&#x8DF5;&#x4E2D;&#x6478;&#x7D22;&#x51FA;&#x3001;&#x5E76;&#x53CD;&#x590D;&#x9A8C;&#x8BC1;&#x8FC7;&#x7684;&#xFF0C;&#x5BF9;&#x5E38;&#x89C1;&#x95EE;&#x9898;&#x7684;&#x7ECF;&#x5178;&#x89E3;&#x51B3;&#x65B9;&#x6848;&#x3002;&#x800C;&#x8FD9;&#x4E9B;&#x89E3;&#x51B3;&#x65B9;&#x6848;&#xFF0C;&#x53EF;&#x4EE5;&#x5E2E;&#x6211;&#x4EEC;</p>]]></description><link>https://blog.fluere.cloud/design-patterns/</link><guid isPermaLink="false">6173d1b3c76a54582562e6e8</guid><category><![CDATA[笔记]]></category><category><![CDATA[备忘单]]></category><dc:creator><![CDATA[栗子℃]]></dc:creator><pubDate>Sat, 30 Oct 2021 14:27:00 GMT</pubDate><content:encoded><![CDATA[<p>&#x8BBE;&#x8BA1;&#x6A21;&#x5F0F;&#x662F;&#x6307;&#x524D;&#x4EBA;&#x4ECE;&#x5B9E;&#x8DF5;&#x4E2D;&#x6478;&#x7D22;&#x51FA;&#x3001;&#x5E76;&#x53CD;&#x590D;&#x9A8C;&#x8BC1;&#x8FC7;&#x7684;&#xFF0C;&#x5BF9;&#x5E38;&#x89C1;&#x95EE;&#x9898;&#x7684;&#x7ECF;&#x5178;&#x89E3;&#x51B3;&#x65B9;&#x6848;&#x3002;&#x800C;&#x8FD9;&#x4E9B;&#x89E3;&#x51B3;&#x65B9;&#x6848;&#xFF0C;&#x53EF;&#x4EE5;&#x5E2E;&#x6211;&#x4EEC;&#x601D;&#x8003;&#x5728;&#x7279;&#x5B9A;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x5982;&#x4F55;&#x7EC4;&#x7EC7;&#x3001;&#x8BBE;&#x8BA1;&#x7C7B;&#x548C;&#x63A5;&#x53E3;&#x6765;&#x8FBE;&#x6210;&#x4E00;&#x5B9A;&#x76EE;&#x7684;&#x3002;</p><h2 id="%E5%88%9B%E5%BB%BA%E5%9E%8B%E6%A8%A1%E5%BC%8F-creational-pattern">&#x521B;&#x5EFA;&#x578B;&#x6A21;&#x5F0F; Creational Pattern</h2><p>&#x5982;&#x4F55;&#x521B;&#x5EFA;&#x5BF9;&#x8C61;&#x3002;&#x5982;&#x9690;&#x85CF;&#x4E00;&#x4E9B;&#x5177;&#x4F53;&#x7684;&#x521B;&#x5EFA;&#x903B;&#x8F91;&#xFF0C;&#x4ECE;&#x800C;&#x63D0;&#x9AD8;&#x4EE3;&#x7801;&#x7684;&#x7075;&#x6D3B;&#x6027;&#x4E0E;&#x53EF;&#x91CD;&#x7528;&#x6027;&#x3002;</p><h3 id="%E5%B7%A5%E5%8E%82-factory">&#x5DE5;&#x5382; Factory</h3><ul><li>&#x4E0D;&#x76F4;&#x63A5;&#x9760;new&#x6765;&#x521B;&#x5EFA;&#x5B9E;&#x4F8B;&#xFF0C;&#x800C;&#x662F;&#x5EFA;&#x9020;&#x4E00;&#x4E2A;&#x5DE5;&#x5382;&#x7C7B;&#xFF0C;&#x518D;&#x901A;&#x8FC7;&#x5DE5;&#x5382;&#x7684;&#x65B9;&#x6CD5;&#x521B;&#x5EFA;&#x201C;&#x4EA7;&#x54C1;&#x201D;&#x7C7B;&#x3002;</li><li>&#x4F7F;&#x7528;&#x8005;&#x4F7F;&#x7528;&#x4EA7;&#x54C1;&#xFF0C;&#x4E0D;&#x9700;&#x8981;&#x77E5;&#x9053;&#x5DE5;&#x5382;&#x5177;&#x4F53;&#x600E;&#x6837;&#x751F;&#x4EA7;&#x3001;&#x4EA7;&#x54C1;&#x7684;&#x5177;&#x4F53;&#x7EC6;&#x8282;&#xFF0C;&#x53EA;&#x9700;&#x8981;&#x77E5;&#x9053;&#x5DE5;&#x5382;&#x53EF;&#x4EE5;&#x751F;&#x4EA7;&#x4EA7;&#x54C1;&#xFF0C;&#x4E14;&#x751F;&#x4EA7;&#x51FA;&#x6765;&#x7684;&#x4EA7;&#x54C1;&#x90FD;&#x5177;&#x6709;&#x81EA;&#x5DF1;&#x60F3;&#x8981;&#x7684;&#x67D0;&#x79CD;&#x7528;&#x9014;&#xFF08;&#x4EA7;&#x54C1;&#x4EEC;&#x9700;&#x8981;&#x6709;&#x7EDF;&#x4E00;&#x63A5;&#x53E3;&#xFF09;&#x5C31;&#x53EF;&#x4EE5;&#x4E86;&#x3002;</li></ul><pre><code class="language-java">public interface Product {
    void doSomething();
    void showName();
}

/*------Product A -----*/
public class ProductA implements Product {
    public String name = &quot;Product A&quot;;
    void doSomething() {
    	System.out.println(&quot;A: do something&quot;);
    }
    
    void showName() {
    	System.out.println(name);
    }
}

/*------Product B -----*/
public class ProductB implements Product {
    public String name = &quot;Product B&quot;;
    void doSomething() {
    	System.out.println(&quot;B: do something&quot;);
    }
    
    void showName() {
    	System.out.println(name);
    }
}


/*-----------Factory------------*/
public class MyFactory {
    public Product getProduct(String productType) {
    	if(productType.equals(&quot;A&quot;) return new ProductA();
        else if(productType.equals(&quot;B&quot;)) return new ProductB();
        else return null;
    }
}

/* ================== OR ================== */

public abstract class MyFactory {
    public void makeUseOfProduct() {
    	Product p = getProduct();
        p.showName();
        p.doSomething();
    }
    
    public abstract Product getProduct();
}

public class FactoryA extends MyFactory {
    @Override
    public Product getProduct() {
    	return new ProductA();
    }
}

public class FactoryB extends MyFactory {
    @Override
    public Product getProduct() {
    	return new ProductB();
    }
}

// and use the subclasses to generate and make use of products</code></pre><h3 id="%E6%8A%BD%E8%B1%A1%E5%B7%A5%E5%8E%82-abstract-factory">&#x62BD;&#x8C61;&#x5DE5;&#x5382; Abstract Factory</h3><ul><li>&#x8BA9;&#x5DE5;&#x5382;&#x4E0D;&#x53EA;&#x80FD;&#x751F;&#x4EA7;&#x4E00;&#x4E2A;&#xFF0C;&#x800C;&#x662F;&#x80FD;&#x751F;&#x4EA7;&#x4E00;&#x5957;&#x4EA7;&#x54C1;&#x3002;</li><li>&#x53EF;&#x4EE5;&#x4FDD;&#x8BC1;&#x4E00;&#x4E2A;&#x5DE5;&#x5382;&#x91CC;&#x751F;&#x4EA7;&#x51FA;&#x6765;&#x7684;&#x4EA7;&#x54C1;&#x90FD;&#x662F;&#x914D;&#x5957;&#x7684;&#xFF08;&#x6BD4;&#x5982;&#x90FD;&#x662F;&#x670D;&#x88C5;&#x5DE5;&#x5382;&#xFF0C;&#x4F46;&#x4E00;&#x4E2A;&#x7537;&#x88C5;&#xFF0C;&#x4E00;&#x4E2A;&#x5973;&#x88C5;&#xFF09;</li><li>&#x4F7F;&#x7528;&#x8005;&#x4E0D;&#x7528;&#x77E5;&#x9053;&#x5DE5;&#x5382;&#x5177;&#x4F53;&#x600E;&#x4E48;&#x751F;&#x4EA7;&#xFF0C;&#x4F46;&#x53EF;&#x4EE5;&#x77E5;&#x9053;&#x4ECE;&#x540C;&#x4E00;&#x4E2A;&#x5DE5;&#x5382;&#x62FF;&#x51FA;&#x7684;&#x4EA7;&#x54C1;&#x662F;&#x914D;&#x5957;&#x7684;&#xFF08;&#x53EA;&#x4F7F;&#x7528;&#x4ECE;&#x7537;&#x88C5;&#x5DE5;&#x5382;&#x751F;&#x4EA7;&#x7684;&#x4EA7;&#x54C1;&#xFF0C;&#x5C31;&#x4E0D;&#x4F1A;&#x51FA;&#x73B0;&#x5973;&#x88C5;&#x5927;&#x4F6C;&#xFF09;</li></ul><pre><code class="language-java">public interface Upper {
    void printName();
    void dress();
}

public interface Lower {
    void printName();
    void dress();
}

public abstract class ClothingFactory {
    public abstract Upper produceUpper();
    public abstract Lower produceLower();
    
    public void dressUp() {
        Upper upper = produceUpper();
        Lower lower = produceLower();
        
        upper.dress();
        lower.dress();
    }
}

public class WomensClothingFactory {
    @Override
    public Upper produceUpper() {
        // produce women&apos;s upper garment
    }
    
    @Override
    public Lower produceLower() {
        // produce women&apos;s lower garment
    }
}

public class MenssClothingFactory {
    @Override
    public Upper produceUpper() {
        // produce men&apos;s upper garment
    }
    
    @Override
    public Lower produceLower() {
        // produce men&apos;s lower garment
    }
}

</code></pre><h3 id="%E5%BB%BA%E9%80%A0%E8%80%85-builder">&#x5EFA;&#x9020;&#x8005; Builder</h3><ul><li>&#x4E00;&#x6B65;&#x4E00;&#x6B65;&#x6784;&#x9020;&#x5BF9;&#x8C61;</li><li>&#x9002;&#x7528;&#x4E8E;&#xFF0C;&#x6709;&#x5F88;&#x591A;&#x6B65;&#x9AA4;&#x6709;&#x901A;&#x7528;&#x6027;&#xFF0C;&#x4F46;&#x5177;&#x4F53;&#x6B65;&#x9AA4;&#x5BB9;&#x6613;&#x6709;&#x6539;&#x53D8;&#xFF0C;&#x6216;&#x8005;&#x6784;&#x9020;&#x5BF9;&#x8C61;&#x65F6;&#x5E76;&#x975E;&#x6BCF;&#x4E2A;&#x6B65;&#x9AA4;&#x90FD;&#x6267;&#x884C;&#x7684;&#x65F6;&#x5019;</li><li>&#x4F8B;&#xFF1A;StringBuilder&#xFF0C;&#x6BCF;&#x4E2A;&#x6B65;&#x9AA4;&#x589E;&#x52A0;&#x6216;&#x53BB;&#x9664;&#x5B57;&#x7B26;&#xFF0C;&#x6700;&#x540E;&#x751F;&#x4EA7;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;</li><li>&#x4F8B;&#xFF1A;&#x6784;&#x5EFA;&#x4E00;&#x4E2A;&#x5957;&#x9910;&#xFF0C;&#x4E00;&#x9053;&#x4E00;&#x9053;&#x70B9;&#x83DC;&#xFF0C;&#x6700;&#x540E;&#x7ED3;&#x8D26;</li><li>&#x4F8B;&#xFF1A;&#x88C5;&#x4FEE;&#x623F;&#x5C4B;&#xFF0C;&#x57CB;&#x7EBF;&#x3001;&#x5237;&#x5899;&#x3001;&#x94FA;&#x5730;&#x677F;&#x3001;&#x5B89;&#x88C5;&#x53A8;&#x623F;&#x3001;&#x5B89;&#x88C5;&#x536B;&#x751F;&#x95F4;&#x3001;&#x6446;&#x653E;&#x5BB6;&#x5177;&#x7B49;&#x7B49;</li><li>&#x6709;&#x65F6;&#x4E5F;&#x53EF;&#x4EE5;&#x5B89;&#x6392;&#x4E00;&#x4E2A;Director&#x8D1F;&#x8D23;&#x6307;&#x6325;&#x4E00;&#x4E2A;&#x6216;&#x591A;&#x4E2A;Builder&#x6309;&#x4E00;&#x5B9A;&#x7A0B;&#x5E8F;&#x6267;&#x884C;&#x5EFA;&#x9020;&#x6B65;&#x9AA4;</li></ul><pre><code class="language-java">public interface Builder {
    PartA buildStepA();
    PartB buildStepB();
    void buildStepC();
    
    void reset();
    Product getResult();
}

public class Director {
    private Builder builder;
    
    public Director(builder) {
        this.builder = builder;
    }
    
    public void changeBuilder(builder) {
        this.builder = builder;
    }
    
    public Product build(String type) {
    	if(type.equals(&quot;A&quot;)) {
            builder.buildStepA();
            builder.buildStepC();
            return builder.getResult();
        } else {
            builder.buildStepB();
            builder.buildStepC();
            return builder.getResult();
        }
    }
}</code></pre><h3 id="%E5%8D%95%E4%BE%8B-singleton">&#x5355;&#x4F8B; Singleton</h3><ul><li>&#x6BCF;&#x4E2A;&#x5355;&#x4F8B;&#x7C7B;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x5B9E;&#x4F8B;</li><li>&#x5168;&#x5C40;&#x8981;&#x7528;&#x8FD9;&#x4E2A;&#x7C7B;&#x65F6;&#xFF0C;&#x8BBF;&#x95EE;&#x7684;&#x90FD;&#x662F;&#x540C;&#x4E00;&#x4E2A;&#x5B9E;&#x4F8B;</li><li>&#x7528;&#x6765;&#x51CF;&#x5C11;&#x4E00;&#x4E2A;&#x88AB;&#x5168;&#x5C40;&#x5171;&#x7528;&#x7684;&#x7C7B;&#x7684;&#x9891;&#x7E41;&#x521B;&#x5EFA;&#x548C;&#x9500;&#x6BC1;</li><li>&#x4F8B;&#xFF1A;&#x5BF9;&#x67D0;&#x4E2A;&#x6570;&#x636E;&#x5E93;&#x7684;&#x8BBF;&#x95EE;</li><li>&#x4F8B;&#xFF1A;&#x4E00;&#x4EFD;&#x5168;&#x5C40;&#x901A;&#x7528;&#x7684;&#x914D;&#x7F6E;&#x6587;&#x4EF6;</li><li>&#x4F8B;&#xFF1A;&#x4E00;&#x4E2A;&#x53EF;&#x4EE5;&#x91CD;&#x7528;&#x7684;pipeline</li><li>&#x6CE8;&#x610F;&#xFF1A;&#x591A;&#x7EBF;&#x7A0B;&#x65F6;&#x8FD8;&#x9700;&#x8981;&#x6CE8;&#x610F;&#x540C;&#x6B65;&#x95EE;&#x9898;&#xFF0C;&#x907F;&#x514D;&#x591A;&#x4E2A;&#x7EBF;&#x7A0B;&#x4EA7;&#x751F;&#x591A;&#x4E2A;&#x5B9E;&#x4F8B;</li><li>&#x6CE8;&#x610F;&#xFF1A;&#x5F88;&#x591A;&#x6D4B;&#x8BD5;&#x6846;&#x67B6;&#x4E0D;&#x652F;&#x6301;&#x5BF9;Singleton&#x7684;&#x5355;&#x5143;&#x6D4B;&#x8BD5;&#xFF08;&#x56E0;&#x4E3A;private constructor&#x8FD8;&#x6709;static&#x7684;getInstance&#xFF09;</li></ul><pre><code class="language-java">public class MySingleton {
    private MySingleton instance;
    
    // &#x9632;&#x6B62;&#x5B83;&#x88AB;&#x4ECE;&#x5916;&#x90E8;&#x521B;&#x5EFA;
    private MySingleton() {}
    
    public static MySingleton getInstance() {
        if(instance==null) {
        	synchronized (MySingleton.class) {
                if(instance==null) {
                    instance = new MySingleton();
                }
            }
        }
        
        return instance;
    }
    
    public void doSomething() {
        // do something
    }
}</code></pre><h3 id="%E5%8E%9F%E5%9E%8B-prototype">&#x539F;&#x578B; Prototype</h3><ul><li>&#x8BF4;&#x767D;&#x4E86;&#x5C31;&#x662F;&#x5141;&#x8BB8;Clone&#x7684;&#x7C7B; &#xFF08;Java&#x91CC;&#x5B9E;&#x73B0;Cloneable&#x63A5;&#x53E3;&#xFF0C;override clone&#x65B9;&#x6CD5;&#xFF09;</li><li>&#x5141;&#x8BB8;&#x4F7F;&#x7528;&#x8005;&#x5728;&#x4E0D;&#x4E86;&#x89E3;&#x7C7B;&#x7684;&#x5168;&#x8C8C;&#xFF08;&#x6CA1;&#x6709;class&#x6587;&#x4EF6;&#xFF09;&#xFF0C;&#x53EA;&#x6709;&#x5BF9;&#x8C61;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x4E5F;&#x80FD;&#x590D;&#x5236;&#x5BF9;&#x8C61;</li></ul><h2 id="%E7%BB%93%E6%9E%84%E5%9E%8B%E6%A8%A1%E5%BC%8F-structural-pattern">&#x7ED3;&#x6784;&#x578B;&#x6A21;&#x5F0F; Structural Pattern </h2><p>&#x5982;&#x4F55;&#x628A;&#x5BF9;&#x8C61;&#x4EEC;&#x7EC4;&#x5408;&#x5230;&#x4E00;&#x8D77;&#xFF0C;&#x8BA9;&#x5B83;&#x4EEC;&#x5728;&#x4E00;&#x4E2A;&#x7CFB;&#x7EDF;&#x91CC;&#x53EF;&#x4EE5;&#x4E92;&#x76F8;&#x914D;&#x5408;&#x7684;&#x5DE5;&#x4F5C;&#x3002;</p><h3 id="%E9%80%82%E9%85%8D%E5%99%A8-adapter">&#x9002;&#x914D;&#x5668; Adapter</h3><ul><li>&#x5982;&#x679C;&#x4E24;&#x4E2A;&#x7C7B;&#x65E0;&#x6CD5;&#x914D;&#x5408;&#xFF0C;&#x6BD4;&#x5982;&#x8F93;&#x5165;&#x8F93;&#x51FA;&#x4E0D;&#x5339;&#x914D;&#xFF0C;&#x90A3;&#x5C31;&#x5728;&#x4E2D;&#x95F4;&#x63D2;&#x4E00;&#x4E2A;&#x9002;&#x914D;&#x5668;</li><li>&#x4F8B;&#xFF1A;&#x7535;&#x6E90;&#x662F;240V&#x7535;&#x538B;&#xFF0C;&#x624B;&#x673A;&#x5145;&#x7535;&#x9700;&#x8981;12V&#xFF0C;&#x63D2;&#x4E00;&#x4E2A;&#x4ECE;240V&#x8F6C;&#x5230;12V&#x7684;&#x9002;&#x914D;&#x5668;</li><li>&#x4F8B;&#xFF1A;&#x7B14;&#x8BB0;&#x672C;&#x6CA1;&#x6709;SD&#x5361;&#x63D2;&#x53E3;&#xFF0C;&#x5F80;USB&#x53E3;&#x63D2;&#x4E00;&#x4E2A;&#x8BFB;&#x5361;&#x5668;&#xFF0C;&#x518D;&#x5F80;&#x8BFB;&#x5361;&#x5668;&#x91CC;&#x63D2;SD&#x5361;</li><li>&#x4F8B;&#xFF1A;A&#x7A0B;&#x5E8F;&#x8F93;&#x5165;XML&#xFF0C;B&#x7A0B;&#x5E8F;&#x8BFB;JSON&#xFF0C;&#x4E2D;&#x95F4;&#x63D2;&#x4E00;&#x4E2A;XML&#x8F6C;JSON&#x7684;&#x9002;&#x914D;&#x5668;</li></ul><h3 id="%E6%A1%A5%E6%8E%A5-bridge">&#x6865;&#x63A5; Bridge</h3><ul><li>&#x5982;&#x679C;&#x4E00;&#x4E2A;&#x7C7B;&#x6709;&#x591A;&#x4E2A;&#x72EC;&#x7ACB;&#x7EF4;&#x5EA6;&#x7684;&#x53D8;&#x5316;&#xFF0C;&#x5219;&#x53EF;&#x4EE5;&#x628A;&#x6BCF;&#x4E2A;&#x7EF4;&#x5EA6;&#x90FD;&#x62C6;&#x6210;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x7C7B;&#x6216;&#x63A5;&#x53E3;</li><li>&#x4F8B;&#xFF1A;&#x56FE;&#x5F62;&#x5305;&#x542B;&#xFF08;&#x7EA2;&#x7684;&#x5706;&#xFF0C;&#x767D;&#x7684;&#x5706;&#xFF0C;&#x84DD;&#x7684;&#x5706;&#xFF0C;&#x7EA2;&#x7684;&#x6B63;&#x65B9;&#x5F62;&#xFF0C;&#x84DD;&#x7684;&#x6B63;&#x65B9;&#x5F62;&#xFF0C;&#x7EA2;&#x7684;&#x4E09;&#x89D2;&#xFF0C;&#x767D;&#x7684;&#x4E09;&#x89D2;&#x2026;&#x2026;&#xFF09;&#xFF0C;&#x53EF;&#x4EE5;&#x89E3;&#x8026;&#x6210;&#xFF1A;&#x56FE;&#x5F62; {&#x5F62;&#x72B6;&#xFF0C;&#x989C;&#x8272;}</li></ul><h3 id="%E7%BB%84%E5%90%88-composite">&#x7EC4;&#x5408; Composite</h3><ul><li>&#x6811;&#x5F62;&#x7ED3;&#x6784;&#x6A21;&#x5F0F;</li><li>&#x5E0C;&#x671B;&#x7528;&#x6237;&#x80FD;&#x591F;&#x5FFD;&#x7565;&#x7EC4;&#x5408;&#x5BF9;&#x8C61;&#x548C;&#x5355;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;&#x4E0D;&#x540C;&#xFF08;&#x7EC4;&#x5408;&#x5BF9;&#x8C61;&#x548C;&#x5355;&#x4E2A;&#x5BF9;&#x8C61;&#x90FD;&#x5B9E;&#x73B0;&#x540C;&#x4E00;&#x4E2A;&#x63A5;&#x53E3;&#xFF0C;&#x7EC4;&#x5408;&#x5BF9;&#x8C61;&#x7684;&#x63A5;&#x53E3;&#x5BF9;&#x7EC4;&#x5408;&#x4E2D;&#x6BCF;&#x4E2A;&#x5355;&#x72EC;&#x5BF9;&#x8C61;&#x8C03;&#x7528;&#x8BE5;&#x63A5;&#x53E3;&#xFF09;</li><li>&#x4F8B;&#xFF1A;&#x5355;&#x4E2A;&#x56FE;&#x5F62;&#x53EF;&#x4EE5;&#x88AB;&#x7ED8;&#x5236;&#xFF0C;&#x7EC4;&#x5408;&#x56FE;&#x5F62;&#x7684;&#x7ED8;&#x5236;&#x5219;&#x662F;&#x628A;&#x7EC4;&#x5408;&#x4E2D;&#x6BCF;&#x4E2A;&#x56FE;&#x5F62;&#x90FD;&#x7ED8;&#x5236;&#x51FA;&#x6765;</li></ul><h3 id="%E8%BF%87%E6%BB%A4%E5%99%A8%E6%A0%87%E5%87%86-filtercriteria">&#x8FC7;&#x6EE4;&#x5668;/&#x6807;&#x51C6; Filter/Criteria</h3><ul><li>&#x5141;&#x8BB8;&#x7528;&#x6237;&#x4EE5;&#x4E0D;&#x540C;&#x6807;&#x51C6;&#x8FC7;&#x6EE4;&#x4E00;&#x7EC4;&#x5BF9;&#x8C61;</li></ul><pre><code class="language-java">public interface Criteria {
    public List&lt;T&gt; meet(List&lt;T&gt; toFilter);
}

public class Criteria1 implements Criteria {
    @Override
    public List&lt;T&gt; meet(List&lt;T&gt; toFilter) {
    	List&lt;T&gt; list = new ArrayList&lt;T&gt;();
        for(T item: toFilter){
            if(item.getAttr1().equals(&quot;value 1&quot;)) {
                list.add(item);
            }
        }
        return list;
    }
}

public class Criteria2 implements Criteria {
    @Override
    public List&lt;T&gt; meet(List&lt;T&gt; toFilter) {
    	List&lt;T&gt; list = new ArrayList&lt;T&gt;();
        for(T item: toFilter){
            if(item.getAttr2().equals(&quot;value 2&quot;)) {
                list.add(item);
            }
        }
        return list;
    }
}


public class AndCriteria implements Criteria {
 
   private Criteria c1;
   private Criteria c2;
 
   public AndCriteria(Criteria c1, Criteria c2) {
      this.c1 = c1;
      this.c2 = c2; 
   }
 
   @Override
   public List&lt;T&gt; meet(List&lt;T&gt; toFilter) {
      List&lt;T&gt; firstCriteriaList = c1.meet(toFilter);     
      return c2.meet(firstCriteriaList);
   }
}


// or &#x5C31;&#x6C42;&#x5E76;&#x96C6;
</code></pre><h3 id="%E8%A3%85%E9%A5%B0%E5%99%A8-decorator">&#x88C5;&#x9970;&#x5668; Decorator</h3><ul><li>&#x5373;&#x5728;&#x67D0;&#x4E2A;&#x7C7B;&#x5916;&#x9762;&#x591A;&#x5305;&#x4E00;&#x5C42;&#xFF0C;&#x8FD9;&#x4E00;&#x5C42;&#x5177;&#x6709;&#x81EA;&#x5DF1;&#x7684;&#x67D0;&#x4E9B;&#x4F5C;&#x7528;&#xFF0C;&#x4E14;&#x5141;&#x8BB8;&#x900F;&#x8FC7;&#x8FD9;&#x4E00;&#x5C42;&#x4F7F;&#x7528;&#x91CC;&#x9762;&#x5305;&#x7684;&#x4E1C;&#x897F;&#x539F;&#x672C;&#x6709;&#x7684;</li><li>&#x4F7F;&#x7528;&#x7EE7;&#x627F;&#x5F88;&#x522B;&#x626D;&#x3001;&#x4E0D;&#x5BB9;&#x6613;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x88C5;&#x9970;&#x5668;</li><li>&#x88C5;&#x9970;&#x7C7B;&#x548C;&#x88AB;&#x88C5;&#x9970;&#x7C7B;&#x53EF;&#x4EE5;&#x72EC;&#x7ACB;&#x53D1;&#x5C55;</li><li>&#x53EF;&#x80FD;&#x9700;&#x8981;&#x5728;&#x88AB;&#x88C5;&#x9970;&#x7684;&#x7C7B;&#x5916;&#x9762;&#x591A;&#x5305;&#x4E00;&#x5C42;Wrapper&#x7C7B;</li><li>&#x4E00;&#x4E9B;&#x8BED;&#x8A00;&#x548C;&#x6846;&#x67B6;&#x53EF;&#x4EE5;&#x5728;&#x7C7B;&#x548C;&#x65B9;&#x6CD5;&#x4E0A;&#x76F4;&#x63A5;@&#x6CE8;&#x91CA;&#x6765;&#x8FDB;&#x884C;&#x88C5;&#x9970;</li><li>(&#x4E5F;&#x53EF;&#x4EE5;&#x7406;&#x89E3;&#x4E3A;&#x5F80;&#x7C7B;&#x91CC;&#x6CE8;&#x5165;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;)</li></ul><h3 id="%E5%A4%96%E8%A7%82-facade">&#x5916;&#x89C2; Facade</h3><ul><li>&#x9690;&#x85CF;&#x590D;&#x6742;&#x7CFB;&#x7EDF;&#x5185;&#x90E8;&#x7684;&#x903B;&#x8F91;&#xFF0C;&#x63D0;&#x4F9B;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x5BF9;&#x5916;&#x63A5;&#x53E3;&#x4F9B;&#x5BA2;&#x6237;&#x7AEF;&#x4F7F;&#x7528;</li><li>&#x5BA2;&#x6237;&#x7AEF;&#x4E0D;&#x4E0E;&#x7CFB;&#x7EDF;&#x8026;&#x5408;&#xFF0C;&#x5916;&#x89C2;&#x7C7B;&#x4E0E;&#x7CFB;&#x7EDF;&#x8026;&#x5408;</li><li>&#x5916;&#x89C2;&#x7C7B;&#x53EF;&#x80FD;&#x63D0;&#x4F9B;&#x7684;&#x529F;&#x80FD;&#x6BD4;&#x7CFB;&#x7EDF;&#x6240;&#x80FD;&#x63D0;&#x4F9B;&#x7684;&#x5168;&#x90E8;&#x8981;&#x5C11;&#x5F88;&#x591A;&#xFF0C;&#x4F46;&#x53EA;&#x8981;&#x63D0;&#x4F9B;&#x53EF;&#x7EED;&#x65AD;&#x6240;&#x9700;&#x5C31;&#x53EF;&#x4EE5;&#x4E86;</li><li>&#x4F8B;&#xFF1A;&#x89C9;&#x5F97;&#x529E;&#x7406;&#x67D0;&#x4E9B;&#x624B;&#x7EED;&#x7684;&#x6D41;&#x7A0B;&#x590D;&#x6742;&#x96BE;&#x61C2;&#xFF0C;&#x627E;&#x4E2D;&#x4ECB;&#xFF08;&#x5916;&#x89C2;&#x7C7B;&#xFF09;&#x6765;&#x5904;&#x7406;</li><li>&#x4F8B;&#xFF1A;&#x516C;&#x53F8;&#x7684;&#x63A5;&#x5F85;&#x4EBA;&#x5458;</li></ul><h3 id="%E4%BA%AB%E5%85%83-flyweight">&#x4EAB;&#x5143; Flyweight</h3><ul><li>&#x4E5F;&#x88AB;&#x79F0;&#x4F5C;&#x7F13;&#x5B58; Cache</li><li>&#x5F53;&#x6709;&#x5927;&#x91CF;&#x5BF9;&#x8C61;&#x6709;&#x76F8;&#x540C;&#x7684;&#x90E8;&#x5206;&#xFF0C;&#x4E14;&#x8FD9;&#x4E9B;&#x90E8;&#x5206;&#x57FA;&#x672C;&#x4E0D;&#x53D8;&#x65F6;&#xFF0C;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x628A;&#x5171;&#x4EAB;&#x7684;&#x90E8;&#x5206;&#x62BD;&#x53D6;&#x51FA;&#x6765;&#x5355;&#x72EC;&#x5B58;&#x4E00;&#x4EFD;&#x6765;&#x8282;&#x7701;&#x5185;&#x5B58;</li><li>&#x5982;&#x679C;&#x6709;&#x76F8;&#x540C;&#x7684;&#x4E1A;&#x52A1;&#x8BF7;&#x6C42;&#xFF0C;&#x76F4;&#x63A5;&#x8FD4;&#x56DE;&#x5185;&#x5B58;&#x4E2D;&#x5DF2;&#x6709;&#x7684;&#xFF0C;&#x907F;&#x514D;&#x91CD;&#x65B0;&#x521B;&#x5EFA;</li><li>&#x76F8;&#x540C;&#x7684;&#x90E8;&#x5206;&#x53EA;&#x5B58;&#x4E00;&#x4EFD;&#xFF0C;&#x4E0D;&#x540C;&#x7684;&#x90E8;&#x5206;&#x5404;&#x5B58;&#x5404;&#x7684;</li><li>&#x4F8B;&#xFF1A;&#x6E38;&#x620F;&#x4E2D;&#x4E00;&#x7FA4;&#x9E1F;&#x5171;&#x4EAB;&#x4E00;&#x4EFD;&#x7ED8;&#x56FE;&#x8D44;&#x6E90;&#xFF0C;&#x5404;&#x5B58;&#x5404;&#x7684;&#x4F4D;&#x7F6E;&#x548C;&#x98DE;&#x884C;&#x901F;&#x5EA6;</li><li>&#x4F8B;&#xFF1A;Java&#x4E2D;&#x7684;String&#xFF0C;&#x6570;&#x636E;&#x5E93;&#x7684;&#x6570;&#x636E;&#x6C60;</li><li>&#x4F8B;&#xFF1A;&#x5728;HashMap&#x6216;&#x5B57;&#x5178;&#x7C7B;&#x7B49;&#x91CC;&#x5BF9;&#x5171;&#x4EAB;&#x8D44;&#x6E90;&#x8FDB;&#x884C;&#x7F13;&#x5B58;&#xFF0C;&#x5982;&#x679C;&#x547D;&#x4E2D;&#xFF0C;&#x76F4;&#x63A5;&#x8FD4;&#x56DE;</li><li>&#x4F8B;&#xFF1A;&#x952E;&#x503C;&#x5BF9;&#x3001;&#x67E5;&#x8BE2;&#x7ED3;&#x679C;&#x7B49;&#x7F13;&#x5B58;&#x8FDB;NoSQL&#x6570;&#x636E;&#x5E93;</li></ul><h3 id="%E4%BB%A3%E7%90%86-proxy">&#x4EE3;&#x7406; Proxy</h3><ul><li>&#x4E3A;&#x4E86;&#x589E;&#x52A0;&#x5BF9;&#x8BBF;&#x95EE;&#x7684;&#x63A7;&#x5236;&#x3001;&#x65B9;&#x4FBF;&#x7BA1;&#x7406;&#x8BBF;&#x95EE;&#x3001;&#x6216;&#x8005;&#x65B9;&#x4FBF;&#x8BBF;&#x95EE;&#xFF0C;&#x5728;&#x8BBF;&#x95EE;&#x8005;&#x548C;&#x88AB;&#x8BBF;&#x95EE;&#x7684;&#x7C7B;&#x4E4B;&#x95F4;&#x589E;&#x52A0;&#x4E00;&#x5C42;&#x4EE3;&#x7406;</li><li>&#x4E0E;&#x9002;&#x914D;&#x5668;&#x4E4B;&#x95F4;&#x7684;&#x5DEE;&#x522B;&#xFF1A;&#x4EE3;&#x7406;&#x4E0D;&#x6539;&#x53D8;&#x88AB;&#x4EE3;&#x7406;&#x7684;&#x63A5;&#x53E3;</li><li>&#x4E0E;&#x88C5;&#x9970;&#x5668;&#x4E4B;&#x95F4;&#x7684;&#x5DEE;&#x522B;&#xFF1A;&#x4EE3;&#x7406;&#x4E0D;&#x589E;&#x52A0;&#x529F;&#x80FD;&#xFF0C;&#x800C;&#x662F;&#x589E;&#x52A0;&#x63A7;&#x5236;</li><li>&#x4F8B;&#xFF1A;&#x8F66;&#x7968;&#x7684;&#x4EE3;&#x552E;&#x70B9;</li><li>&#x4F8B;&#xFF1A;&#x4EE5;&#x94F6;&#x884C;&#x5361;&#x4EE3;&#x7406;&#x94F6;&#x884C;&#x8D26;&#x6237;&#xFF0C;&#x8FDB;&#x884C;&#x6536;&#x4ED8;&#x6B3E;</li></ul><h2 id="%E8%A1%8C%E4%B8%BA%E6%A8%A1%E5%BC%8F-behavioral-pattern">&#x884C;&#x4E3A;&#x6A21;&#x5F0F; Behavioral Pattern</h2><p>&#x5173;&#x4E4E;&#x5BF9;&#x8C61;&#x4E4B;&#x95F4;&#x7684;&#x804C;&#x8D23;&#x5212;&#x5206;&#x4E0E;&#x901A;&#x8BAF;&#x3001;&#x5982;&#x4F55;&#x8BBE;&#x8BA1;&#x4EE5;&#x5B8C;&#x6210;&#x7B97;&#x6CD5;&#x3002;</p><h3 id="%E8%B4%A3%E4%BB%BB%E9%93%BE-chain-of-responsibility">&#x8D23;&#x4EFB;&#x94FE; Chain of Responsibility</h3><ul><li>&#x53C8;&#x79F0;CoR&#x6216;Chain of Command</li><li>&#x5728;&#x4E00;&#x4E32;&#x4E0D;&#x540C;&#x7684;Handlers&#x4F20;&#x9012;Request&#xFF0C;&#x6BCF;&#x4E2A;Handler&#x51B3;&#x5B9A;&#x662F;&#x5426;&#x5904;&#x7406;&#x8FD9;&#x4E2A;&#x8BF7;&#x6C42;&#x3001;&#x4EE5;&#x53CA;&#x662F;&#x5426;&#x7EE7;&#x7EED;&#x628A;&#x5B83;&#x4F20;&#x7ED9;&#x4E0B;&#x4E00;&#x4E2A;Handler</li></ul><p>&#x53EF;&#x80FD;&#x4EE5;&#x4E24;&#x79CD;&#x5E38;&#x89C1;&#x5F62;&#x5F0F;&#x5B58;&#x5728;&#xFF1A;</p><ul><li>&#x4E00;&#x8FDE;&#x4E32;&#x7684;Handler&#x662F;&#x4E00;&#x8FDE;&#x4E32;&#x7684;&#x68C0;&#x6D4B;&#x6216;&#x5904;&#x7406;&#xFF0C;&#x6BCF;&#x4E2A;Handler&#x662F;&#x4E00;&#x4E2A;&#x6B65;&#x9AA4;&#x3002;&#x5982;&#x679C;&#x901A;&#x8FC7;&#x68C0;&#x6D4B;&#x6216;&#x5904;&#x7406;&#x6210;&#x529F;&#xFF0C;&#x5219;&#x4F20;&#x7ED9;&#x4E0B;&#x4E00;&#x4E2A;Handler&#xFF0C;&#x5426;&#x5219;&#x76F4;&#x63A5;&#x8FD4;&#x56DE;&#x5931;&#x8D25;&#x3002;&#xFF08;e.g. &#x7528;&#x6237;&#x8BA4;&#x8BC1;&#xFF1A;&#x68C0;&#x6D4B;ID&#x4E0E;&#x53E3;&#x4EE4;&#x662F;&#x5426;&#x5339;&#x914D;&#xFF0C;&#x662F;&#x5426;&#x547D;&#x4E2D;&#x7F13;&#x5B58;&#xFF0C;IP&#x662F;&#x5426;&#x77ED;&#x65F6;&#x95F4;&#x591A;&#x6B21;&#x8BD5;&#x56FE;&#x8BA4;&#x8BC1;&#x7B49;&#x4E00;&#x8FDE;&#x4E32;&#x5904;&#x7406;&#xFF09;&#xFF08;e.g. &#x62E8;&#x6253;&#x5BA2;&#x670D;&#x70ED;&#x7EBF;-&gt;&#x63D0;&#x793A;&#x6309;&#x952E;&#x54A8;&#x8BE2;&#x8BE6;&#x60C5;-&gt;&#x81EA;&#x52A8;&#x56DE;&#x590D;&#x7B54;&#x6848;, &#x8BE2;&#x95EE;&#x662F;&#x5426;&#x8F6C;&#x4EBA;&#x5DE5;-&gt;&#x4EBA;&#x5DE5;&#x5BA2;&#x670D; -&gt;&#x5BA2;&#x670D;&#x8F6C;&#x5176;&#x4ED6;&#x4E13;&#x4E1A;&#x90E8;&#x95E8;&#x2026;&#x2026;&#xFF09;</li><li>&#x4E00;&#x8FDE;&#x4E32;&#x7684;Handler&#x4E2D;&#xFF0C;&#x8BF7;&#x6C42;&#x4F1A;&#x88AB;&#x7B2C;&#x4E00;&#x4E2A;&#x80FD;&#x5904;&#x7406;&#x5B83;&#x7684;Handler&#x5904;&#x7406;&#xFF0C;&#x5E76;&#x8FD4;&#x56DE;&#x3002;&#x5982;&#x679C;&#x4E00;&#x4E2A;Handler&#x51B3;&#x5B9A;&#x81EA;&#x5DF1;&#x4E0D;&#x80FD;&#x5904;&#x7406;&#xFF0C;&#x5219;&#x4F20;&#x7ED9;&#x4E0B;&#x4E00;&#x4E2A;&#xFF0C;&#x76F4;&#x5230;&#x6CA1;&#x6709;&#x66F4;&#x591A;Handler&#xFF0C;&#x8FD4;&#x56DE;&#x5931;&#x8D25;&#x3002;&#xFF08;e.g. &#x4E00;&#x4E2A;GUI&#x4E0A;&#x7684;&#x70B9;&#x51FB;&#x4E8B;&#x4EF6;&#x88AB;&#x54EA;&#x5C42;Handler&#x6355;&#x83B7;&#x3001;&#x5904;&#x7406;&#xFF09;</li></ul><h3 id="%E5%91%BD%E4%BB%A4-command">&#x547D;&#x4EE4; Command</h3><ul><li>&#x53C8;&#x79F0;Action&#x6216;Transaction</li><li>&#x628A;&#x8BF7;&#x6C42;&#x5C01;&#x88C5;&#x6210;&#x4E00;&#x4E2A;&#x5305;&#x542B;&#x6240;&#x6709;&#x5B8C;&#x6210;&#x8BF7;&#x6C42;&#x6240;&#x9700;&#x5916;&#x90E8;&#x4FE1;&#x606F;&#x7684;&#x7C7B;&#xFF0C;&#x4EE5;&#x4FBF;&#x628A;&#x8BF7;&#x6C42;&#x5F53;&#x53C2;&#x6570;&#x4F20;&#x5165;&#x65B9;&#x6CD5;&#x3001;&#x5EFA;&#x7ACB;&#x8BF7;&#x6C42;&#x961F;&#x5217;&#x3001;&#x5EF6;&#x8FDF;&#x6267;&#x884C;&#x3001;&#x5904;&#x7406;&#x5F02;&#x5E38;&#xFF08;&#x65E0;&#x6CD5;&#x6267;&#x884C;&#x7684;&#x64CD;&#x4F5C;&#xFF09;&#x7B49;&#x7B49;</li><li>&#x4FBF;&#x4E8E;&#x5BF9;&#x884C;&#x4E3A;&#x8FDB;&#x884C;&#x8BB0;&#x5F55;&#x3001;&#x6267;&#x884C;&#x3001;&#x64A4;&#x9500;&#x3001;&#x4E8B;&#x52A1;&#x7B49;&#x5904;&#x7406;</li></ul><h3 id="%E8%A7%A3%E9%87%8A%E5%99%A8-interpreter">&#x89E3;&#x91CA;&#x5668; Interpreter</h3><ul><li>&#x7ED9;&#x5B9A;&#x4E00;&#x79CD;&#x8BED;&#x8A00;&#x548C;&#x5B83;&#x7684;&#x8BED;&#x6CD5;&#xFF0C;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x89E3;&#x91CA;&#x5668;&#x89E3;&#x91CA;&#x8BED;&#x53E5;&#x3002;</li><li>&#x4F8B;&#xFF1A; SQL&#x89E3;&#x6790;&#x3001;&#x7B26;&#x53F7;&#x5904;&#x7406;&#x5F15;&#x64CE;</li><li>&#x5B9E;&#x73B0;&#xFF1A;&#x6784;&#x5EFA;&#x8BED;&#x6CD5;&#x6811;&#xFF0C;&#x5E76;&#x5B9A;&#x4E49;&#x7EC8;&#x7ED3;&#x7B26;&#x4E0E;&#x975E;&#x7EC8;&#x7ED3;&#x7B26;</li></ul><h3 id="%E8%BF%AD%E4%BB%A3%E5%99%A8-iterator">&#x8FED;&#x4EE3;&#x5668; Iterator</h3><ul><li>&#x63D0;&#x4F9B;&#x4E00;&#x79CD;&#x904D;&#x5386;&#x96C6;&#x5408;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x4E14;&#x65E0;&#x5E8F;&#x77E5;&#x9053;&#x8BE5;&#x96C6;&#x5408;&#x7684;&#x5E95;&#x5C42;&#x8868;&#x793A;&#xFF08;&#x4E0D;&#x9700;&#x8981;&#x77E5;&#x9053;&#x5B83;&#x662F;&#x6570;&#x7EC4;&#x3001;&#x5217;&#x8868;&#x3001;&#x6570;&#x8FD8;&#x662F;&#x4EC0;&#x4E48;&#x522B;&#x7684;&#x5F62;&#x5F0F;&#xFF09;</li><li>Iterator&#x63A5;&#x53E3;&#xFF0C;&#x9700;&#x8981;&#x5B9E;&#x73B0;next&#x548C;hasNext&#x65B9;&#x6CD5;</li></ul><h3 id="%E4%B8%AD%E4%BB%8B%E8%80%85-mediator">&#x4E2D;&#x4ECB;&#x8005; Mediator</h3><ul><li>&#x53C8;&#x79F0; Intermediary &#x6216; Controller</li><li>&#x5F53;&#x5BF9;&#x8C61;&#x4E0E;&#x5BF9;&#x8C61;&#x4E4B;&#x95F4;&#x5B58;&#x5728;&#x5927;&#x91CF;&#x5173;&#x8054;&#x65F6;&#xFF08;&#x7F51;&#x72B6;&#x7ED3;&#x6784;&#xFF09;&#xFF0C;&#x628A;&#x5B83;&#x4EEC;&#x7684;&#x5173;&#x8054;&#x8F6C;&#x5165;&#x4E00;&#x4E2A;&#x4E2D;&#x4ECB;&#x91CC;&#xFF0C;&#x89E3;&#x8026;&#x6210;&#x4E00;&#x4E2A;&#x661F;&#x72B6;&#x7ED3;&#x6784;</li><li>&#x7F3A;&#x70B9;&#x662F;&#x4E2D;&#x4ECB;&#x8005;&#x53EF;&#x80FD;&#x53D8;&#x5F97;&#x5F88;&#x590D;&#x6742;</li></ul><h3 id="%E5%A4%87%E5%BF%98%E5%BD%95-memento">&#x5907;&#x5FD8;&#x5F55; Memento</h3><ul><li>&#x53C8;&#x79F0; Snapshot</li><li>&#x5B58;&#x50A8;&#x3001;&#x6062;&#x590D;&#x5BF9;&#x8C61;&#x7684;&#x67D0;&#x4E2A;&#x72B6;&#x6001;&#xFF0C;&#x4E14;&#x65E0;&#x9700;&#x77E5;&#x9053;&#x8BE5;&#x5BF9;&#x8C61;&#x7684;&#x5177;&#x4F53;&#x5B9E;&#x73B0;</li><li>&#x5907;&#x5FD8;&#x5F55;&#x6A21;&#x5F0F;&#x4F7F;&#x7528;&#x4E09;&#x4E2A;&#x7C7B; <em>Memento</em>&#x3001;<em>Originator</em> &#x548C; <em>CareTaker</em></li><li>Memento&#x5305;&#x542B;&#x5BF9;&#x8C61;&#x7684;&#x72B6;&#x6001;/&#x5FEB;&#x7167;&#xFF0C;&#x6BD4;&#x5982;&#x67D0;&#x5B9E;&#x4F8B;&#x5E8F;&#x5217;&#x5316;&#x540E;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x3002;&#x8FD9;&#x4E2A;&#x72B6;&#x6001;&#x3001;&#x5FEB;&#x7167;&#xFF0C;&#x4E0D;&#x53EF;&#x88AB;&#x5176;&#x4ED6;&#x7C7B;&#x76F4;&#x63A5;&#x8BBF;&#x95EE;&#x3002;&#x5176;&#x4ED6;&#x7C7B;&#x53EF;&#x8BBF;&#x95EE;Memento&#x7684;&#x5143;&#x4FE1;&#x606F;&#xFF0C;&#x5982;&#x65F6;&#x95F4;&#x6233;</li><li>Originator&#x8D1F;&#x8D23;&#x521B;&#x5EFA;Memento&#x91CC;&#x5B58;&#x653E;&#x7684;&#x5FEB;&#x7167;&#xFF0C;&#x9700;&#x8981;&#x6709;&#x5BF9;&#x8981;&#x5B58;&#x6863;&#x5BF9;&#x8C61;&#x7684;&#x8BBF;&#x95EE;&#x6743;&#x3002;&#x53EA;&#x6709;Originator&#x6709;&#x5BF9;Memento&#x7684;&#x8BFB;&#x5199;&#x6743;&#x9650;&#xFF0C;&#x53EF;&#x4EE5;&#x4FEE;&#x6539;&#x3001;&#x6062;&#x590D;Memento&#x91CC;&#x5B58;&#x50A8;&#x7684;&#x72B6;&#x6001;</li><li>&#x5B58;&#x653E;Memento&#x7684;&#x7C7B;&#x53EB;Caretaker&#xFF0C;&#x53EA;&#x8D1F;&#x8D23;&#x5B58;&#x653E;&#x3001;&#x8C03;&#x53D6;Memento&#xFF0C;&#x65E0;&#x6743;&#x67E5;&#x770B;&#x91CC;&#x9762;&#x5185;&#x5BB9;</li><li>&#x4F8B;&#xFF1A;&#x7F16;&#x8F91;&#x5668;&#x7684;&#x5386;&#x53F2;&#x8BB0;&#x5F55;&#xFF0C;&#x6BCF;&#x4E2A;&#x64CD;&#x4F5C;&#x90FD;&#x662F;&#x4E00;&#x4E2A;Memento&#xFF0C;&#x7F16;&#x8F91;&#x5668;&#x672C;&#x8EAB;&#x662F;Originator&#xFF0C;&#x5386;&#x53F2;&#x8BB0;&#x5F55;&#x662F;CareTaker&#x3002;&#x6BCF;&#x6B21;&#x64A4;&#x9500;&#x662F;CareTaker&#x62FF;&#x51FA;&#x6808;&#x9876;&#x64CD;&#x4F5C;&#xFF0C;&#x7136;&#x540E;&#x7F16;&#x8F91;&#x5668;&#x518D;&#xFF08;Originator&#xFF09;&#x6062;&#x590D;&#x91CC;&#x9762;&#x5B58;&#x653E;&#x7684;&#x72B6;&#x6001;</li></ul><h3 id="%E8%A7%82%E5%AF%9F%E8%80%85-observer">&#x89C2;&#x5BDF;&#x8005; Observer</h3><ul><li>&#x53C8;&#x79F0; Event-Subscriber&#xFF0C;&#x6216; Listener</li><li>&#x5F53;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x88AB;&#x4FEE;&#x6539;&#x65F6;&#xFF0C;&#x901A;&#x77E5;&#x6240;&#x6709;&#x4F9D;&#x8D56;&#x5B83;&#x7684;&#x5BF9;&#x8C61; &#xFF08;&#x5373;&#x591A;&#x4E2A;&#x5BF9;&#x8C61;&#x89C2;&#x5BDF;&#x7740;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;&#x53D8;&#x5316;&#xFF09;</li><li>&#x4E5F;&#x53EF;&#x4EE5;&#x8BA4;&#x4E3A;&#x662F;Publisher-Subscriber&#x6A21;&#x5F0F;&#x7684;&#x4E00;&#x79CD;&#x3002;&#x6BCF;&#x6B21;&#x5BF9;&#x8C61;&#x66F4;&#x65B0;&#x90FD;&#x5411;&#x6240;&#x6709;&#x8BA2;&#x9605;&#x8005;/&#x89C2;&#x5BDF;&#x8005;&#x63A8;&#x9001;&#x66F4;&#x65B0;&#x6D88;&#x606F;</li></ul><h3 id="%E7%8A%B6%E6%80%81-state">&#x72B6;&#x6001; State</h3><ul><li>&#x6B64;&#x6A21;&#x5F0F;&#x4E2D;&#xFF0C;&#x7C7B;&#x7684;&#x884C;&#x4E3A;&#x662F;&#x57FA;&#x4E8E;&#x5B83;&#x7684;&#x72B6;&#x6001;&#x6539;&#x53D8;&#x7684;</li><li>&#x540C;&#x4E00;&#x4E2A;&#x7C7B;&#x72B6;&#x6001;&#x4E0D;&#x540C;&#x65F6;&#xFF0C;&#x4EFF;&#x4F5B;&#x5B83;&#x53D8;&#x6210;&#x4E86;&#x53E6;&#x4E00;&#x4E2A;&#x7C7B;</li><li>&#x72B6;&#x6001;&#x9700;&#x8981;&#x662F;&#x6709;&#x7A77;&#x7684;&#xFF0C;&#x4E14;&#x5B58;&#x5728;&#x72B6;&#x6001;&#x95F4;&#x7684;&#x8F6C;&#x5316;&#x5173;&#x7CFB;</li><li>&#x4F8B;&#xFF1A;&#x6587;&#x6863; {&#x8349;&#x7A3F;&#xFF0C;&#x5BA1;&#x9605;&#xFF0C;&#x4FEE;&#x8BA2;&#xFF0C;&#x5DF2;&#x53D1;&#x5E03;}</li><li>&#x4F8B;&#xFF1A;&#x4EBA; {&#x72B6;&#x6001;&#x597D;&#xFF0C;&#x72B6;&#x6001;&#x4E00;&#x822C;&#xFF0C;&#x72B6;&#x6001;&#x5DEE;}</li></ul><h3 id="%E7%AD%96%E7%95%A5-strategy">&#x7B56;&#x7565; Strategy</h3><ul><li>&#x4E00;&#x4E2A;&#x7C7B;&#x7684;&#x884C;&#x4E3A;&#x6216;&#x5176;&#x7B97;&#x6CD5;&#x53EF;&#x4EE5;&#x5728;&#x8FD0;&#x884C;&#x65F6;&#x66F4;&#x6539;</li><li>&#x4E00;&#x4E2A;&#x7C7B;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x5F88;&#x591A;&#x79CD;&#x4E0D;&#x540C;&#x65B9;&#x5F0F;&#x53BB;&#x505A;&#x67D0;&#x4EF6;&#x4E8B;&#x3002;&#x4E3A;&#x6BCF;&#x4E2A;&#x65B9;&#x5F0F;&#x5206;&#x522B;&#x521B;&#x5EFA;&#x4E3A;&#x4E00;&#x4E2A;&#x7C7B;&#x6216;&#x51FD;&#x6570;&#xFF0C;&#x5E76;&#x79F0;&#x5B83;&#x4EEC;&#x4E3A;&#x7B56;&#x7565;&#x3002;&#x8FD9;&#x4E2A;&#x7C7B;&#x968F;&#x7740;&#x7B56;&#x7565;&#x7684;&#x6539;&#x53D8;&#x800C;&#x6539;&#x53D8;&#x505A;&#x8FD9;&#x4EF6;&#x4E8B;&#x7684;&#x884C;&#x4E3A;</li><li>&#x4E3B;&#x8981;&#x7528;&#x4E8E;&#x89E3;&#x51B3;if...else&#x592A;&#x591A;&#x7684;&#x95EE;&#x9898;</li><li>&#x4F8B;&#xFF1A;lambda&#x51FD;&#x6570;&#x4F5C;&#x4E3A;&#x7B56;&#x7565;&#x53C2;&#x6570;&#x4F20;&#x5165;&#x65B9;&#x6CD5;</li><li>&#x4F8B;&#xFF1A;&#x7ED9;&#x6BCF;&#x4E2A;&#x7B56;&#x7565;&#x4E00;&#x4E2A;&#x76F8;&#x540C;&#x7684;&#x63A5;&#x53E3;&#xFF0C;&#x8BA9;&#x539F;&#x6765;&#x7684;&#x7C7B;(Context) &#x5B58;&#x4E00;&#x4E2A;&#x7B56;&#x7565;&#x7684;&#x5F15;&#x7528;&#xFF0C;&#x5E76;&#x8C03;&#x7528;&#x7B56;&#x7565;&#x7684;&#x90A3;&#x4E2A;&#x63A5;&#x53E3;</li><li>&#x4F8B;&#xFF1A;&#x8DEF;&#x7EBF;&#x89C4;&#x5212; {&#x516C;&#x4EA4;&#xFF0C;&#x9A7E;&#x8F66;&#xFF0C;&#x6B65;&#x884C;&#xFF0C;&#x9A91;&#x884C;}</li></ul><h3 id="%E6%A8%A1%E6%9D%BF-template">&#x6A21;&#x677F; Template</h3><ul><li>&#x5728;&#x4E00;&#x4E2A;&#x7C7B;&#x91CC;&#x5B9A;&#x4E49;&#x67D0;&#x79CD;&#x7B97;&#x6CD5;&#x7684;&#x9AA8;&#x67B6;&#xFF0C;&#x7136;&#x540E;&#x5728;&#x5B50;&#x7C7B;&#x91CC;&#x5B9E;&#x73B0;&#x9AA8;&#x67B6;&#x7684;&#x5177;&#x4F53;&#x6B65;&#x9AA4;</li><li>&#x628A;&#x7B97;&#x6CD5;&#x5206;&#x5272;&#x6210;&#x82E5;&#x5E72;&#x6B65;&#x9AA4;&#xFF0C;&#x6BCF;&#x4E2A;&#x6B65;&#x9AA4;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;&#xFF0C;&#x7136;&#x540E;&#x5728;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;&#x91CC;&#x6267;&#x884C;&#x6240;&#x6709;&#x7684;&#x6B65;&#x9AA4;</li><li>&#x5C01;&#x88C5;&#x4E0D;&#x53D8;&#x90E8;&#x5206;&#xFF0C;&#x6269;&#x5C55;&#x53EF;&#x53D8;&#x90E8;&#x5206;</li><li>&#x7236;&#x7C7B;&#x63A7;&#x5236;&#x884C;&#x4E3A;&#xFF0C;&#x5B50;&#x7C7B;&#x63A7;&#x5236;&#x5177;&#x4F53;&#x5B9E;&#x73B0;</li></ul><h3 id="%E8%AE%BF%E9%97%AE%E8%80%85-visitor">&#x8BBF;&#x95EE;&#x8005; Visitor</h3><ul><li>&#x5C06;&#x5BF9;&#x8C61;&#x4E0E;&#x5BF9;&#x8C61;&#x4E0A;&#x7684;&#x64CD;&#x4F5C;&#x5206;&#x79BB;</li><li>&#x5982;&#x679C;&#x9700;&#x8981;&#x4F5C;&#x7528;&#x5728;&#x7C7B;&#x4E0A;&#x7684;&#x65B9;&#x6CD5;&#x4E0E;&#x7C7B;&#x4E0D;&#x600E;&#x4E48;&#x76F8;&#x5173;&#xFF0C;&#x4E3A;&#x4E86;&#x9632;&#x6B62;&#x5927;&#x91CF;&#x8FD9;&#x6837;&#x4E0D;&#x76F8;&#x5173;&#x7684;&#x65B9;&#x6CD5;&#x6C61;&#x67D3;&#x8FD9;&#x4E2A;&#x7C7B;&#xFF0C;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x8BBF;&#x95EE;&#x8005;&#x7C7B;&#x5E76;&#x5C06;&#x65B9;&#x6CD5;&#x5199;&#x5165;&#x8BBF;&#x95EE;&#x8005;&#xFF0C;&#x628A;&#x539F;&#x6765;&#x90A3;&#x4E2A;&#x7C7B;&#x5F53;&#x53C2;&#x6570;&#x4F20;&#x5165;&#x3002;</li></ul><pre><code class="language-java">public class DataNode1 implements DataNode {
    public string data;
    public SpecialObject1 specialObject1;
    public void processData(){
        // ...
        // &#x4E0E;&#x8FD9;&#x4E2A;&#x7C7B;&#x5F88;&#x76F8;&#x5173;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x6240;&#x4EE5;&#x4FDD;&#x7559;&#x4E86;
    }
    
    /* double delegation
       &#x8BA9;&#x7C7B;&#x81EA;&#x5DF1;&#x6765;&#x9009;&#x62E9;&#x5F53;&#x6709;Visitor&#x6765;&#x65F6;&#xFF0C;&#x6267;&#x884C;Visitor&#x91CC;&#x54EA;&#x4E2A;&#x65B9;&#x6CD5;&#x6765;&#x5904;&#x7406;&#x81EA;&#x5DF1;
       &#x56E0;&#x4E3A;Visitor&#x91CC;&#x53EF;&#x80FD;&#x6709;&#x591A;&#x4E2A;&#x5904;&#x7406;&#x4E0D;&#x540C;&#x7C7B;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x800C;&#x7528;&#x6237;&#x4E5F;&#x53EF;&#x80FD;&#x4E0D;&#x77E5;&#x9053;&#x9009;&#x54EA;&#x4E2A;&#x597D; */ 
    public void accept(Visitor v) {
    	v.manageDataNode1(this);
    }
}

public class DataNode2 implements DataNode {
    // &#x603B;&#x4E4B;&#x5C31;&#x662F;&#x53E6;&#x4E00;&#x4E2A;&#x7C7B;
    public string data;
    public SpecialObject2 specialObject2;
    public void processData(){
        // ...
    }
    
    // double delegation
    public void accept(Visitor v) {
    	v.manageDataNode2(this);
    }
}

public class Visitor {
    public static String exportDataAsJSON(DataNode d) {
        // &#x6BD4;&#x5982;&#x8FD9;&#x662F;&#x4E00;&#x4E2A;&#x4E0E;DataNode&#x5F88;&#x4E0D;&#x76F8;&#x5173;&#x7684;&#x65B9;&#x6CD5;
        // &#x7528;&#x5F97;&#x4E0D;&#x591A;&#xFF0C;&#x4F46;&#x5076;&#x5C14;&#x9700;&#x8981;
        // &#x4E0D;&#x60F3;&#x8BA9;&#x5B83;&#x6C61;&#x67D3;DataNode
    }
    
    public static void manageDataNode1(DataNode1 d) {
        // &#x4E13;&#x95E8;&#x5904;&#x7406;DataNode1&#x7C7B;&#x7684;&#x65B9;&#x6CD5;
        d.specialObject1.doSomething();
    }
    
    public static void manageDataNode2(DataNode2 d) {
        // &#x4E13;&#x95E8;&#x5904;&#x7406;DataNode2&#x7C7B;&#x7684;&#x65B9;&#x6CD5;
        d.specialObject2.doSomething();
    }
    
    public static void manageDataNode(DataNode d) {
    	d.accept(this); // double delegation
        // &#x7528;&#x6237;&#x4E5F;&#x4E0D;&#x77E5;&#x9053;&#x5E94;&#x8BE5;&#x7528;&#x54EA;&#x4E2A;&#x65B9;&#x6CD5;&#x7684;&#x65F6;&#x5019;
        // &#x8BA9;DataNode&#x81EA;&#x5DF1;&#x51B3;&#x5B9A;&#x7528;Visitor&#x7684;&#x54EA;&#x4E2A;&#x65B9;&#x6CD5;&#x5427;
    }
}</code></pre><p> </p><p></p><p>Ref:</p><ul><li><a href="https://www.runoob.com/design-pattern/design-pattern-intro.html">https://www.runoob.com/design-pattern/design-pattern-intro.html</a></li><li><a href="https://refactoring.guru/design-patterns">https://refactoring.guru/design-patterns</a></li><li></li></ul>]]></content:encoded></item><item><title><![CDATA[设置WSL2]]></title><description><![CDATA[<h1 id="wsl">WSL</h1><h2 id="%E4%BB%80%E4%B9%88%E6%98%AFwsl">&#x4EC0;&#x4E48;&#x662F;WSL</h2><p>WSL&#x662F;Windows Subsystem for Linux&#x7684;&#x7F29;&#x5199;&#x3002;&#x7B80;&#x5355;&#x6765;&#x8BF4;&#xFF0C;&#x5B83;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x5728;Windows&#x4E0A;&#x8DD1;&#x4E00;&#x4E2A;Linux&#xFF08;&#x53EF;&#x4EE5;&#x81EA;&#x5DF1;&#x6311;&#x53D1;&#x884C;&#x7248;&#xFF09;&#xFF0C;&#x8FD9;&#x6837;&#x5C31;&#x53EF;&#x4EE5;&#x65B9;&#x4FBF;</p>]]></description><link>https://blog.fluere.cloud/install-wsl/</link><guid isPermaLink="false">6168fd65c76a54582562e57b</guid><category><![CDATA[笔记]]></category><category><![CDATA[Tech]]></category><dc:creator><![CDATA[栗子℃]]></dc:creator><pubDate>Fri, 15 Oct 2021 11:00:00 GMT</pubDate><content:encoded><![CDATA[<h1 id="wsl">WSL</h1><h2 id="%E4%BB%80%E4%B9%88%E6%98%AFwsl">&#x4EC0;&#x4E48;&#x662F;WSL</h2><p>WSL&#x662F;Windows Subsystem for Linux&#x7684;&#x7F29;&#x5199;&#x3002;&#x7B80;&#x5355;&#x6765;&#x8BF4;&#xFF0C;&#x5B83;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x5728;Windows&#x4E0A;&#x8DD1;&#x4E00;&#x4E2A;Linux&#xFF08;&#x53EF;&#x4EE5;&#x81EA;&#x5DF1;&#x6311;&#x53D1;&#x884C;&#x7248;&#xFF09;&#xFF0C;&#x8FD9;&#x6837;&#x5C31;&#x53EF;&#x4EE5;&#x65B9;&#x4FBF;&#x7684;&#x5728;Windows&#x673A;&#x5B50;&#x4E0A;&#x7528;Linux&#x7CFB;&#x7EDF;&#x7684;&#x8F6F;&#x4EF6;&#x4E0E;&#x5DE5;&#x5177;&#x4E86;&#x3002;&#x6BD4;&#x5982;&#x5728;Windows&#x4E0A;&#xFF0C;&#x5B89;&#x88C5;Docker&#x8981;&#x6C42;WSL2&#x6216;&#x8005;Hyper-V&#x3002;&#x5BB6;&#x5EAD;&#x7248;&#x7684;Windows&#x5C31;&#x6CA1;&#x5F97;&#x9009;&#x5566;&#xFF0C;&#x53EA;&#x80FD;WSL2&#x2026;&#x2026;</p><h2 id="%E5%85%B6%E4%BB%96%E9%80%89%E6%8B%A9%E4%B8%8E%E6%AF%94%E8%BE%83">&#x5176;&#x4ED6;&#x9009;&#x62E9;&#x4E0E;&#x6BD4;&#x8F83;</h2><p>&#x5728;&#x88C5;&#x4E86;Windows&#x7684;&#x673A;&#x5B50;&#x4E0A;&#x4F7F;&#x7528;Linux&#x7684;&#x5176;&#x4ED6;&#x9009;&#x62E9;&#x4E3B;&#x8981;&#x6709;</p><ul><li>VM&#xFF08;&#x865A;&#x62DF;&#x673A;&#xFF09;</li><li>&#x53CC;/&#x591A;&#x7CFB;&#x7EDF;</li></ul><p>&#x5176;&#x4E2D;VM&#x4E3B;&#x6D41;&#x4E0A;&#x662F;&#x4F7F;&#x7528;VMWare&#xFF08;&#x4ED8;&#x8D39;&#xFF09;&#x3001;VirtualBox&#xFF08;&#x514D;&#x8D39;&#xFF09;&#x3001;Hyper-V&#xFF08;Windows&#x6BD4;&#x8F83;&#x8D35;&#x7684;&#x7248;&#x672C;&#x9650;&#x5B9A;&#x2026;&#xFF09;&#x548C;&#x7CFB;&#x7EDF;&#x955C;&#x50CF;&#x5728;Windows&#x91CC;&#x914D;&#x7F6E;&#x3001;&#x542F;&#x52A8;&#x865A;&#x62DF;&#x7CFB;&#x7EDF;&#x3002;&#x786C;&#x4EF6;&#x8DB3;&#x591F;&#x597D;&#x7684;&#x65F6;&#x5019;&#x53EF;&#x4EE5;&#x540C;&#x65F6;&#x542F;&#x52A8;&#x591A;&#x4E2A;&#x3002;</p><p>&#x591A;&#x7CFB;&#x7EDF;&#x5C31;&#x662F;&#x76F4;&#x63A5;&#x5728;&#x786C;&#x76D8;&#x4E0A;&#x518D;&#x591A;&#x5B89;&#x88C5;&#x4E00;&#x4E2A;&#x6216;&#x591A;&#x4E2A;&#x7CFB;&#x7EDF;&#xFF0C;&#x7136;&#x540E;&#x5728;&#x542F;&#x52A8;&#x65F6;&#x9009;&#x62E9;&#x8981;&#x542F;&#x52A8;&#x54EA;&#x4E2A;&#x3002;&#x53EF;&#x4EE5;&#x6700;&#x9AD8;&#x6548;&#x7684;&#x8BA9;&#x7CFB;&#x7EDF;&#x4F7F;&#x7528;&#x786C;&#x4EF6;&#x8D44;&#x6E90;&#xFF0C;&#x4F46;&#x4E00;&#x6B21;&#x53EA;&#x80FD;&#x5F00;&#x4E00;&#x4E2A;&#x7CFB;&#x7EDF;&#x3002;&#x5B89;&#x88C5;&#x524D;&#x9700;&#x8981;&#x6839;&#x636E;&#x8981;&#x5B89;&#x88C5;&#x7684;&#x7CFB;&#x7EDF;&#x5BF9;&#x90E8;&#x5206;&#x786C;&#x76D8;&#x8FDB;&#x884C;&#x683C;&#x5F0F;&#x5316;&#xFF0C;&#x800C;&#x5B89;&#x88C5;&#x540E;&#x4E5F;&#x53EF;&#x80FD;&#x56E0;&#x4E3A;&#x6587;&#x4EF6;&#x7CFB;&#x7EDF;&#x7684;&#x4E0D;&#x517C;&#x5BB9;&#xFF0C;&#x5207;&#x6362;&#x7CFB;&#x7EDF;&#x540E;&#x5B58;&#x5728;&#x4E00;&#x5B9A;&#x7684;&#x6587;&#x4EF6;&#x8BFB;&#x5199;&#x4E0A;&#x7684;&#x56F0;&#x96BE;&#xFF08;&#x5982;Windows&#x65E0;&#x6CD5;&#x76F4;&#x63A5;&#x8BFB;&#x53D6;Linux&#x7684;&#x5404;&#x79CD;ext&#x683C;&#x5F0F;&#xFF0C;&#x8DE8;&#x7CFB;&#x7EDF;&#x8BBF;&#x95EE;&#x6587;&#x4EF6;&#x540E;&#x4E5F;&#x53EF;&#x80FD;&#x4EA7;&#x751F;&#x4E00;&#x4E9B;&#x5947;&#x5947;&#x602A;&#x602A;&#x7684;&#x6587;&#x4EF6;&#x5360;&#x7528;&#x548C;&#x9501;&#xFF09;&#x3002;</p><h3 id="wsl%E4%B8%8Evm">WSL&#x4E0E;VM</h3><ul><li>WSL&#x53EF;&#x4EE5;&#x542F;&#x52A8;&#x5F97;&#x6BD4;VM&#x66F4;&#x5FEB;&#xFF0C;&#x5360;&#x7528;&#x8D44;&#x6E90;&#x4E5F;&#x6BD4;VM&#x66F4;&#x5C11;&#xFF08;WSL2&#x5176;&#x5B9E;&#x662F;&#x4F7F;&#x7528;VM&#x7684;&#xFF0C;&#x4F46;&#x6709;&#x9488;&#x5BF9;&#x6027;&#x4F18;&#x5316;&#xFF09;</li><li>WSL&#x4E0E;Windows&#x7684;&#x8FDE;&#x901A;&#x6027;&#x66F4;&#x597D;&#xFF0C;&#x6587;&#x4EF6;&#x7CFB;&#x7EDF;&#x53EF;&#x4EE5;&#x8BF4;&#x662F;&#x7684;&#x8054;&#x901A;&#x7684;&#x3002;&#x800C;VM&#x548C;Windows&#x662F;&#x5206;&#x9694;&#x5F00;&#x7684;&#xFF0C;&#x4F5C;&#x4E3A;&#x6C99;&#x76D2;&#xFF0C;&#x6709;&#x66F4;&#x597D;&#x7684;&#x5B89;&#x5168;&#x6027;&#xFF0C;&#x66F4;&#x9002;&#x5408;&#x505A;&#x5404;&#x79CD;&#x4F5C;&#x6B7B;&#x7684;&#x5B9E;&#x9A8C;&#x2026;&#x2026;</li><li>VM&#x91CC;&#x53EF;&#x4EE5;&#x63D0;&#x4F9B;&#x66F4;&#x5B8C;&#x6574;&#x5168;&#x9762;&#x7684;Linux&#x529F;&#x80FD;&#xFF0C;&#x6BD4;&#x5982;&#x56FE;&#x5F62;&#x3001;&#x97F3;&#x9891;&#x3001;&#x786C;&#x4EF6;&#x652F;&#x6301;</li><li>VM&#x91CC;&#x53EF;&#x4EE5;&#x8DD1;&#x7684;&#x4E0D;&#x53EA;&#x662F;Linux&#xFF0C;&#x6CDB;&#x7528;&#x6027;&#x66F4;&#x597D;&#x4E00;&#x4E9B;</li></ul><h2 id="wsl%E5%92%8Cwsl2%E7%9A%84%E5%B7%AE%E5%BC%82">WSL&#x548C;WSL2&#x7684;&#x5DEE;&#x5F02;</h2><p>Ref: <a href="https://docs.microsoft.com/zh-cn/windows/wsl/compare-versions">https://docs.microsoft.com/zh-cn/windows/wsl/compare-versions</a></p><ul><li>WSL1&#x76F4;&#x63A5;&#x5728;Windows&#x8FD0;&#x884C;&#xFF0C;WSL2&#x662F;&#x4F7F;&#x7528;VM&#x7684;&#x3002;</li><li>WSL2&#x76F8;&#x6BD4;WSL1&#xFF0C;&#x4F18;&#x5316;&#x4E86;&#x6587;&#x4EF6;&#x7CFB;&#x7EDF;&#x6027;&#x80FD;&#xFF0C;&#x4E5F;&#x63D0;&#x9AD8;&#x4E86;&#x7CFB;&#x7EDF;&#x8C03;&#x7528;&#x517C;&#x5BB9;&#x6027;&#xFF08;&#x5373;&#x66F4;&#x9AD8;&#x7684;&#x786C;&#x4EF6;&#x4F7F;&#x7528;&#x6548;&#x7387;&#xFF09;&#x3002;</li><li>&#x8DE8;Windows&#x548C;Linux&#x7CFB;&#x7EDF;&#x7684;&#x6587;&#x4EF6;&#x8BBF;&#x95EE;&#x4E0A;&#xFF0C;WSL1&#x66F4;&#x5FEB;</li><li>WSL2&#x4E0D;&#x652F;&#x6301;&#x4E32;&#x884C;&#x63A5;&#x53E3;&#xFF0C;&#x6240;&#x4EE5;&#x8981;&#x7528;&#x5230;&#x5916;&#x63A5;&#x8BBE;&#x5907;&#x7684;&#x8BDD;&#xFF0C;&#x53EA;&#x80FD;WSL1&#x3001;VM&#x6216;&#x591A;&#x7CFB;&#x7EDF;</li><li>WSL2&#x7684;&#x5185;&#x5B58;&#x662F;&#x968F;&#x4F7F;&#x7528;&#x72B6;&#x51B5;&#x52A8;&#x6001;&#x589E;&#x51CF;&#x7684;&#xFF0C;&#x4E14;&#x76EE;&#x524D;&#x7248;&#x672C;&#x6682;&#x4E0D;&#x652F;&#x6301;&#x5728;&#x4F7F;&#x7528;&#x4E2D;&#x628A;&#x7528;&#x4E8E;&#x7F13;&#x5B58;&#x7684;&#x5185;&#x5B58;&#x91CA;&#x653E;&#x56DE;Windows</li></ul><h1 id="wsl2%E5%AE%89%E8%A3%85%E3%80%81%E5%90%AF%E7%94%A8">WSL2&#x5B89;&#x88C5;&#x3001;&#x542F;&#x7528;</h1><h2 id="%E5%89%8D%E6%8F%90">&#x524D;&#x63D0;</h2><p>Windows 10 &#x7684; Build 19041 &#x4EE5;&#x4E0A;&#xFF0C;&#x6216;Windows 11&#x3002;</p><h2 id="%E5%AE%89%E8%A3%85">&#x5B89;&#x88C5;</h2><pre><code class="language-powershell">wsl --install</code></pre><p>&#x5F88;&#x7B80;&#x5355;&#xFF0C;&#x76F4;&#x63A5;&#x5728;PowerShell&#x91CC;&#x8FD0;&#x884C;&#x4E0A;&#x8FF0;&#xFF0C;&#x4F1A;&#x542F;&#x7528;&#x4E00;&#x4E2A;&#x9ED8;&#x8BA4;Ubuntu&#x53D1;&#x884C;&#x7248;&#x7684;WSL2&#x3002;</p><p>&#x9700;&#x8981;&#x4F7F;&#x7528;&#x5176;&#x4ED6;&#x53D1;&#x884C;&#x7248;&#x7684;&#x8BDD;&#xFF1A;</p><pre><code class="language-powershell">wsl --list --online</code></pre><p>&#x53EF;&#x4EE5;&#x67E5;&#x770B;&#x53EF;&#x7528;&#x7684;&#x53D1;&#x884C;&#x7248;&#xFF0C;&#x76EE;&#x524D;&#x6765;&#x8BF4;&#x6709;Ubuntu(&#x591A;&#x4E2A;&#x7248;&#x672C;), Debian, Kali(&#x4E13;&#x6CE8;&#x4E8E;&#x641E;&#x6E17;&#x900F;&#x6D4B;&#x8BD5;&#x7528;&#x7684;&#x90A3;&#x4E2A;), openSUSE, SLES&#x3002;</p><h2 id="%E9%85%8D%E7%BD%AE">&#x914D;&#x7F6E;</h2><p>&#x4E0A;&#x4E00;&#x6B65;&#x5B89;&#x88C5;&#x5B8C;&#x540E;&#xFF0C;&#x9700;&#x8981;&#x91CD;&#x65B0;&#x542F;&#x52A8;&#xFF0C;&#x624D;&#x53EF;&#x4EE5;&#x5728;&#x5F00;&#x59CB;&#x83DC;&#x5355;&#x627E;&#x5230;&#x5B89;&#x88C5;&#x597D;&#x7684;&#x5BF9;&#x5E94;Linux&#x53D1;&#x884C;&#x7248;&#x3002;&#x7B2C;&#x4E00;&#x6B21;&#x542F;&#x7528;&#x4F1A;&#x8981;&#x6C42;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x7BA1;&#x7406;&#x5458;&#x7528;&#x6237;&#xFF0C;&#x8981;&#x8BB0;&#x597D;&#x7528;&#x6237;&#x540D;&#x548C;&#x5BC6;&#x7801;&#x3002;</p><p>&#x5982;&#x679C;&#x5B89;&#x88C5;&#x4E4B;&#x524D;&#x5C31;&#x5B89;&#x88C5;&#x8FC7;Windows Terminal&#xFF0C;&#x90A3;&#x4ECE;&#x5F00;&#x59CB;&#x83DC;&#x5355;&#x542F;&#x52A8;&#x3001;&#x8BBE;&#x7F6E;&#x597D;&#x5BC6;&#x7801;&#x540E;&#xFF0C;&#x4ECE;Windows Terminal&#x5C31;&#x4E5F;&#x53EF;&#x4EE5;&#x627E;&#x5230;&#x8FD9;&#x4E2A;&#x9009;&#x9879;&#x4E86;&#x3002;</p>]]></content:encoded></item><item><title><![CDATA[Data Structure (8) - Graph]]></title><description><![CDATA[<h1 id="definition">Definition</h1><h2 id="graph">Graph</h2><p>G = (V, E), where V is a set of vertices, and E is a set of edges. </p><p>For some reason unknown, people usually use <strong>n</strong> for the number of vertices, and <strong>m</strong> for the number of edges. |V|=n, |E|=m</p><p>An edge can not point to nowhere.</p>]]></description><link>https://blog.fluere.cloud/data-structure-part8-graph/</link><guid isPermaLink="false">615e7163c76a54582562e2b3</guid><category><![CDATA[C++]]></category><category><![CDATA[数据结构]]></category><dc:creator><![CDATA[栗子℃（EN）]]></dc:creator><pubDate>Wed, 13 Oct 2021 13:50:17 GMT</pubDate><content:encoded><![CDATA[<h1 id="definition">Definition</h1><h2 id="graph">Graph</h2><p>G = (V, E), where V is a set of vertices, and E is a set of edges. </p><p>For some reason unknown, people usually use <strong>n</strong> for the number of vertices, and <strong>m</strong> for the number of edges. |V|=n, |E|=m</p><p>An edge can not point to nowhere. It&apos;s always with 2 vertices.</p><figure class="kg-card kg-image-card kg-card-hascaption"><img src="https://strapi.fluere.cloud/uploads/graph_vocabulary_definition_1_bf0ae2c42e.png" class="kg-image" alt loading="lazy"><figcaption>Vocabulary for a graph</figcaption></figure><ul><li>Complete Graph: A graph where all vertices are connected to each other.</li><li>Connected Graph: A graph where all vertices are connected, not necessarily to each other.</li><li>Acyclic Graph: A graph that has no cycles.</li></ul><h2 id="incident-edges">Incident Edges</h2><p>I(v) = { {x,v} in E}</p><p>It&apos;s all the edges connected to the particular vertex.</p><h2 id="degree">Degree</h2><p>D(v) = |I(v)|</p><p>The number of edges one vertex has.</p><h2 id="adjacent-vertices">Adjacent Vertices</h2><p>A(v) = {x: {x, v} in E}</p><h2 id="path">Path</h2><p>Path(G<sub>1</sub>) = Sequence of vertices connected by edges.</p><h2 id="cycle">Cycle</h2><p>Cycle(G<sub>2</sub>) = Path with that goes in circle (with common begin and end vertex).</p><h2 id="simple-graph">Simple Graph</h2><p>A graph that has no self-loop or multi-edges.</p><figure class="kg-card kg-image-card kg-card-hascaption"><img src="https://strapi.fluere.cloud/uploads/graph_what_is_not_a_simple_graph_bc060979c4.png" class="kg-image" alt loading="lazy"><figcaption>It&apos;s not simple at all</figcaption></figure><h2 id="subgraph">Subgraph</h2><p>G&apos; = {V&apos;, E&apos;} where V&apos; belongs to V, and E&apos; belongs to E and</p><p>if edge (u,v) belongs to E&apos;, then both <strong>u</strong> and <strong>v</strong> belongs to V&apos;.</p><ul><li>Complete subgraph: A subgraph that is a complete graph</li><li>Connected subgraph: All vertices in the subgraph are connected.</li><li>Acyclic subgraph: A subgraph that has no cycle.</li><li>Spanning Tree: A subgraph of graph G that contains all vertices of G with no cycle.</li></ul><figure class="kg-card kg-image-card kg-card-hascaption"><img src="https://strapi.fluere.cloud/uploads/subgraph_spanning_tree_definition_ecd4efce64.png" class="kg-image" alt loading="lazy"><figcaption>Spanning Tree</figcaption></figure><h1 id="observations-calculations">Observations / Calculations</h1><p>In a graph with n vertices and m edges:</p><p>Minimum Edges: </p><ul><li>In a not-connected graph &#x2014; 0</li><li>In a connected graph &#x2014; n-1</li></ul><p>Maximum Edges:</p><ul><li>In a simple graph &#x2014; n* (n-1) / 2</li><li>In a not-simple graph &#x2014; Infinity</li></ul><p>Sum of degree(v) for all v in V = 2*m &#x2014; or 2*|E|</p><p>Acyclic graph with n vertices has (n-1) edges.</p><p>Complete graph with n vertices has n* (n-1) / 2 edges.</p><h1 id="graph-adt">Graph ADT</h1><h2 id="data">Data</h2><ul><li>Vertices</li><li>Edges</li><li>Some data structure maintaining structure between vertices and edges </li></ul><h2 id="functions">Functions</h2><ul><li>insertVertex(K key) &#x2014; key as label of vertex</li><li>insertEdge(Vertex v1, Vertex v2, K key) &#x2014; key as label of edge</li><li>removeVertex(K key) or removeVertex(Vertex v)</li><li>removeEdge(K key) or removeEdge(Vertex v1, Vertex v2)</li><li>incidentEdges(Vertex v)</li><li>areAdjacent(Vertex v1, Vertex v2)</li></ul><p></p><p>-- to be continued</p><p></p><p>Ref:</p><ul><li><a href="https://www.bilibili.com/video/BV1454y127Lk?p=30">https://www.bilibili.com/video/BV1454y127Lk?p=30</a></li></ul><p></p><p></p><p></p><p></p>]]></content:encoded></item><item><title><![CDATA[Java基本数据类型]]></title><description><![CDATA[<h1 id="%E6%95%B0%E5%AD%97">&#x6570;&#x5B57;</h1><h2 id="%E6%95%B4%E6%95%B0">&#x6574;&#x6570;</h2><h3 id="integer-int">Integer / int</h3><ul><li>32&#x4F4D;&#x6709;&#x7B26;&#x53F7;&#x6574;&#x6570;&#xFF0C;&#x5177;&#x4F53;&#x8303;&#x56F4;&#x662F;&#xFF1A;-2<sup>31</sup> (-2147483648) ~ 2<sup>31</sup> -1 (2147483647)</li><li>&#x5927;&#x81F4;&#x8303;&#x56F4;&#xFF1A;&#x6B63;&#x8D1F;2*10<sup>9</sup> &#x4E0D;&#x7528;&#x7BA1;&#x6EA2;&#x51FA;&#xFF0C;10<sup>10</sup> &#x4E0D;&#x884C;</li><li>Integer&#x63A5;&#x53D7;</li></ul>]]></description><link>https://blog.fluere.cloud/java-basic-datatypes/</link><guid isPermaLink="false">61626253c76a54582562e388</guid><category><![CDATA[Java]]></category><category><![CDATA[备忘单]]></category><dc:creator><![CDATA[栗子℃]]></dc:creator><pubDate>Sun, 10 Oct 2021 05:22:03 GMT</pubDate><content:encoded><![CDATA[<h1 id="%E6%95%B0%E5%AD%97">&#x6570;&#x5B57;</h1><h2 id="%E6%95%B4%E6%95%B0">&#x6574;&#x6570;</h2><h3 id="integer-int">Integer / int</h3><ul><li>32&#x4F4D;&#x6709;&#x7B26;&#x53F7;&#x6574;&#x6570;&#xFF0C;&#x5177;&#x4F53;&#x8303;&#x56F4;&#x662F;&#xFF1A;-2<sup>31</sup> (-2147483648) ~ 2<sup>31</sup> -1 (2147483647)</li><li>&#x5927;&#x81F4;&#x8303;&#x56F4;&#xFF1A;&#x6B63;&#x8D1F;2*10<sup>9</sup> &#x4E0D;&#x7528;&#x7BA1;&#x6EA2;&#x51FA;&#xFF0C;10<sup>10</sup> &#x4E0D;&#x884C;</li><li>Integer&#x63A5;&#x53D7;null, int&#x4E0D;&#x63A5;&#x53D7;</li><li>Integer&#x9700;&#x8981;&#x662F;&#x7528;equals&#x5224;&#x65AD;&#x76F8;&#x7B49;&#xFF08;&#x76F4;&#x63A5;&#x7528;==&#x4F1A;&#x5224;&#x65AD;&#x662F;&#x5426;&#x662F;&#x540C;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#xFF0C;&#x800C;&#x975E;&#x662F;&#x5426;&#x6570;&#x503C;&#x76F8;&#x7B49;&#xFF09;&#xFF0C;&#x4F46;&gt;=&#x4E0E;&lt;=&#x53EF;&#x4EE5;&#x53EF;&#x4EE5;&#x6B63;&#x5E38;&#x4F7F;&#x7528;</li><li>Integer.parseInt&#x8FD4;&#x56DE;int&#xFF0C;Integer.valueOf&#x8FD4;&#x56DE;Integer</li></ul><h3 id="long-long">Long / long</h3><ul><li>64&#x4F4D;&#x6709;&#x7B26;&#x53F7;&#x6574;&#x6570;&#xFF0C;&#x5177;&#x4F53;&#x8303;&#x56F4;&#x662F; -2<sup>63</sup> (-9223372036854775808) ~ 2<sup>63</sup> -1 (9223372036854775807)</li><li>&#x5927;&#x81F4;&#x8303;&#x56F4;&#xFF1A;-9*10<sup>18</sup> ~ 9*10<sup>18</sup></li><li>&#x9ED8;&#x8BA4;&#x503C;&#x662F;0L</li></ul><h3 id="byte-byte">Byte / byte</h3><ul><li>8&#x4F4D;&#x6709;&#x7B26;&#x53F7;&#x6574;&#x6570;&#xFF0C;&#x5177;&#x4F53;&#x8303;&#x56F4;&#x662F; -128 ~ 127</li></ul><h3 id="short-short">Short / short</h3><ul><li>16&#x4F4D;&#x6709;&#x7B26;&#x53F7;&#x6574;&#x6570;&#xFF0C;&#x5177;&#x4F53;&#x8303;&#x56F4;&#x662F;-32768 ~ 32767</li></ul><h2 id="%E5%B0%8F%E6%95%B0">&#x5C0F;&#x6570;</h2><h3 id="float-float">Float / float</h3><ul><li>&#x6709;&#x7B26;&#x53F7;32&#x4F4D;IEEE754&#x6807;&#x51C6;&#x7684;&#x6D6E;&#x70B9;&#x6570;</li><li>&#x7B26;&#x53F7;&#x4F4D;(s)&#xFF1A;1&#xFF0C;&#x6307;&#x6570;&#x4F4D;(E)&#xFF1A;8&#xFF0C;&#x5C3E;&#x6570;&#x4F4D;(M)&#xFF1A;23 </li><li>&#x503C;&#x4E3A; (-1)<sup>S</sup> * 2<sup>E-127</sup> * (1.M)</li><li>&#x53D6;&#x503C;&#x8303;&#x56F4;&#xFF1A;&#x8D1F;&#x65E0;&#x7A77;&#xFF0C; -2<sup>128</sup> ~ -2<sup>-149</sup> &#xFF0C;0&#xFF0C;2<sup>-149</sup> ~ 2<sup>128</sup> &#xFF0C;&#x6B63;&#x65E0;&#x7A77;</li><li>&#x8303;&#x56F4;&#x4E0D;&#x8FDE;&#x5E8F;&#xFF0C;&#x65E0;&#x6CD5;&#x8868;&#x793A;&#x975E;&#x5E38;&#x63A5;&#x8FD1;0&#x7684;&#x6570;</li><li>&#x7CBE;&#x5EA6;&#x6709;&#x9650;&#xFF0C;7~8&#x4F4D;&#x6709;&#x6548;&#x4F4D;&#xFF08;7&#x51C6;&#x786E;&#xFF0C;8&#x5927;&#x90E8;&#x5206;&#x51C6;&#x786E;&#xFF09;</li></ul><h3 id="double-double">Double / double</h3><ul><li>&#x6709;&#x7B26;&#x53F7;64&#x4F4D;IEEE754&#x6807;&#x51C6;&#x7684;&#x6D6E;&#x70B9;&#x6570;</li><li>&#x7B26;&#x53F7;&#x4F4D;(s)&#xFF1A;1&#xFF0C;&#x6307;&#x6570;&#x4F4D;(E)&#xFF1A;11&#xFF0C;&#x5C3E;&#x6570;&#x4F4D;(M)&#xFF1A;52 </li><li>&#x503C;&#x4E3A; (-1)<sup>S</sup> * 2<sup>E-1023</sup> * (1.M)</li><li>&#x533A;&#x76F4;&#x5355;&#x4F4D;&#xFF1A;&#x8D1F;&#x65E0;&#x7A77;&#xFF0C; -2<sup>1024</sup> ~ -2<sup>-1074</sup> &#xFF0C;0&#xFF0C;2<sup>-1074</sup>~ 2<sup>1024</sup> &#xFF0C;&#x6B63;&#x65E0;&#x7A77;</li><li>&#x7CBE;&#x5EA6;&#x6709;&#x9650;&#xFF0C;&#x80FD;&#x4FDD;&#x8BC1;15~16&#x6709;&#x6548;&#x4F4D;</li></ul><h1 id="%E5%85%B6%E5%AE%83">&#x5176;&#x5B83;</h1><h2 id="character-char">Character / char</h2><ul><li>16&#x4F4D;Unicode&#xFF0C;&#x8303;&#x56F4;&#x662F;0~65535</li><li>&#x6700;&#x5C0F; \u0000</li><li>&#x6700;&#x5927; \uffff</li></ul><h2 id="boolean-boolean">Boolean / boolean</h2><ul><li>true / false</li><li>&#x4F7F;&#x7528;Boolean&#xFF08;&#x5E26;null&#xFF09;&#x7684;&#x60C5;&#x51B5;&#xFF0C;&#x9700;&#x8981;&#x5728;List, Set, Map&#x7B49;&#x4E2D;&#x4F7F;&#x7528;&#x65F6;&#xFF1B;&#x6216;&#x4EE5;null&#x8868;&#x793A;&#x6B64;&#x503C;&#x662F;&#x672A;&#x77E5;&#x7684;&#xFF08;&#x5982;&#xFF0C;&#x672A;&#x586B;&#x5199;&#x3001;&#x8FD8;&#x6CA1;&#x6709;&#x8FDB;&#x884C;&#x8FC7;&#x5224;&#x65AD;&#xFF09;</li></ul><p></p><p></p><p>Ref:</p><ul><li><a href="https://blog.csdn.net/a327369238/article/details/52354811">https://blog.csdn.net/a327369238/article/details/52354811</a></li><li><a href="https://www.runoob.com/java/java-basic-datatypes.html">https://www.runoob.com/java/java-basic-datatypes.html</a></li><li><a href="https://segmentfault.com/a/1190000005015151">https://segmentfault.com/a/1190000005015151</a></li></ul>]]></content:encoded></item><item><title><![CDATA[Data Structure (7) - Disjoint Sets]]></title><description><![CDATA[<h1 id="disjoint-sets">Disjoint Sets</h1><p>Disjoint Sets is the data structure that supports the operation of adding one element (as their own set), [finding the label/canonical_name of one element in a set], and union 2 sets.</p><p>It can represents data like:</p><ul><li>Set1 (people whose favorite color&apos;s green)</li><li>Set2 (people</li></ul>]]></description><link>https://blog.fluere.cloud/data-structure-part7-disjoint-sets/</link><guid isPermaLink="false">615de0ccc76a54582562e122</guid><category><![CDATA[C++]]></category><category><![CDATA[数据结构]]></category><dc:creator><![CDATA[栗子℃（EN）]]></dc:creator><pubDate>Tue, 05 Oct 2021 17:46:00 GMT</pubDate><content:encoded><![CDATA[<h1 id="disjoint-sets">Disjoint Sets</h1><p>Disjoint Sets is the data structure that supports the operation of adding one element (as their own set), [finding the label/canonical_name of one element in a set], and union 2 sets.</p><p>It can represents data like:</p><ul><li>Set1 (people whose favorite color&apos;s green)</li><li>Set2 (people whose favorite color&apos;s blue)</li><li>Set3 (people whose favorite color&apos;s red)</li><li>...</li></ul><p>For set S = {S<sub>1</sub>, S<sub>2</sub>, S<sub>3</sub>, ... , S<sub>k</sub>}: </p><ul><li>find(S<sub>i</sub>) where 1&lt;=i&lt;=k, gives representative integer of the set.</li></ul><p>e.g. find(S<sub>i</sub>) = min(S)</p><ul><li>join(Set1,<sub> </sub>Set2): join 2 sets into one. After this operation, find(S<sub>i</sub>) where S<sub>i</sub> was in Set1, will equal to find(S<sub>j</sub>) where S<sub>j</sub> was in Set2.</li></ul><figure class="kg-card kg-image-card kg-card-hascaption"><img src="https://strapi.fluere.cloud/uploads/example_disjoint_sets_with_min_as_label_fb5a48801b.png" class="kg-image" alt loading="lazy"><figcaption>Example: Disjoint Sets with min value as label</figcaption></figure><h2 id="key-ideas">Key Ideas</h2><ul><li>Each element exists in exactly one set.</li><li>(Elements in sets are not ordered.)</li><li>(If the element is not integer, hash it to integer first.)</li><li>Every set is an equitant representation: 4 belongs to [0]<sub>R</sub>, 8 belongs to [0]<sub>R</sub>, then find(4)==find(8) is true (find(4) is 0, and find(8) is 0).</li></ul><h2 id="build">Build</h2><p>When building disjoint sets: </p><ul><li>Start with everything separated</li><li>Build sets by union the elements</li></ul><h2 id="implementations">Implementations</h2><h3 id="array">Array</h3><p>Store label of item at array[hash(item)]</p><ul><li>find: O(1)</li><li>union: O(n) &#x2013; need to go through the array to update labels of one set</li></ul><h3 id="up-tree">Up-Tree</h3><p>Continue using an array where index is the key. Start all content in array as -1.</p><p>The value of array is:</p><ul><li>-1 if we&apos;ve found the representative element</li><li><strong>The index of parent</strong>, if we haven&apos;t found the rep.</li></ul><figure class="kg-card kg-image-card kg-card-hascaption"><img src="https://strapi.fluere.cloud/uploads/example_disjoint_sets_uptree_71792bce4e.png" class="kg-image" alt loading="lazy"><figcaption>Example: Disjoint Sets Implementation - UpTree</figcaption></figure><ul><li>find: O(h) where h is the height of the up-tree, and h is only bounded by n (size of the array). So we would like to keep the trees short.</li><li>union: O(1)</li></ul><p>Make up-trees faster: instead of storing -1 at roots, store -[number of nodes in tree] and use smart union:</p><pre><code class="language-cpp">int DisjointSets::find(int i) {
    if(arr_[i]&lt;0) return i; // this is the root
    else return find(arr_[i]);
}

void DisjointSets::union(int r1, int r2) {
    arr_[r1] = r2; // points r1 to r2
}

void DisjointSets::unionBySize(int r1, int r2) {
    // union by size: keeps the number of nodes in a tree smaller
    
    int newSize = arr_[r1] + arr_[r2];
    
    if(arr_[r1]&lt;arr_[r2]) { // r1 has more nodes
        arr_[r2] = r1; // so only -arr_[r2] nodes gets 1 layer higher
        arr_[r1] = newSize;
    } else {
        arr_[r1] = r2;
        arr_[r2] = newSize;
    }
}

void DisjointSets::unionByHeight(int r1, int r2) {
    // union by height: keeps the height of all trees smaller
    
    if(arr_[r1]&lt; arr_[r2]) { // r1 is heigher
        arr_[r2] = r1; // so r1 doesn&apos;t get heigher
    } else if (arr_[r1]&gt;arr_[r2]) {
        arr_[r1] = r2;
    } else { // tree gets higher no matter what
        arr_[r1] = r2;
        arr_[r2] -= 1;
    }
}</code></pre><p>which makes the height of the trees: O(log(n))</p><h4 id="path-compression">Path Compression</h4><pre><code class="language-cpp">int DisjointSets::find(int i) {
    if(arr_[i]&lt;0) return i; // this is the root
    else {
        int root = find(arr_[i]);
        arr_[i] = root;
        return root;
    }
}</code></pre><p>which makes find O(log(n)) amortized.</p><h3 id="if-we-use-both-smart-union-and-path-compression">If we use both smart union and path compression?</h3><p>The <strong>iterated log</strong> function:</p><p>log*(n) = {</p><p> 0							, n&lt;=1</p><p> 1+log*(log(n))	 , n&gt;1</p><p>}</p><p>e.g. What&apos;s lg*(2<sup>65535</sup>)? </p><p>1: lg(2<sup>65535</sup>)<sup> </sup>= 65535, </p><p>2: lg(65535) = 16, </p><p>3: lg(16) = 4, </p><p>4: lg(4) = 2, </p><p>5: lg(2) = 1</p><p>so lg*(2<sup>65535</sup>) = 5</p><p>union_find: O(m*&#x3B1;(n)) or O(&#x3B1;(n)) amortized, where n is number of items in Disjoint Sets. For all practice inputs, it&apos;s roughly O(1)</p><p></p><p>Ref: </p><ul><li>https://courses.engr.illinois.edu/cs225/fa2020/</li><li><a href="https://www.bilibili.com/video/BV1454y127Lk?p=29">https://www.bilibili.com/video/BV1454y127Lk?p=29</a></li></ul>]]></content:encoded></item><item><title><![CDATA[Data Structure (6) - Heaps & Priority Queues]]></title><description><![CDATA[<h2 id="adt">ADT</h2><ul><li>insert(value)</li><li>remove()</li><li>isEmpty()</li></ul><p>Where value is among int, string, double, char... or objects that supports &lt; operator.</p><h1 id="heap">Heap</h1><p>Unsorted Array: O(1)* insert, O(n) removeMin (need to find min first)</p><p>Unsorted Linked List: O(1) insert, O(n) removeMin</p><p>Sorted Array: O(n)* insert, O(1) removeMin</p>]]></description><link>https://blog.fluere.cloud/data-structure-part6-heaps-and-priority-queues/</link><guid isPermaLink="false">615dab32c76a54582562de9b</guid><category><![CDATA[C++]]></category><category><![CDATA[数据结构]]></category><dc:creator><![CDATA[栗子℃（EN）]]></dc:creator><pubDate>Mon, 04 Oct 2021 13:57:00 GMT</pubDate><content:encoded><![CDATA[<h2 id="adt">ADT</h2><ul><li>insert(value)</li><li>remove()</li><li>isEmpty()</li></ul><p>Where value is among int, string, double, char... or objects that supports &lt; operator.</p><h1 id="heap">Heap</h1><p>Unsorted Array: O(1)* insert, O(n) removeMin (need to find min first)</p><p>Unsorted Linked List: O(1) insert, O(n) removeMin</p><p>Sorted Array: O(n)* insert, O(1) removeMin</p><p>Sorted Linked List: O(n) insert, O(1) removeMin</p><p>It&apos;s easier to removeMin on sorted structures and insert on unsorted structures.</p><p>To balance the worst case time to O(lg(n)) insert and O(lg(n)) removeMin, we could use a binary tree.</p><h2 id="definition">Definition</h2><h3 id="min-heap">(min-)Heap</h3><p>A complete binary tree t is a min-heap if:</p><ul><li>T = {} or</li><li>T = {r, T<sub>L</sub>, T<sub>R</sub>} where r is less than roots of {T<sub>L</sub> , T<sub>R</sub>} and {T<sub>L</sub> , T<sub>R</sub>} are min-heaps.</li></ul><p>Logically it&apos;s a tree, and implementation-wise, it&apos;s an array.</p><figure class="kg-card kg-image-card kg-card-hascaption"><img src="https://strapi.fluere.cloud/uploads/example_a_min_heap_454ad16d0e.png" class="kg-image" alt loading="lazy"><figcaption>Example: a min-heap</figcaption></figure><p>The heap above in array would be: [1, 4, 5, 12, 7, 6, 8, 13, 14, null, null, 11, null, null, null]</p><p>We could define left must be larger or smaller than right, but as long as all roots are smaller than their children, it&apos;s a min-heap.</p><p>Priority queues are based on heaps.</p><h2 id="operations">Operations</h2><ul><li>Left child of node[i]:		node[2*i]</li><li>Right child of node[i]:	 node[2*i + 1]</li><li>Parent of node[i]:			 floor(i/2)</li></ul><p>We could also store the number of nodes, n, in node[0], and the above remains the same. Knowing the number of nodes can help you know where in the array is outside of the heap.</p><h3 id="how-to-insert">How to Insert</h3><pre><code class="language-cpp">template &lt;class T&gt;
void Heap&lt;T&gt;::_insert(const T &amp; key) {
    // check if there&apos;s enough space
    if (size_ == capacity) {
        _growArray();
    }
    
    // insert element at the end of array
    item_[++size] = key;
    
    // restore heap property
    _heapifyUp();
}

template &lt;class T&gt;
void Heap&lt;T&gt;::_heapifyUp(size_t index) {
    if(index&gt;1) { // 1 is the root here, as we stored size in 0
        if(item_[index] &lt; item_[parent(index)]) {
            std::swap(item_[index], item_[parent(index));
            _heapifyUp(parent(index));
        }
    }
}</code></pre><h3 id="how-to-remove">How to Remove</h3><p>removeMin() is removing root of a min-heap, and removeMax() is removing root of a max-heap.</p><p>When removing root:</p><ol><li>Swap root with the last node.</li><li>Remove the last node (root).</li><li>Heapify to restore heap property</li></ol><pre><code class="language-cpp">template &lt;class T&gt;
T Heap&lt;T&gt;::_remove() {
    T min_value = item_[1];
    item_[1] = item_[size--]; // swap with last and delete
    
    _heapifyDown();
    return min_value; // return original root value
}

template &lt;class T&gt;
void Heap&lt;T&gt;::_heapifyDown(size_t index=1) {
    if(!_isLeaf(index)) {
        size_t minChildIndex = _minChild(index);
        if(item_[index] &gt; item_[minChildIndex]) {
            std::swap(item_[index], item_[minChildIndex]);
            _heapifyDown(minChildIndex);
        }
    }
}</code></pre><h3 id="how-to-build-a-heap">How to Build a Heap</h3><p>buildHeap - heapifyUp: O(n*log(n)) &#xA0;&#x2013; each insert is O(log(n))</p><pre><code class="language-cpp">void Heap&lt;T&gt;::buildHeap() {
    for(unsigned i=2; i&lt;= size_; i++) {
        heapifyUp(i);
        // takes O(n logn)
    }
}</code></pre><p>buildHeap - heapifyDown: O(n) &#xA0;&#x2013; takes time of O(sum of the height of all sub trees)</p><pre><code>void Heap&lt;T&gt;::buildHeap() {
    for(unsigned i=parent(size_); i&gt;0; i--) {
        heapifyDown(i);
        // takes O(n)
    }
}</code></pre><p>Prove heap building running time:</p><ul><li>S(h): Sum of heights of all nodes in a complete tree of h</li><li>S(0) = 0</li><li>S(1) = 1 + 0 + 0 = 1</li><li>S(2) = 2 + 1 + 1 = 4</li><li>....</li><li>S(h) = h + 2 * S(h-1) = 2<sup>(h+1)</sup> -h - 2</li><li>Since h&lt;=lg(n), </li><li> O(2<sup>(lg(n)+1)</sup> - lg(n) -2) = O(n-2-lg(n)) = O(n)</li></ul>]]></content:encoded></item><item><title><![CDATA[Regex Cheat Sheet]]></title><description><![CDATA[<!--kg-card-begin: markdown--><table>
<thead>
<tr>
<th>Syntax &#xA0;&#xA0;&#xA0;&#xA0;</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>*</td>
<td>0 or more times</td>
</tr>
<tr>
<td>+</td>
<td>1 or more times</td>
</tr>
<tr>
<td>?</td>
<td>1 or 0 times</td>
</tr>
<tr>
<td>{m}</td>
<td>exactly m times</td>
</tr>
<tr>
<td>{m,}</td>
<td>m or more times</td>
</tr>
<tr>
<td>{m,n}</td>
<td>m to n times (both inclusive)</td>
</tr>
<tr>
<td>\n</td>
<td>new line</td>
</tr>
<tr>
<td>[&#x2026;]</td>
<td>range or character class (e.g. [a-z0-9] all lowercases and digits)</td></tr></tbody></table>]]></description><link>https://blog.fluere.cloud/regex-cheat-sheet/</link><guid isPermaLink="false">615e822bc76a54582562e30f</guid><category><![CDATA[笔记]]></category><category><![CDATA[备忘单]]></category><category><![CDATA[Tech]]></category><dc:creator><![CDATA[栗子℃（EN）]]></dc:creator><pubDate>Sat, 02 Oct 2021 05:16:00 GMT</pubDate><content:encoded><![CDATA[<!--kg-card-begin: markdown--><table>
<thead>
<tr>
<th>Syntax &#xA0;&#xA0;&#xA0;&#xA0;</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>*</td>
<td>0 or more times</td>
</tr>
<tr>
<td>+</td>
<td>1 or more times</td>
</tr>
<tr>
<td>?</td>
<td>1 or 0 times</td>
</tr>
<tr>
<td>{m}</td>
<td>exactly m times</td>
</tr>
<tr>
<td>{m,}</td>
<td>m or more times</td>
</tr>
<tr>
<td>{m,n}</td>
<td>m to n times (both inclusive)</td>
</tr>
<tr>
<td>\n</td>
<td>new line</td>
</tr>
<tr>
<td>[&#x2026;]</td>
<td>range or character class (e.g. [a-z0-9] all lowercases and digits)</td>
</tr>
<tr>
<td>[^&#x2026;]</td>
<td>not in range or negated character class (e.g. [^0-9a-z] everything but 0-9 and lower case characters)</td>
</tr>
<tr>
<td>.</td>
<td>Any character except newline</td>
</tr>
<tr>
<td>\w</td>
<td>Word character [a-zA-Z0-9_]</td>
</tr>
<tr>
<td>\W</td>
<td>Non-word character [^a-zA-Z0-9_]</td>
</tr>
<tr>
<td>\d</td>
<td>Digit character [0-9]</td>
</tr>
<tr>
<td>\D</td>
<td>Non-digit character [^0-9]</td>
</tr>
<tr>
<td>\s</td>
<td>Whitespace character [\n\r\f\t]</td>
</tr>
<tr>
<td>\S</td>
<td>Non-whitespace character [^\n\r\f\t]</td>
</tr>
<tr>
<td>^</td>
<td>The start of the line of text</td>
</tr>
<tr>
<td>$</td>
<td>The end of the line of text. (e.g. <sup class="footnote-ref"><a href="#fn1" id="fnref1">[1]</a></sup>$ match an all-digit-and-dash string)</td>
</tr>
<tr>
<td>\b</td>
<td>Word boundary</td>
</tr>
<tr>
<td>\B</td>
<td>Not-word-boundary</td>
</tr>
<tr>
<td>i</td>
<td>Case-insensitive matching m ^ and $ match next to embedded \n</td>
</tr>
<tr>
<td>(&#x2026;)</td>
<td>Group subpattern and capture submatch into \1, \2, ..</td>
</tr>
<tr>
<td>\n</td>
<td>Contains the result of nth earlier submatch from a parentheses capture group, or a named capture group</td>
</tr>
</tbody>
</table>
<hr class="footnotes-sep">
<section class="footnotes">
<ol class="footnotes-list">
<li id="fn1" class="footnote-item"><p>0-9- <a href="#fnref1" class="footnote-backref">&#x21A9;&#xFE0E;</a></p>
</li>
</ol>
</section>
<!--kg-card-end: markdown--><p></p><p>Ref: </p><ul><li><a href="https://regexcheatsheet.com/">https://regexcheatsheet.com/</a></li><li><a href="https://www3.ntu.edu.sg/home/ehchua/programming/howto/Regexe.html">https://www3.ntu.edu.sg/home/ehchua/programming/howto/Regexe.html</a></li></ul>]]></content:encoded></item><item><title><![CDATA[常见的网页字体]]></title><description><![CDATA[<h1 id="%E8%8B%B1%E6%96%87%E5%AD%97%E4%BD%93">&#x82F1;&#x6587;&#x5B57;&#x4F53;</h1><!--kg-card-begin: html--><figure>
	<img src="https://strapi.fluere.cloud/uploads/common_fonts_3a025c6e40.png">
    <figurecaption><a href="http://ampsoft.net/webdesign-l/WindowsMacFonts.html">ref: Common Fonts for Windows &amp; MAC</a></figurecaption>
</figure><!--kg-card-end: html--><p>&#x200C;&#x200C; </p><h2 id="serif">Serif</h2><p>Serif &#x6765;&#x81EA;&#x53E4;&#x8377;&#x5170;&#x8BED;&#x7684;&#x201C;&#x77ED;&#x7EBF;&#x201D;&#x3001;&#x201C;&#x7EBF;&#x201D;&#x3002;Serif &#x6307;&#x886C;&#x7EBF;&#x5B57;&#x4F53;&#xFF0C;&#x5373;&#x5728;&#x5B57;&#x6BCD;&#x7684;&#x5F00;&#x59CB;&#x3001;&#x7ED3;&#x675F;&#x7684;&#x5730;</p>]]></description><link>https://blog.fluere.cloud/web-and-fonts/</link><guid isPermaLink="false">61546b2fc76a54582562d31e</guid><category><![CDATA[Web]]></category><category><![CDATA[笔记]]></category><dc:creator><![CDATA[栗子℃]]></dc:creator><pubDate>Wed, 29 Sep 2021 14:31:00 GMT</pubDate><content:encoded><![CDATA[<h1 id="%E8%8B%B1%E6%96%87%E5%AD%97%E4%BD%93">&#x82F1;&#x6587;&#x5B57;&#x4F53;</h1><!--kg-card-begin: html--><figure>
	<img src="https://strapi.fluere.cloud/uploads/common_fonts_3a025c6e40.png">
    <figurecaption><a href="http://ampsoft.net/webdesign-l/WindowsMacFonts.html">ref: Common Fonts for Windows &amp; MAC</a></figurecaption>
</figure><!--kg-card-end: html--><p>&#x200C;&#x200C; </p><h2 id="serif">Serif</h2><p>Serif &#x6765;&#x81EA;&#x53E4;&#x8377;&#x5170;&#x8BED;&#x7684;&#x201C;&#x77ED;&#x7EBF;&#x201D;&#x3001;&#x201C;&#x7EBF;&#x201D;&#x3002;Serif &#x6307;&#x886C;&#x7EBF;&#x5B57;&#x4F53;&#xFF0C;&#x5373;&#x5728;&#x5B57;&#x6BCD;&#x7684;&#x5F00;&#x59CB;&#x3001;&#x7ED3;&#x675F;&#x7684;&#x5730;&#x65B9;&#x6709;&#x989D;&#x5916;&#x88C5;&#x9970;&#x7684;&#x5B57;&#x4F53;&#x3002;</p><p>&#x886C;&#x7EBF;&#x5B57;&#x4F53;&#x7684;&#x7531;&#x6765;&#xFF1A;&#x636E;&#x8BF4;&#x662F;&#x6765;&#x81EA;&#x5BF9;&#x7B14;&#x5237;&#x3001;&#x96D5;&#x523B;&#x5B57;&#x4F53;&#x7684;&#x6A21;&#x4EFF;&#x3002;&#x886C;&#x7EBF;&#x5373;&#x7528;&#x5237;&#x5B50;&#x5237;&#x5B57;&#x65F6;&#xFF0C;&#x4E3A;&#x4E86;&#x8BA9;&#x5B57;&#x6BCD;&#x7684;&#x5F00;&#x59CB;&#x3001;&#x7ED3;&#x5C3E;&#x6574;&#x9F50;&#x65E0;&#x6BDB;&#x523A;&#xFF0C;&#x800C;&#x5782;&#x76F4;&#x4E66;&#x5199;&#x65B9;&#x5411;&#x8FDB;&#x884C;&#x5212;&#x7EBF;&#x3001;&#x505C;&#x987F;&#x7B49;&#x800C;&#x7559;&#x4E0B;&#x7684;&#x75D5;&#x8FF9;&#x3002;&#x4EE5;&#x53CA;&#x96D5;&#x523B;&#x65F6;&#x4E0B;&#x5200;&#x3001;&#x6536;&#x5200;&#x3001;&#x6216;&#x4E3A;&#x4E86;&#x906E;&#x76D6;&#x7ED3;&#x5C3E;&#x4E0D;&#x6574;&#x9F50;&#x800C;&#x7559;&#x4E0B;&#x7684;&#x75D5;&#x8FF9;&#x3002;</p><p>&#x4E0B;&#x56FE;&#x4E2D;&#x7BAD;&#x5934;&#x6307;&#x7684;&#x5C31;&#x662F;&#x886C;&#x7EBF;&#x3002;</p><figure class="kg-card kg-image-card kg-card-hascaption"><img src="https://strapi.fluere.cloud/uploads/fonts_serif_vs_sans_a8d536cce8.png" class="kg-image" alt loading="lazy"><figcaption>serif vs sans</figcaption></figure><p>&#x8F83;&#x5E38;&#x89C1;&#x7684; serif &#x5B57;&#x4F53;&#x6709; Times (&#x5373;Time Roman), Times New Roman, Georgia, Palatino, Bookman&#x7B49;&#x3002;</p><h2 id="sans-serif">Sans Serif</h2><p>Sans &#x662F;&#x6CD5;&#x8BED;&#x7684;&#x201C;&#x6CA1;&#x6709;&#x201D;&#x3002;Sans-Serif &#x6307;&#x65E0;&#x886C;&#x7EBF;&#x5B57;&#x4F53;&#xFF0C;&#x5982;&#x4E0A;&#x56FE;&#x6240;&#x793A;&#x3002;</p><p>&#x8F83;&#x5E38;&#x89C1;&#x7684; sans-serif &#x5B57;&#x4F53;&#x6709; Helvetica, Arial, Verdana, Tahoma, Segoe UI&#x7B49;&#x3002;&#x6B64;&#x5916;&#xFF0C;&#x5B89;&#x5353;&#x4E0A;&#x8FD8;&#x5E38;&#x89C1;Roboto&#x548C;Droid Sans&#x3002;</p><h2 id="monospace">Monospace</h2><p>&#x6BCF;&#x4E2A;&#x5B57;&#x7B26;&#x5360;&#x5F97;&#x7A7A;&#x95F4;&#x90FD;&#x4E00;&#x6837;&#x5927;&#xFF0C;&#x65E0;&#x8BBA;&#x5185;&#x5BB9;&#x662F;&#x4EC0;&#x4E48;&#xFF0C;&#x4E0A;&#x4E0B;&#x4E24;&#x884C;&#x7684;&#x5B57;&#x6BCD;&#x95F4;&#x90FD;&#x80FD;&#x4E24;&#x4E24;&#x5BF9;&#x9F50;&#x3002;&#x7ECF;&#x5E38;&#x7528;&#x4E8E;&#x663E;&#x793A;&#x7A0B;&#x5E8F;&#xFF0C;&#x5B57;&#x7B26;&#x753B;&#x7B49;&#x3002;</p><p>&#x8F83;&#x5E38;&#x89C1;&#x7684; monospace &#x5B57;&#x4F53;&#x6709; Courier, Courier New, Andale Mono&#x7B49;&#x3002;</p><h2 id="%E5%93%AA%E9%87%8C%E9%83%BD%E8%83%BD%E6%94%BE%E5%BF%83%E4%BD%BF%E7%94%A8%E7%9A%84%E8%8B%B1%E6%96%87%E5%AD%97%E4%BD%93">&#x54EA;&#x91CC;&#x90FD;&#x80FD;&#x653E;&#x5FC3;&#x4F7F;&#x7528;&#x7684;&#x82F1;&#x6587;&#x5B57;&#x4F53;</h2><p>ref: <a href="https://web.mit.edu/jmorzins/www/fonts.html">https://web.mit.edu/jmorzins/www/fonts.html</a></p><p>The fonts that are most safe to use are&#xFF08;&#x57FA;&#x672C;&#x54EA;&#x91CC;&#x90FD;&#x80FD;&#x7528;&#x7684;&#x7CFB;&#x7EDF;&#x81EA;&#x5E26;&#xFF09;:</p><ul><li>Arial / Helvetica</li><li>Times New Roman / Times</li><li>Courier New / Courier</li></ul><p>Other options that usually work cross-platform are&#xFF08;&#x5176;&#x4ED6;&#x8DE8;&#x5E73;&#x53F0;&#x9009;&#x62E9;&#xFF09;:</p><ul><li>Palatino</li><li>Garamond</li><li>Bookman</li><li>Avant Garde</li></ul><p>Fonts that work on Windows and MacOS but not Unix+X are&#xFF08;Window/MacOS&#x81EA;&#x5E26;&#xFF0C;Unix+X&#x6CA1;&#x6709;&#xFF09;:</p><ul><li>Verdana</li><li>Georgia</li><li>Comic Sans MS</li><li>Trebuchet MS</li><li>Arial Black</li><li>Impact</li></ul><p></p><h1 id="%E4%B8%AD%E6%96%87%E5%AD%97%E4%BD%93">&#x4E2D;&#x6587;&#x5B57;&#x4F53;</h1><h2 id="%E5%85%BC%E5%AE%B9%E9%97%AE%E9%A2%98">&#x517C;&#x5BB9;&#x95EE;&#x9898;</h2><p>&#x5F88;&#x53EF;&#x60DC;&#xFF0C;&#x5404;&#x79CD;&#x64CD;&#x4F5C;&#x7CFB;&#x7EDF;&#x4E2D;&#x5E76;&#x6CA1;&#x6709;&#x4E00;&#x4E2A;&#x80FD;&#x901A;&#x7528;&#x7684;&#x4E2D;&#x6587;&#x5B57;&#x4F53;&#x3002;</p><p>&#x5BF9; Windows:</p><ul><li>Vista&#x4E4B;&#x540E;&#xFF1A;Microsoft Yahei&#xFF0C;&#x5FAE;&#x8F6F;&#x96C5;&#x9ED1;</li><li>Vista&#x4E4B;&#x524D;&#xFF1A;SimSun (&#x4E2D;&#x6613;&#x5B8B;&#x4F53;)</li></ul><p>&#x5BF9; Mac OS &#x548C; iOS</p><ul><li>&#x200C;Hiragino Sans GB &#xFF08;&#x51AC;&#x9752;&#x9ED1;&#x4F53;/&#x82F9;&#x679C;&#x4E3D;&#x9ED1;&#xFF09;</li><li>&#x534E;&#x6587;&#x9ED1;&#x4F53;</li></ul><p>&#x5BF9;Android:</p><ul><li>Droid Sans Fallback</li></ul><p>&#x5BF9;Linux&#x5404;&#x53D1;&#x884C;&#x7248;&#x2026;&#x2026;&#x6CA1;&#x8F99;&#x4E86;&#xFF0C;&#x53EA;&#x80FD;&#x8BD5;&#x8BD5;&#x770B;&#x5BF9;&#x65B9;&#x6709;&#x6CA1;&#x6709;&#x5B89;&#x88C5;&#x5F00;&#x6E90;&#x4E2D;&#x6587;&#x5B57;&#x4F53;</p><ul><li>WenQuanYi Micro Hei &#xFF08;&#x6587;&#x6CC9;&#x9A7F;&#x5FAE;&#x7C73;&#x9ED1;&#xFF09;&#xFF08;&#x8FD9;&#x4E2A;&#x6700;&#x5E38;&#x89C1;&#x4E86;&#xFF09;</li><li>Source Han Sans &#xFF08;&#x601D;&#x6E90;&#x9ED1;&#x4F53;&#xFF09;</li></ul>]]></content:encoded></item><item><title><![CDATA[复习 ---- 位运算，乘除与移位]]></title><description><![CDATA[<h1 id="%E4%BD%8D%E8%BF%90%E7%AE%97">&#x4F4D;&#x8FD0;&#x7B97;</h1><h2 id="%E4%B8%94-bitwise-and">&#x4E14; &amp; &#xA0;Bitwise AND</h2><p>&#x6309;&#x4F4D;&#x4E14;&#x3002;&#x5411;&#x53F3;&#x5BF9;&#x5176;&#xFF0C;&#x5BF9;&#x5176;&#x540E;&#x4E24;&#x4F4D;&#x90FD;&#x5728;&#x4E14;&#x4E24;&#x4F4D;&#x90FD;&#x4E3A;1&#x7684;&#x65F6;&#x5019;&#x624D;&#x4E3A;1&#x3002;</p><p>&#x4F8B;&#xFF1A;1110 &amp; 0001 = 0000&#xFF0C;1111 &amp; 1101 = 1101&#xFF0C; 1010</p>]]></description><link>https://blog.fluere.cloud/bitewise-operations-and-shifts/</link><guid isPermaLink="false">61529766c76a54582562d09e</guid><category><![CDATA[基础复习]]></category><category><![CDATA[笔记]]></category><category><![CDATA[Java]]></category><dc:creator><![CDATA[栗子℃]]></dc:creator><pubDate>Tue, 28 Sep 2021 11:15:00 GMT</pubDate><content:encoded><![CDATA[<h1 id="%E4%BD%8D%E8%BF%90%E7%AE%97">&#x4F4D;&#x8FD0;&#x7B97;</h1><h2 id="%E4%B8%94-bitwise-and">&#x4E14; &amp; &#xA0;Bitwise AND</h2><p>&#x6309;&#x4F4D;&#x4E14;&#x3002;&#x5411;&#x53F3;&#x5BF9;&#x5176;&#xFF0C;&#x5BF9;&#x5176;&#x540E;&#x4E24;&#x4F4D;&#x90FD;&#x5728;&#x4E14;&#x4E24;&#x4F4D;&#x90FD;&#x4E3A;1&#x7684;&#x65F6;&#x5019;&#x624D;&#x4E3A;1&#x3002;</p><p>&#x4F8B;&#xFF1A;1110 &amp; 0001 = 0000&#xFF0C;1111 &amp; 1101 = 1101&#xFF0C; 1010 &amp; 1001 = 1000</p><p>&#x5E38;&#x89C1;&#x7528;&#x9014;&#xFF1A;&#x63A9;&#x7801;&#xFF0C;&#x53D6;&#x67D0;&#x4E2A;&#x6570;&#x7B2C;n&#x4F4D;&#xFF08;&amp; &#x9664;&#x4E86;&#x7B2C;n&#x4F4D;&#x662F;1&#xFF0C;&#x5176;&#x4ED6;&#x90FD;&#x662F;0&#x7684;&#x6570;&#xFF0C;&#x7136;&#x540E;&#x9664;&#x4EE5;n&#x4E2A;2&#xFF09;, &#x5224;&#x65AD;&#x4E00;&#x4E2A;&#x6570;&#x7684;&#x5947;&#x5076;&#xFF08;&amp;1==1&#x662F;&#x5947;&#x6570;&#xFF09;</p><h2 id="%E6%88%96-bitwise-or">&#x6216; | &#xA0;Bitwise OR</h2><p>&#x6309;&#x4F4D;&#x6216;&#x3002;&#x5411;&#x53F3;&#x5BF9;&#x5176;&#xFF0C;&#x5BF9;&#x5176;&#x540E;&#x6709;&#x4E00;&#x4F4D;&#x4E3A;1&#x5C31;&#x4E3A;1</p><p>&#x4F8B;&#xFF1A;0000|1111 = 1111&#xFF0C; 0101 &amp; 1010 = 1111&#xFF0C;0010 &amp; 1010 = 1010</p><h2 id="%E9%9D%9E-bitwise-complementnot">&#x975E; ~ &#xA0;Bitwise Complement/NOT</h2><p>&#x6309;&#x4F4D;&#x53D6;&#x53CD;</p><p>&#x4F8B;&#xFF1A; ~0000 = 1111&#xFF0C; &#xA0;~1010 = 0101&#xFF0C; ~1110 = 0001</p><p>&#x6CE8;&#x610F;&#xFF1A;&#x5728;&#x5927;&#x591A;&#x8BED;&#x8A00;&#x7684;&#x5927;&#x591A;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x91CC;&#xFF0C;&#x6570;&#x90FD;&#x4E0D;&#x53EA;4&#x4F4D;&#xFF0C;&#x9AD8;&#x4F4D;&#x7684;0&#x53D6;&#x975E;&#x540E;&#x90FD;&#x4F1A;&#x53D8;&#x6210;1</p><p>&#x5982;&#xFF0C;java&#x4E2D;&#xFF1A;</p><p>1: 00000000000000000000000000000001 </p><p>~1(-2): 11111111111111111111111111111110 </p><p>1000: 00000000000000000000001111101000 </p><p>~1000(-1001): 11111111111111111111110000010111</p><pre><code class="language-java">public static void printAllBits(int t){
	System.out.println(t);
	int[] res = new int[32];
	for(int i = 0; i&lt;32; i++){
		res[i] = t&amp;1; // &#x53D6;&#x672B;&#x4F4D;
		t&gt;&gt;=1; // &#x53F3;&#x79FB;
	}
	for(int i=31; i&gt;=0; i--){ //&#x4ECE;&#x9AD8;&#x4F4D;&#x5F00;&#x59CB;&#x6253;&#x5370;
		System.out.print(res[i]);
	}
	System.out.println();
}</code></pre><h2 id="%E5%BC%82%E6%88%96-bitwise-xor">&#x5F02;&#x6216; ^ Bitwise XOR</h2><p>&#x6309;&#x4F4D;&#x5F02;&#x6216;&#x3002;&#x5411;&#x53F3;&#x5BF9;&#x5176;&#xFF0C;&#x5BF9;&#x5176;&#x7684;&#x4E24;&#x4F4D;&#x4E0D;&#x540C;&#x65F6;&#x53D6;1&#xFF0C;&#x76F8;&#x540C;&#x65F6;&#x53D6;0</p><p>&#x4F8B;&#xFF1A;1111^0000 = 1111, &#xA0;1010^0001 = 1011, &#xA0;0001^0101 = 0100</p><p>&#x5E38;&#x89C1;&#x7528;&#x9014;&#xFF1A;&#x62B5;&#x6D88;&#x76F8;&#x540C;&#x7684;&#x6570;&#xFF0C;&#x5224;&#x65AD;&#x4E24;&#x6570;&#x7B26;&#x53F7;&#x662F;&#x5426;&#x76F8;&#x540C;&#xFF08;&#x5F02;&#x6216;&#x540E;&#x770B;&#x7B26;&#x53F7;&#x4F4D;&#xFF09;</p><h1></h1><h1 id="%E4%B9%98%E9%99%A4%E5%92%8C%E4%BD%8D%E7%A7%BB">&#x4E58;&#x9664;&#x548C;&#x4F4D;&#x79FB;</h1><h2 id="%E5%8D%81%E8%BF%9B%E5%88%B6%E5%92%8C%E4%BA%8C%E8%BF%9B%E5%88%B6">&#x5341;&#x8FDB;&#x5236;&#x548C;&#x4E8C;&#x8FDB;&#x5236;</h2><p>&#x4E8C;&#x8FDB;&#x5236;&#xFF1A;11101 &#x5373; 1*2<sup>4</sup> + 1*2<sup>3</sup> + 1*2<sup>2</sup> + 0*2<sup>1</sup> + 1*2<sup>0</sup> </p><p>&#x5341;&#x8FDB;&#x5236;&#xFF1A; 2<sup>4</sup> + 2<sup>3</sup> + 2<sup>2</sup> + 0 + 2<sup>0</sup> = 16+8+4+0+1 = 29 </p><h2 id="%E4%B9%98%E6%B3%95%E5%92%8C%E5%B7%A6%E7%A7%BB">&#x4E58;&#x6CD5;&#x548C;&#x5DE6;&#x79FB;</h2><p>&#x4F8B;&#xFF1A;29 (11101) * 4 (10)</p><p>= (1*2<sup>4</sup> + 1*2<sup>3</sup> + 1*2<sup>2</sup> + 0*2<sup>1</sup> + 1*2<sup>0</sup>)* 2<sup>2</sup> </p><p>= (1*2<sup>6</sup> + 1*2<sup>5</sup> + 1*2<sup>4</sup> + 0*2<sup>3</sup> + 1*2<sup>2</sup>) + (0*2<sup>1</sup> + 0*2<sup>0</sup>) &#xA0;</p><p>&#xFF08;&#x5373;&#x4E8C;&#x8FDB;&#x5236; 1110100&#xFF0C;11101&#x5DE6;&#x79FB;&#x4E24;&#x4F4D;&#xFF09;</p><p>&#x6240;&#x4EE5;&#xFF0C;a * 2<sup>i</sup> == a&lt;&lt;i</p><p></p><p>&#x4F8B;&#xFF1A; 29 (11101) * 7(111)</p><p>= (1*2<sup>4</sup> + 1*2<sup>3</sup> + 1*2<sup>2</sup> + 0*2<sup>1</sup> + 1*2<sup>0</sup>) * (1* 2<sup>2</sup> + 1* 2<sup>1</sup> + 1* 2<sup>0</sup>)</p><p>= (29&lt;&lt;2) + (29&lt;&lt;1) + 29</p><pre><code class="language-java">public Class MultiplyByShift {
    public static int multiply(int a, int b){
    	// &#x5047;&#x8BBE;&#x6CA1;&#x6709;&#x6EA2;&#x51FA;
        
        int res = 0;
        boolean negative = (a^b)&lt;0; // &#x5F02;&#x6216;&#x540E;&#x53EA;&#x770B;&#x6700;&#x9AD8;&#x4F4D;&#xFF08;&#x7B26;&#x53F7;&#x4F4D;&#xFF09;
        // &#x82E5;a,b&#x4E2D;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x8D1F;&#x6570;&#xFF0C;&#x5219;&#x6700;&#x9AD8;&#x4F4D;0^1 = 1&#xFF0C;&#x7ED3;&#x679C;&#x4E3A;&#x8D1F;
        
        int m = a&lt;0? -a: a;
        int n = b&lt;0? -b: b;
        
        int i = 0;
        
        while(n&gt;0){
            int t = n &amp; 1; // n&#x7684;&#x6700;&#x540E;&#x4E00;&#x4F4D;
            n &gt;&gt;= 1;
            if(t==1) res += (m&lt;&lt;i);
            i++;
        }
        
        return negative? -res: res;
    }
}</code></pre><h2 id="%E9%99%A4%E6%B3%95%E5%92%8C%E5%8F%B3%E7%A7%BB">&#x9664;&#x6CD5;&#x548C;&#x53F3;&#x79FB;</h2><p>&#x4F8B;&#xFF1A;101(1100101)/4(10)</p><p>= (1*2<sup>6</sup> + 1*2<sup>5</sup> + 0*2<sup>4</sup> + 0*2<sup>3</sup> + 1*2<sup>2</sup> + 0*2<sup>1</sup> + 1*2<sup>0</sup>) / 2<sup>2</sup></p><p>= 1* 2<sup>4</sup> + 1*2<sup>3</sup> + 0*2<sup>2</sup> + 0*<em>2<sup>1</sup> + 1*</em>2<sup>0</sup> + 0*2<sup>-1</sup> + 0*2<sup>-2</sup></p><p>&#x5728;&#x6574;&#x6570;&#x8303;&#x56F4;&#x4E3A; 1* 2<sup>4</sup> + 1*2<sup>3</sup> + 0*2<sup>2</sup> + 0*<em>2<sup>1</sup> + 1*</em>2<sup>0</sup> &#x5373;11001</p><p>&#x6240;&#x4EE5;a/2<sup>b</sup> = a&gt;&gt;b</p><p></p><p>&#x901A;&#x8FC7;&#x4F4D;&#x79FB;&#x6765;&#x505A;&#x9664;&#x6CD5;&#x65F6;&#xFF0C;&#x5982;&#x679C;&#x9664;&#x6570;&#x4E0D;&#x662F;2&#x7684;&#x5E42;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F9D;&#x6B21;&#x8BD5;&#x63A2;&#x6BCF;&#x4E00;&#x4F4D;</p><p>&#x4F8B;&#xFF1A;101(1100101)/13(1101)</p><p>13(1101)&#x53F3;&#x79FB;1&#x4F4D;&#xFF0C;26(11010)&lt;=101; </p><p>&#x53F3;&#x79FB;2&#x4F4D;, 52(110100)&lt;=101; </p><p>&#x53F3;&#x79FB;3&#x4F4D;&#xFF0C;104&gt;101;</p><p>&#x6240;&#x4EE5;&#x7ED3;&#x679C;&#x7B2C;3&#x4F4D;&#x662F;0&#xFF0C;&#x7B2C;2&#x4F4D;&#x662F;1</p><p>&#x628A;101&#x62C6;&#x6210; (1*13*2<sup>2</sup>) + 49&#xFF0C;&#x7EE7;&#x7EED;&#x8BD5;&#x63A2;49</p><p>49 = (1*13*2<sup>1</sup>)+ 23, &#xA0;23 = (1*13*2<sup>0</sup> +22)</p><p>&#x6240;&#x4EE5;101/13 = &#x4E8C;&#x8FDB;&#x5236;&#x7684;111&#xFF0C;&#x5373;7</p><pre><code class="language-java">public class DivideByShift {
    public static void main(String[] args) {
        System.out.println(divide(1234567,123)+&quot;,&quot;+1234567/123);
    }
    
    public static int divide(int a, int b){
    	// &#x5047;&#x8BBE;&#x6CA1;&#x6709;&#x6EA2;&#x51FA;
        boolean negative = (a^b)&lt;0;
        int m = a&lt;0? -a:a;
        int n = b&lt;0? -b:b;
        
        int res = 0;
        
        while(m&gt;=n){
            int i=0;
            while((n&lt;&lt;i) &lt;=m) i++;
            i--;
            res += (1&lt;&lt;i);
            m -= (n &lt;&lt;i);
        }
        
        return negative? -res: res;
    }
}</code></pre><h2 id="%E8%B0%88%E8%B0%88%E6%BA%A2%E5%87%BA%EF%BC%88java%E7%9A%84%E6%83%85%E5%86%B5%EF%BC%89">&#x8C08;&#x8C08;&#x6EA2;&#x51FA;&#xFF08;Java&#x7684;&#x60C5;&#x51B5;&#xFF09;</h2><p>Java&#x7684;Integer&#x7684;&#x53D6;&#x503C;&#x8303;&#x56F4;&#x662F;-2147483648&#x5230;2147483647&#x3002;</p><h3 id="%E5%90%91%E4%B8%8B%E6%BA%A2%E5%87%BA">&#x5411;&#x4E0B;&#x6EA2;&#x51FA;</h3><p>Integer.MIN_VALUE&#xFF0C;&#x5373;-2147483638&#xFF0C;&#x662F;&#x4E2A;&#x6BD4;&#x8F83;&#x70E6;&#x7684;&#x6570;&#xFF0C;&#x56E0;&#x4E3A;&#x53D6;&#x8D1F;&#x5B83;&#x4F1A;&#x6EA2;&#x51FA;&#x3002;Integer.MIN_VALUE = -Integer.MIN_VALUE = -2147483638.... &#x6240;&#x4EE5;&#x60F3;&#x8981;&#x5B58;&#x5B83;&#x7684;&#x7EDD;&#x5BF9;&#x503C;&#xFF0C;&#x5C31;&#x53EA;&#x597D;&#x7528;Long&#x4E86;&#x3002;</p><p>&#x6B63;&#x786E;&#x7684;&#x5F97;&#x5230;Integer.MIN_VALUE&#x7684;&#x529E;&#x6CD5;&#xFF1A;</p><pre><code class="language-java">Long abs = -(Long)Integer.MIN_VALUE; // &#x4E00;&#x5B9A;&#x8981;&#x5148;&#x8F6C;&#x6362;&#x6210;Long&#x518D;&#x53D6;&#x8D1F;</code></pre><p>Java&#x91CC;&#xFF0C;&#x8D1F;&#x6570;&#x79FB;&#x4F4D;&#x4E0D;&#x4F1A;&#x53D8;&#x6210;&#x6B63;&#x6570;&#xFF0C;&#x4F46;&#x662F;&#x53EF;&#x80FD;&#x53D8;&#x6210;0</p><h3 id="%E5%90%91%E4%B8%8A%E6%BA%A2%E5%87%BA">&#x5411;&#x4E0A;&#x6EA2;&#x51FA;</h3><p>&#x4F7F;&#x7528;*&#x505A;&#x4E58;&#x6CD5;&#x65F6;&#xFF0C;&#x5148;&#x4E58;&#x518D;&#x5224;&#x65AD;&#x662F;&#x5426;&#x7B26;&#x53F7;&#x53D8;&#x5316;&#x662F;&#x4E0D;&#x9760;&#x8C31;&#x7684;&#xFF0C;&#x56E0;&#x4E3A;&#x4E58;&#x4E00;&#x6B21;&#x53EF;&#x80FD;&#x4EA7;&#x751F;2&#x6B21;&#x6EA2;&#x51FA;&#x62B5;&#x6D88;&#x7B26;&#x53F7;&#x53D8;&#x5316;&#x3002;</p><p>&#x4F7F;&#x7528;&#x5DE6;&#x79FB;&#x548C;+&#x505A;&#x4E58;&#x6CD5;&#x65F6;&#xFF0C;&#x6BCF;&#x6B21;&#x76F8;&#x52A0;&#x540E;&#x68C0;&#x67E5;&#x7B26;&#x53F7;&#x4F4D;&#x6539;&#x53D8;&#x662F;&#x53EF;&#x4EE5;&#x7684;&#x3002;</p>]]></content:encoded></item><item><title><![CDATA[Data Structure (5) - Hash Table]]></title><description><![CDATA[<h1 id="hash-table">Hash Table</h1><p>A hash table has:</p><ul><li>A hash function h(k)</li><li>An array</li><li>Something that handles collisions and else</li></ul><p>Dictionary in Python is a hash table.</p><h2 id="hash-function">Hash Function</h2><p>A function that maps keys from key-space to an integer bounded by array size.</p><p>A hash function consists of 2 parts:</p><ol><li>Takes</li></ol>]]></description><link>https://blog.fluere.cloud/data-structure-part5-hashing/</link><guid isPermaLink="false">615d8e16c76a54582562dcd0</guid><category><![CDATA[C++]]></category><category><![CDATA[数据结构]]></category><dc:creator><![CDATA[栗子℃（EN）]]></dc:creator><pubDate>Sun, 26 Sep 2021 11:58:00 GMT</pubDate><content:encoded><![CDATA[<h1 id="hash-table">Hash Table</h1><p>A hash table has:</p><ul><li>A hash function h(k)</li><li>An array</li><li>Something that handles collisions and else</li></ul><p>Dictionary in Python is a hash table.</p><h2 id="hash-function">Hash Function</h2><p>A function that maps keys from key-space to an integer bounded by array size.</p><p>A hash function consists of 2 parts:</p><ol><li>Takes key and produce some integer</li><li>A compression: shrinks large integer into range of array &#xA0;(almost always mod N)</li></ol><p>Characteristics of a good hash function:</p><ul><li>O(1) computation time</li><li>Deterministic (it is not random): for k<sub>1</sub> = k<sub>2</sub> , h(k<sub>1</sub>) = h(k<sub>2</sub>)</li><li>Satisfy SUHA (Simple Uniform Hashing Assumption)</li></ul><p>SUHA:</p><ul><li>Keys are distributed uniformly in array. </li><li>For i &#x2260; j, P(h(i)=h(j)) = 1/N, where N is the size of array (choosing index for i like randomly choosing an index of the array). </li><li>(Hash by mod is not SUHA)</li></ul><h2 id="collision-handling">Collision Handling</h2><h3 id="separate-chaining-open-hashing">Separate Chaining (open hashing)</h3><p>Store linked lists (a pointer) in array. </p><p>For example: |S|=n, |Array| = N</p><figure class="kg-card kg-image-card"><img src="https://strapi.fluere.cloud/uploads/open_hashing_collision_handling_separate_chaining_94238c8781.png" class="kg-image" alt loading="lazy"></figure><p>When collide, create a new node in the list in array[h(key)].</p><p>When hashing by mod: </p><ul><li>Insert: O(1)</li><li>Find/Remove: O(n) &#xA0;(Find/Remove: worst case collision)</li></ul><p>When hashing satisfy SUHA:</p><ul><li>insert(O1)</li><li>Find/Remove: O(n/N). </li></ul><p><strong>n/N</strong> is called load factor, it describes how efficiently the array is used.</p><h3 id="probe-based-hashing-closed-hashing">Probe-based Hashing (closed hashing)</h3><p>Use linear probing: when collide, just put into the next available space. </p><p>To make every key into array, size of array size should be no smaller than size of key space.</p><p>When find, calculate h(key) and try the value at that index. If it is not equal to key, find next, loop until finding the key in the array.</p><p>When hashing by mode:</p><ul><li>Insert: O(n)</li><li>Find/Remove: O(n)</li></ul><p>When SUHA:</p><ul><li>Insert: O(n/N)</li><li>Find/Remove: O(n/N)</li></ul><p>(Basically, no one uses this...)</p><h3 id="double-hashing-closed-hashing">Double Hashing (closed hashing)</h3><p>If hashing once, collide, hash again, until finding available space.</p><ul><li>S = {k<sub>1</sub>, k<sub>2</sub>, ... , k<sub>n</sub>}, &#xA0;|S| = n</li><li>|Array| = N</li><li>h<sub>1</sub>(k) = k % N</li><li>h<sub>2</sub> = 5 - (k%5)</li><li>h(k,i) = (h<sub>1</sub>(k) + i*h<sub>2</sub>(k))%N, where i is the number of tries before finding available space</li><li>N and all h<sub>2</sub> values should share no factors. Thus, we&apos;d like N to be a prime</li></ul><h3 id="comparisons">Comparisons</h3><p>The expected number of probes for find(key) under SUHA, </p><p>where |Key Space| = n, |Array| = N, &#xA0;</p><p>and a is load factor: (n/N)</p><p>Linear Probing:</p><ul><li>Successful:		(1+1/(1-a))/2</li><li>Unsuccessful:	(1+1/(1-a))<sup>2</sup>/2</li></ul><p>Double Hashing:</p><ul><li>Successful:		1/a * ln(1/(1-a))</li><li>Unsuccessful:	1/(1-a)</li></ul><p>Separate Chaining:</p><ul><li>Successful:		1+a/2</li><li>Unsuccessful:	1+a</li></ul><p>When heavily loaded, avoid linear probing and double hashing. When hash is SUHA and load factor is less than 0.75 (75% of the array space used), then linear probing and double hashing are pretty fast (double hashing is still generally faster than linear probing when a is under 0.75). When light loaded, these 2 can be faster than separate chaining.</p><p>When hash function is bad (not SUHA), usually separate chaining is better.</p><h2 id="rehashing">ReHashing</h2><p>When having too many keys, and the array is filled (n/N&gt;some constant value), we could expand array length to 2*N or next prime.</p><p>When changing N, as hash function is updated, we need to calculate new indexes for old keys in the new (expanded) array. This is called rehashing.</p><p>Rehashing takes O(n), but the chances we need to rehash is O(1) amortized, expected, given SUHA hashing.</p><h2 id="summary">Summary</h2><h3 id="better-collision-strategy">Better Collision Strategy</h3><p>Big records: separate chaining</p><p>Structure speed: double hashing</p><h3 id="constraints-exist-on-hashing-but-not-bsts">Constraints exist on hashing but not BSTs</h3><ul><li>Amortized</li><li>Probalistic runtime</li><li>SUHA</li></ul><p>Hash table only support exact matches, while BST supports range find.</p><p>In C++, map is implemented with red-black tree (it supports range find), and unordered_map is implemented with hashing.</p><p></p><p>Ref: </p><ul><li>https://courses.engr.illinois.edu/cs225/fa2020/</li><li><a href="https://www.bilibili.com/video/BV1454y127Lk?p=26">https://www.bilibili.com/video/BV1454y127Lk?p=26</a></li></ul>]]></content:encoded></item><item><title><![CDATA[Data Structure (4) - Kd-Tree & B-Tree]]></title><description><![CDATA[<h1 id="kd-tree">Kd-Tree</h1><p>Short for k-dimension-tree. When search is not enough in one dimension, Kd-Trees may be used.</p><p>e.g. Search in 2D. Given x<sub>1</sub>, x<sub>2</sub>, y<sub>1</sub>, y<sub>2</sub>, find all points within the range.</p><p>If we don&apos;t want to go through all of the points &#x2013;</p>]]></description><link>https://blog.fluere.cloud/data-structure-part3-kd-tree-b-tree/</link><guid isPermaLink="false">615d578dc76a54582562db87</guid><dc:creator><![CDATA[栗子℃（EN）]]></dc:creator><pubDate>Thu, 23 Sep 2021 07:59:00 GMT</pubDate><content:encoded><![CDATA[<h1 id="kd-tree">Kd-Tree</h1><p>Short for k-dimension-tree. When search is not enough in one dimension, Kd-Trees may be used.</p><p>e.g. Search in 2D. Given x<sub>1</sub>, x<sub>2</sub>, y<sub>1</sub>, y<sub>2</sub>, find all points within the range.</p><p>If we don&apos;t want to go through all of the points &#x2013; O(n) time</p><p>then, we could have a tree, where each root for sub-tree splits points in one axis.</p><figure class="kg-card kg-image-card kg-card-hascaption"><img src="https://strapi.fluere.cloud/uploads/tree_kd_tree_example_80b48f6451.png" class="kg-image" alt loading="lazy"><figcaption>example: Kd-Tree</figcaption></figure><p>For the same sets of points, there are multiple ways to build a Kd-Tree. </p><p>For the example above, P<sub>1</sub> splits y-axis. All nodes in sub-tree on left are with Y less than Y<sub>P<sub>1</sub></sub> , and all nodes in sub-tree on right are with Y larger than Y<sub>P<sub>1</sub></sub> .</p><h1 id="b-tree">B-Tree</h1><h2 id="motivation">Motivation</h2><p>When data set is huge, we can&apos;t always expect that we could fill all of them in main memory. They could be stored in disks, cloud, pingfs, etc.</p><p>If we have large seek time for data (e.g. all nodes are on different servers so the time going from one node to another is significant), then we would want to minimize the number of reads. In order to do that:</p><ul><li>Tree must be short</li><li>Find relevant data</li><li>Work local to a node is fast &#x2013;&gt; Do more things in a single node and decide where to go next. Have more children and larger nodes.</li></ul><h2 id="definition">Definition</h2><p>A B-Tree of order m:</p><ul><li>Is an m-way tree </li><li>Will have (m-1) or less keys and 0~m children per node</li><li>All keys within a node are ordered (sorted array)</li><li>All leaves hold no more than (m-1) keys</li><li>All internal nodes have exactly one more child than keys</li><li>Root node can be a leaf or have [2,m] children</li><li>All non-root, internal nodes have [ceil(m/2),m] children</li><li>All leaves are on the same level</li><li>Height is O(log<sub>m</sub>(n)) &#x2013; meaning the number of seeks will be O(log<sub>m</sub>(n))</li></ul><p>Build a tree that uses [1 network packet (TCP 1500 bytes)] / [1 disk block (512/8192 bytes)] per node.</p><figure class="kg-card kg-image-card"><img src="https://strapi.fluere.cloud/uploads/b_tree_of_order_4_example_33f0df87ed.png" class="kg-image" alt loading="lazy"></figure><p>Example of B-Tree operations: <a href="https://www.cs.usfca.edu/~galles/visualization/BTree.html">https://www.cs.usfca.edu/~galles/visualization/BTree.html</a></p><h2 id="b-tree-insertion">B-Tree Insertion</h2><p>When a B-Tree node reaches m keys:</p><p>Take the key in middle, push it up. Create 2 new nodes from the rest of this node and link to the one we just pushed up. Then insert.</p><p>B-Trees are built bottom up. When reaching m keys, they push nodes up.</p><p><strong>Leaves of B-Tree are all at the same level.</strong></p><p>B-Trees are perfect trees (perfect tree: all leaves have the same distance to root).</p><p>Leaves in B-Tree has at least ceil(m/2)-1 keys.</p><h2 id="b-tree-search">B-Tree Search</h2><pre><code class="language-cpp">bool BTree::_exists(BTree &amp;node, const K &amp;key) {
    unsigned i;
    for( i=0; i&lt;node.keys_ct_ &amp;&amp; key&lt;node.keys_[i]; i++) {
        // i &lt; number of keys in node
        // find the first i, where key &gt;= node.keys[i]
    }
    
    if(i&lt;node.keys_ct &amp;&amp; key == node.keys_[i]) {
        // if i is not out of the array, check if we&apos;ve found the key
    	return true;
    }
    
    if(node.isLeaf()) { // base of recursion
        // if this is a leaf, and key is not here
        return false;
    } else {
        BTreeNode nextChild = node._fetchChild(i);
        // child could be in cloud -- anyway, handle it with the func
        return _exists(nextChild, key);
    }
    
}</code></pre><p></p><p></p><p></p><p>ref: https://courses.engr.illinois.edu/cs225/fa2020/</p><p>ref: <a href="https://www.bilibili.com/video/BV1454y127Lk">https://www.bilibili.com/video/BV1454y127Lk</a></p>]]></content:encoded></item><item><title><![CDATA[在博客养一只猫 -- Live2D SDK 4.x 探索]]></title><description><![CDATA[<p>&#x63D0;&#x524D;&#x58F0;&#x660E;&#xFF0C;&#x8FD9;&#x4E0D;&#x80FD;&#x5F53;&#x6559;&#x7A0B;&#xFF0C;&#x53EA;&#x662F;&#x4E00;&#x4EFD;&#x7B14;&#x8BB0;&#xFF0C;&#x8FD8;&#x6709;&#x5410;&#x69FD;&#x3002;</p><p>&#x641E;&#x4E00;&#x53EA;&#x770B;&#x7248;&#x732B;&#x5230;&#x535A;&#x5BA2;&#x5C45;&#x7136;&#x6BD4;&#x641E;&#x4E00;&#x53EA;&#x770B;&#x677F;&#x5A18;&#x8FD8;&#x8981;&#x9EBB;&#x70E6;&#x2026;&#x2026;&#x56E7;</p><p>&#x867D;</p>]]></description><link>https://blog.fluere.cloud/get-a-cat-for-this-blog1/</link><guid isPermaLink="false">615bd233c76a54582562d656</guid><category><![CDATA[笔记]]></category><category><![CDATA[折腾VPS]]></category><category><![CDATA[Web]]></category><dc:creator><![CDATA[栗子℃]]></dc:creator><pubDate>Wed, 22 Sep 2021 04:29:00 GMT</pubDate><content:encoded><![CDATA[<p>&#x63D0;&#x524D;&#x58F0;&#x660E;&#xFF0C;&#x8FD9;&#x4E0D;&#x80FD;&#x5F53;&#x6559;&#x7A0B;&#xFF0C;&#x53EA;&#x662F;&#x4E00;&#x4EFD;&#x7B14;&#x8BB0;&#xFF0C;&#x8FD8;&#x6709;&#x5410;&#x69FD;&#x3002;</p><p>&#x641E;&#x4E00;&#x53EA;&#x770B;&#x7248;&#x732B;&#x5230;&#x535A;&#x5BA2;&#x5C45;&#x7136;&#x6BD4;&#x641E;&#x4E00;&#x53EA;&#x770B;&#x677F;&#x5A18;&#x8FD8;&#x8981;&#x9EBB;&#x70E6;&#x2026;&#x2026;&#x56E7;</p><p>&#x867D;&#x8BF4;&#x6700;&#x540E;&#x732B;&#x662F;&#x653E;&#x51FA;&#x6765;&#x4E86;&#xFF0C;&#x4F46;&#x516B;&#x6210;&#x8FD8;&#x662F;&#x54EA;&#x91CC;&#x6CA1;&#x7528;&#x5BF9;&#x5427;&#xFF0C;&#x7F16;&#x8BD1;&#x51FA;&#x6765;&#x52A8;&#x8F84;&#x56DB;&#x4E94;&#x767E;kb&#x7684;js&#x6587;&#x4EF6;&#xFF08;&#x867D;&#x7136;&#x786E;&#x5B9E;&#x662F;&#x6CA1;&#x6709;&#x7279;&#x522B;&#x72E0;&#x7684;&#x538B;&#x7F29;&#xFF0C;uglify&#x5168;&#x5F00;&#x4E4B;&#x540E;&#x8FD0;&#x884C;&#x4F1A;&#x51FA;&#x9519;&#x2026;&#x2026;&#xFF09;&#xFF0C;&#x8FD8;&#x6709;&#x5B98;&#x65B9;&#x539F;&#x7248;Demo&#x76F4;&#x63A5;&#x7F16;&#x8BD1;&#x51FA;&#x7684;1.6MB js+sourcemap&#xFF08;&#x4E0D;&#x542B;&#x6A21;&#x578B;&#x4E0E;&#x56FE;&#x7247;&#xFF09;&#xFF0C;&#x600E;&#x4E48;&#x770B;&#x600E;&#x4E48;&#x522B;&#x626D;&#xFF08;&#x6342;&#x8138;&#xFF09;&#x2026;&#x2026; &#x8FD9;&#x4E2A;&#x53EA;&#x6709;&#x77ED;&#x77ED;5&#x9875;&#x7684;<a href="https://docs.live2d.com/cubism-sdk-tutorials/sample-build-web/">&#x5B98;&#x65B9;&#x6587;&#x6863;</a>&#x4E5F;&#x662F;&#x6CA1;&#x8BF4;&#x591A;&#x5C11;&#x4E1C;&#x897F;&#xFF0C;&#x4E5F;&#x53EF;&#x80FD;&#x662F;&#x56E0;&#x4E3A;&#x5927;&#x90E8;&#x5206;&#x7528;&#x6237;&#x90FD;&#x662F;&#x5F00;&#x53D1;&#x624B;&#x673A;&#x539F;&#x751F;&#x5E94;&#x7528;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x7F51;&#x9875;&#x7684;&#x5427;&#x3002;</p><p>&#x8981;&#x627E;&#x6559;&#x7A0B;&#x7684;&#x8BDD;&#xFF0C;&#x5F80;&#x8FD9;&#x8FB9;&#x8D70;&#x5427;~</p><p>&#x5176;&#x4ED6;&#x4EBA;&#x52AA;&#x529B;&#x7814;&#x7A76;&#x7684;v4 SDK&#x6559;&#x7A0B;&#xFF1A;</p><ul><li><a href="https://blog.csdn.net/qq_37735413/article/details/119413744">https://blog.csdn.net/qq_37735413/article/details/119413744</a> (&#x8BA9;&#x6211;&#x610F;&#x8BC6;&#x5230;&#x4E86;&#x6A21;&#x578B;&#x4F7F;&#x7528;&#x975E;&#x6807;&#x51C6;&#x53C2;&#x6570;&#x540D;&#x7684;&#x95EE;&#x9898;)</li></ul><p>&#x8FD8;&#x4E0D;&#x6E05;&#x695A;&#x662F;&#x54EA;&#x4E2A;&#x7248;&#x672C;&#xFF0C;&#x4F46;&#x662F;&#x6587;&#x6863;&#x94FE;&#x63A5;&#x5F88;&#x591A;&#xFF1A;</p><ul><li><a href="https://github.com/stevenjoezhang/live2d-widget">https://github.com/stevenjoezhang/live2d-widget</a></li></ul><p>&#x5E94;&#x8BE5;&#x662F;&#x65E7;&#x7248;&#x672C;SDK&#x7684;&#x5E94;&#x7528;&#xFF1A;</p><ul><li><a href="https://blog.csdn.net/qq_37735413/article/details/104769280">https://blog.csdn.net/qq_37735413/article/details/104769280</a> </li><li><a href="https://github.com/EYHN/hexo-helper-live2d">https://github.com/EYHN/hexo-helper-live2d</a></li></ul><h1 id="live2d">Live2D</h1><p>&#x5C31;&#x662F;&#x76EE;&#x524D;&#x7684;&#x5F88;&#x591A;&#x624B;&#x673A;&#x6E38;&#x620F;&#x91CC;2D &#x52A8;&#x6001;&#x7ACB;&#x7ED8;&#x80CC;&#x540E;&#x7684;&#x90A3;&#x4E2A;&#x6280;&#x672F;&#xFF0C;&#x6216;&#x8005;&#x8BF4;&#xFF0C;&#x4E00;&#x5957;&#x4E13;&#x95E8;&#x5E94;&#x5BF9;&#x6D3B;&#x52A8;&#x5E45;&#x5EA6;&#x8F83;&#x5C0F;&#x7684;&#x7C7B;&#x751F;&#x7269;&#x7684;2D &#x52A8;&#x753B;&#x751F;&#x6210;&#x6280;&#x672F;&#x3002;&#x6211;&#x7684;&#x76F4;&#x89C2;&#x5370;&#x8C61;&#x662F;&#x4E00;&#x5957;&#x53EF;&#x4EE5;&#x7075;&#x6D3B;&#x63A7;&#x5236;&#x7684;&#x5F62;&#x53D8;&#x7CFB;&#x7EDF; + &#x7269;&#x7406; + &#x6A21;&#x578B;&#x548C;&#x52A8;&#x4F5C;&#x7BA1;&#x7406;&#x3002;</p><!--kg-card-begin: html--><figure>
<img src="https://strapi.fluere.cloud/uploads/live2_D_editor_hijiki_802cbaccb5.png">
    <figurecaption>Live2D Editor &amp; Sample Data (hijiki)</figurecaption>
</figure><!--kg-card-end: html--><p>Live2D &#x5B98;&#x7F51;&#x5927;&#x591A;&#x7684;&#x6A21;&#x578B;&#x6837;&#x4F8B;&#xFF08;&#x9664;&#x4E86;&#x6709;&#x4E09;&#x65B9;&#x7248;&#x6743;&#x7684;&#xFF09;&#x5927;&#x591A;&#x662F;&#x5BF9;&#x4E00;&#x822C;&#x4F7F;&#x7528;&#x8005;&#x548C;&#x5C0F;&#x578B;&#x516C;&#x53F8;&#x514D;&#x8D39;&#x7684;&#x3002;&#x8FD9;&#x53EA;&#x732B;&#x5C31;&#x662F;tororo&amp;hijiki&#x5305;&#x91CC;&#x7684;&#x5176;&#x4E2D;&#x4E00;&#x53EA;&#x3002;</p><h2 id="%E4%BD%BF%E7%94%A8">&#x4F7F;&#x7528;</h2><p>&#x4F7F;&#x7528;Live2D&#x9700;&#x8981;&#x6709;&#xFF1A;</p><ol><li>&#x6A21;&#x578B;&#xFF1A; &#x5305;&#x62EC;texture&#x3001;&#x90E8;&#x4EF6;&#x3001;&#x53C2;&#x6570;&#xFF08;&#x4E0A;&#x9762;&#x7ED1;&#x6709;&#x4E0D;&#x540C;&#x90E8;&#x4EF6;&#x7684;&#x5173;&#x952E;&#x5E27;&#xFF09;&#x3001;&#x52A8;&#x4F5C;&#xFF08;&#x90E8;&#x4EF6;&#x3001;&#x7EC4;&#x4EF6;&#x53C2;&#x6570;&#x53D8;&#x5316;&#xFF09;</li><li>SDK</li></ol><h2 id="%E6%A8%A1%E5%9E%8B">&#x6A21;&#x578B;</h2><p>Runtime&#x91CC;&#x7684;model3.json&#x957F;&#x8FD9;&#x4E2A;&#x6837;&#x5B50;&#xFF1A;</p><!--kg-card-begin: html--><img src="https://strapi.fluere.cloud/uploads/live2_D_model_runtime_sample_014c54c4ae.png"><!--kg-card-end: html--><p>&#x5176;&#x4E2D;.2048&#x6587;&#x4EF6;&#x5939;&#x91CC;&#x662F;&#x6750;&#x8D28;png&#x56FE;&#x7247;&#xFF0C;</p><p>motion&#x6587;&#x4EF6;&#x5939;&#x662F;&#x6BCF;&#x4E2A;&#x52A8;&#x4F5C;&#x7684;json&#xFF08;&#x6BCF;&#x4E2A;&#x52A8;&#x4F5C;&#xFF0C;&#x5404;&#x90E8;&#x4EF6;&#x7F51;&#x683C;&#x9876;&#x70B9;&#x4F4D;&#x7F6E;&#x53D8;&#x5316;&#x7B49;&#xFF09;&#xFF0C;</p><p>cdi3&#x662F;&#x53C2;&#x6570;&#x7684;&#x63CF;&#x8FF0;&#x7684;&#x6837;&#x5B50;&#xFF08;&#x4F46;&#x4E5F;&#x5E76;&#x4E0D;&#x662F;&#x5230;default parameter&#x7684;mapper...&#xFF09;&#xFF0C;</p><p>moc3&#x662F;&#x6A21;&#x578B;&#x6587;&#x4EF6;&#xFF0C;&#x91CC;&#x9762;&#x5305;&#x542B;&#x90E8;&#x4EF6;&#x548C;&#x53C2;&#x6570;&#x7684;&#x8BBE;&#x5B9A;&#xFF0C;</p><p>model3&#x662F;&#x5BF9;&#x6A21;&#x578B;&#x7684;&#x63CF;&#x8FF0;&#x548C;&#x914D;&#x7F6E;&#xFF0C;</p><p>physics3&#x662F;&#x4E00;&#x4E9B;&#x7269;&#x7406;&#xFF08;&#x5982;&#x5934;&#x53D1;&#x6446;&#x52A8;&#xFF1F;&#xFF09;&#x548C;&#x52A8;&#x753B;&#x7684;&#x53C2;&#x6570;&#xFF0C;</p><p>pose3&#x4F3C;&#x4E4E;&#x662F;&#x540C;&#x4E00;&#x90E8;&#x4F4D;&#x7684;&#x591A;&#x4E2A;&#x56FE;&#x7247;&#xFF08;&#x5982;&#x65E0;&#x6CD5;&#x9760;&#x5355;&#x4E00;&#x56FE;&#x7247;&#x5F62;&#x53D8;&#x505A;&#x5230;&#x7684;&#xFF0C;&#x540C;&#x4E00;&#x90E8;&#x4EF6;&#x7684;&#x4E0D;&#x540C;&#x72B6;&#x6001;&#xFF09;</p><h2 id="%E6%9D%82%E8%B0%88">&#x6742;&#x8C08;</h2><p>&#x8BF4;&#x5230;&#x52A8;&#x753B;&#xFF0C;&#x5C31;&#x60F3;&#x8DDF;&#x6B7B;&#x6389;&#x7684;Flash&#x6BD4;&#x8F83;&#x4E00;&#x4E0B;&#x3002;&#x5C0F;&#x5B66;&#x7684;&#x65F6;&#x5019;&#xFF0C;Flash&#x5728;&#x6211;&#x5FC3;&#x76EE;&#x4E2D;&#x7684;&#x4E00;&#x5927;&#x9ED1;&#x79D1;&#x6280;&#x5C31;&#x662F;&#x8865;&#x95F4;&#x52A8;&#x753B;&#x3002;&#x7ED9;&#x51FA;&#x5F62;&#x72B6;A&#x548C;&#x5F62;&#x72B6;B&#xFF0C;&#x8BA1;&#x7B97;&#x4ECE;A&#x53D8;&#x6210;B&#x4E4B;&#x95F4;&#x7684;&#x8FC7;&#x7A0B;&#x5E76;&#x81EA;&#x52A8;&#x586B;&#x8865;&#x5173;&#x952E;&#x5E27;&#x3002;&#x4F46;&#x9664;&#x975E;&#x56FE;&#x5F62;&#x89C4;&#x5219;&#xFF0C;&#x8FD9;&#x4E2A;&#x751F;&#x6210;&#x7684;&#x8865;&#x95F4;&#x5BF9;&#x7528;&#x6237;&#x6765;&#x8BF4;&#x57FA;&#x672C;&#x662F;&#x4E0D;&#x53EF;&#x63A7;&#x7684;&#x3002;&#x4E2D;&#x95F4;&#x8FC7;&#x7A0B;&#x53EF;&#x80FD;&#x751F;&#x6210;&#x5F97;&#x5F88;&#x81EA;&#x7136;&#xFF0C;&#x4E5F;&#x53EF;&#x80FD;&#x5148;&#x4ECE;A&#x626D;&#x66F2;&#x6210;&#x5947;&#x602A;&#x7684;&#x56FE;&#x6848;&#xFF0C;&#x518D;&#x53D8;&#x6210;&#x6B63;&#x5E38;&#x7684;B&#x3002;&#x5F53;&#x7136;&#xFF0C;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#xFF0C;&#x5927;&#x5BB6;&#x4E00;&#x822C;&#x4F1A;&#x5220;&#x4E86;&#x8865;&#x95F4;&#xFF0C;&#x5728;&#x4E2D;&#x95F4;&#x52A0;&#x51E0;&#x4E2A;&#x7F13;&#x51B2;&#x7684;&#x5173;&#x952E;&#x5E27;&#xFF08;&#x6211;&#x4EEC;&#x9884;&#x671F;&#x7684;&#x4E2D;&#x95F4;&#x72B6;&#x6001;&#xFF09;&#xFF0C;&#x8BA9;&#x5E27;&#x4E0E;&#x5E27;&#x4E4B;&#x95F4;&#x7684;&#x5DEE;&#x5F02;&#x66F4;&#x5C0F;&#xFF0C;&#x53D8;&#x5316;&#x66F4;&#x89C4;&#x5219;&#xFF0C;&#x518D;&#x521B;&#x5EFA;&#x8865;&#x95F4;&#x3002;Live2D &#x8FD9;&#x8FB9;&#xFF0C;&#x901A;&#x8FC7;&#x5728;&#x56FE;&#x5F62;&#x4E0A;&#x521B;&#x5EFA;&#x7531;&#x591A;&#x4E2A;&#x4E09;&#x89D2;&#x5F62;&#x7EC4;&#x6210;&#x7684;&#x591A;&#x8FB9;&#x5F62;&#x7F51;&#x683C;&#xFF08;&#x4E00;&#x5806;&#x4E09;&#x89D2;&#x5F62;&#x53EF;&#x4EE5;&#x7EC4;&#x5408;&#x51FA;&#x5404;&#x79CD;&#x591A;&#x8FB9;&#x5F62;&#xFF09;&#xFF0C;&#x62D6;&#x62FD;&#x7F51;&#x683C;&#x4E0A;&#x8282;&#x70B9;&#x9020;&#x6210;&#x5F62;&#x53D8;&#x751F;&#x6210;&#x5173;&#x952E;&#x5E27;&#x3002;&#x7136;&#x540E;&#x56E0;&#x4E3A;&#x8FD9;&#x4E9B;&#x5F62;&#x53D8;&#x90FD;&#x88AB;&#x8FD9;&#x4E9B;&#x8282;&#x70B9;&#x7684;&#x79FB;&#x52A8;&#x63CF;&#x8FF0;&#x4E86;&#xFF0C;&#x8865;&#x95F4;&#x7684;&#x751F;&#x6210;&#x5C31;&#x4F1A;&#x5F88;&#x89C4;&#x5219;&#x3002;</p><p>&#x5370;&#x8C61;&#x4E2D;&#x4E0E;Flash&#x51E0;&#x4E4E;&#x540C;&#x671F;&#x7684;&#xFF0C;&#x8FD8;&#x6709;&#x4E00;&#x4E2A;&#x57FA;&#x4E8E;&#x9AA8;&#x9ABC;&#x751F;&#x6210;&#x8865;&#x95F4;&#x7684;Moho&#x3002;&#x67E5;&#x4E86;&#x4E0B;&#x5B83;&#x8FD8;&#x597D;&#x597D;&#x6D3B;&#x7740;&#x2026;&#x2026;&#x662F;&#x5728;&#x56FE;&#x7247;&#x4E0A;&#x521B;&#x5EFA;&#x5E76;&#x7ED1;&#x5B9A;&#x9AA8;&#x67B6;&#xFF0C;&#x901A;&#x8FC7;&#x62D6;&#x62FD;&#x9AA8;&#x67B6;&#x9876;&#x70B9;&#x5BF9;&#x56FE;&#x7247;&#x8FDB;&#x884C;&#x5F62;&#x53D8;&#x6765;&#x521B;&#x5EFA;&#x5173;&#x952E;&#x5E27;&#xFF0C;&#x7136;&#x540E;&#x521B;&#x5EFA;&#x8865;&#x95F4;&#x3002;&#x611F;&#x89C9;&#x5F88;&#x50CF;&#x662F;Flash&#x548C;Live2D&#x7684;&#x4E2D;&#x95F4;&#x72B6;&#x6001;&#x3002;</p><h1 id="live2d-sdk-for-web">Live2D SDK for Web</h1><h2 id="%E6%A8%A1%E5%9E%8B%E8%87%AA%E5%AE%9A%E4%B9%89%E5%8F%82%E6%95%B0%E5%90%8D%E7%9A%84%E9%97%AE%E9%A2%98">&#x6A21;&#x578B;&#x81EA;&#x5B9A;&#x4E49;&#x53C2;&#x6570;&#x540D;&#x7684;&#x95EE;&#x9898;</h2><p>&#x6BD4;&#x8F83;&#x5FEB;&#x901F;&#x4F46;&#x4E00;&#x70B9;&#x4E5F;&#x4E0D;&#x6B63;&#x89C4;&#x7684;&#x64CD;&#x4F5C;&#xFF1A;&#x6539;Framework/src/cubismdefaultparameterid.ts</p><p>&#x9644;camel case&#x4E0E;snake case&#x53EF;&#x4EE5;&#x653E;&#x8FDB;&#x6D4F;&#x89C8;&#x5668;console&#x8F6C;&#x6362;&#xFF1A;</p><pre><code class="language-javascript">/*
&#x76EE;&#x6807;&#x53C2;&#x6570;&#x540D; json&#x793A;&#x4F8B; (&#x732B;&#x7684;&#x6A21;&#x578B;tororo&#x548C;hijiki&#x4F7F;&#x7528;&#x7684;&#x53C2;&#x6570;&#x547D;&#x540D;&#xFF09;
{
    &quot;ParamAngleX&quot;: &quot;PARAM_ANGLE_X&quot;,
    &quot;ParamAngleY&quot;: &quot;PARAM_ANGLE_Y&quot;,
    &quot;ParamAngleZ&quot;: &quot;PARAM_ANGLE_Z&quot;,
}


&#x9ED8;&#x8BA4;&#x53C2;&#x6570;&#x540D;json&#x793A;&#x4F8B;
{
    &quot;ParamAngleX&quot;: &quot;ParamAngleX&quot;,
    &quot;ParamAngleY&quot;: &quot;ParamAngleY&quot;,
    &quot;ParamAngleZ&quot;: &quot;ParamAngleZ&quot;,
}
*/

//&#x4E3B;&#x8981;&#x662F;&#x9700;&#x8981;&#x8FD9;&#x4E2A;&#xFF0C;&#x56E0;&#x4E3A;&#x9ED8;&#x8BA4;&#x662F;camel case&#xFF0C;&#x4F46;&#x6709;&#x4E9B;&#x6A21;&#x578B;&#x662F;snake case
function camel_to_snake(name) {
  return name.replace(/([A-Z])/g,&quot;_$1&quot;).toLowerCase().substring(1);
}

for(k in json){
	json[k] = camel_to_snake(json[k]).toUpperCase();
}


function snake_to_camel(name) {
	return name.split(&quot;_&quot;)
        .map(x =&gt; x.slice(0,1).toUpperCase()+
             x.slice(1).toLowerCase()).join(&apos;&apos;);
}</code></pre><p>&#x5F53;&#x7136;&#x4E86;&#xFF0C;&#x5982;&#x679C;&#x538B;&#x6839;&#x5C31;&#x4E0D;&#x5728;&#x6240;&#x8C13;&#x201C;&#x6807;&#x51C6;&#x53C2;&#x6570;&#x201D;&#x5217;&#x8868;&#x91CC;&#xFF0C;&#x90A3;&#x4E5F;&#x5C31;&#x6CA1;&#x4EC0;&#x4E48;&#x7B80;&#x4FBF;&#x529E;&#x6CD5;&#x4E86;&#x3002;</p><h2 id="%E4%BA%8B%E4%BB%B6%E7%9B%91%E5%90%AC%E4%B8%8E%E5%8A%A8%E4%BD%9C%E7%9A%84%E9%97%AE%E9%A2%98">&#x4E8B;&#x4EF6;&#x76D1;&#x542C;&#x4E0E;&#x52A8;&#x4F5C;&#x7684;&#x95EE;&#x9898;</h2><p>&#x603B;&#x4E4B;&#xFF0C;&#x5728;&#x5B98;&#x65B9;Github&#x7684;&#x4F8B;&#x5B50;&#x91CC;&#xFF0C;&#x4E8B;&#x4EF6;&#x76D1;&#x542C;&#x5728;lappdelegate.ts&#xFF0C;&#x5982;&#x4E0B;&#x90E8;&#x5206;&#x3002;&#x81EA;&#x884C;&#x6309;&#x9700;&#x6539;&#x52A8;&#xFF0C;&#x6BD4;&#x5982;&#x628A;canvas.ontouchmove&#x6539;&#x6210;document.ontouchmove&#x4E4B;&#x7C7B;&#x7684;&#x3002;</p><pre><code class="language-typescript">if (supportTouch) {
      // &#x30BF;&#x30C3;&#x30C1;&#x95A2;&#x9023;&#x30B3;&#x30FC;&#x30EB;&#x30D0;&#x30C3;&#x30AF;&#x95A2;&#x6570;&#x767B;&#x9332;
      canvas.ontouchstart = onTouchBegan;
      canvas.ontouchmove = onTouchMoved;
      canvas.ontouchend = onTouchEnded;
      canvas.ontouchcancel = onTouchCancel;
} else {
	...
}</code></pre><p>&#x52A8;&#x4F5C;&#x5728;lapplive2dmanager.ts&#x91CC;&#xFF08;&#x8FD9;&#x4E2A;onTap&#x8FD9;&#x91CC;&#xFF09;&#xFF1A;</p><pre><code class="language-typescript">public onTap(x: number, y: number): void {
    if (LAppDefine.DebugLogEnable) {
      LAppPal.printMessage(
        `[APP]tap point: {x: ${x.toFixed(2)} y: ${y.toFixed(2)}}`
      );
    }

    for (let i = 0; i &lt; this._models.getSize(); i++) {
      if (this._models.at(i).hitTest(LAppDefine.HitAreaNameHead, x, y)) {
        if (LAppDefine.DebugLogEnable) {
          LAppPal.printMessage(
            `[APP]hit area: [${LAppDefine.HitAreaNameHead}]`
          );
        }
        this._models.at(i).setRandomExpression();
      } else if (this._models.at(i).hitTest(LAppDefine.HitAreaNameBody, x, y)) {
        if (LAppDefine.DebugLogEnable) {
          LAppPal.printMessage(
            `[APP]hit area: [${LAppDefine.HitAreaNameBody}]`
          );
        }
        this._models
          .at(i)
          .startRandomMotion(
            LAppDefine.MotionGroupTapBody,
            LAppDefine.PriorityNormal,
            this._finishedMotion
          );
      } else {
        if (LAppDefine.DebugLogEnable) {
          LAppPal.printMessage(
            `[APP]hit area: None`
          );
        }
        this._models
          .at(i)
          .startRandomMotion(
            LAppDefine.MotionGroupTap,
            LAppDefine.PriorityNormal,
            this._finishedMotion
          );
      }
    }
  }
</code></pre><p>&#x4E5F;&#x53EF;&#x4EE5;&#x5728;&#x76D1;&#x542C;&#x5668;&#x91CC;&#x76F4;&#x63A5;&#x8FD9;&#x6837;&#x89E6;&#x53D1;&#x52A8;&#x4F5C;&#x7684;&#x66F4;&#x65B0;</p><pre><code class="language-typescript">let instance = LAppLive2DManager.getInstance();
instance._models.at(0).startRandomMotion(
	LAppDefine.MotionGroupTap, 
    //&#x5176;&#x5B9E;&#x5C31;&#x662F;model3&#x91CC;&#x7684;&#x52A8;&#x4F5C;, &#x6BD4;&#x5982;&quot;Tap&quot;, &quot;Flick&quot;
	
    LAppDefine.PriorityNormal, 
    //&#x66F4;&#x6362;&#x52A8;&#x4F5C;&#x7684;&#x4F18;&#x5148;&#x7EA7;
	
    instance._finishedMotion 
    //&#x56DE;&#x8C03;&#xFF0C;&#x5176;&#x5B9E;&#x5C31;&#x5728;console&#x6253;&#x5370;&#x4E86;&#x4E2A; Motion Finished
);</code></pre><h2 id="%E5%BA%94%E7%94%A8%E4%BA%8Eghost-cms%E7%9A%84%E9%97%AE%E9%A2%98">&#x5E94;&#x7528;&#x4E8E;Ghost CMS&#x7684;&#x95EE;&#x9898;</h2><p>&#x7531;&#x4E8E;&#x76EE;&#x524D;Ghost CMS&#x4E0D;&#x652F;&#x6301;&#x56FE;&#x7247;&#x3001;js&#x3001;css&#x4EE5;&#x5916;&#x7684;&#x9759;&#x6001;&#x8D44;&#x6E90;&#xFF0C;&#x6A21;&#x578B;&#x662F;&#x4E0D;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x6253;&#x5305;&#x5728;Theme&#x91CC;&#x7684;&#xFF0C;&#x4E0D;&#x7136;&#x8BFB;&#x53D6;json&#x548C;moc&#x6587;&#x4EF6;&#x4F1A;&#x76F4;&#x63A5;301&#x7136;&#x540E;404&#x3002;</p><h3 id="%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88">&#x89E3;&#x51B3;&#x65B9;&#x6848;</h3><p>&#x9700;&#x6C42;&#xFF1A;&#x8BA9;&#x811A;&#x672C;&#x80FD;&#x591F;&#x6210;&#x529F;&#x7684;&#x52A0;&#x8F7D;&#x6A21;&#x578B;&#xFF08;&#x9759;&#x6001;&#x8D44;&#x6E90;&#xFF0C;.json, .moc, .png&#x7B49;&#x7B49;&#xFF09;&#xFF0C;&#x6700;&#x597D;&#x8FD8;&#x4E0D;&#x8981;&#x8DE8;&#x57DF;&#x3002;</p><p>&#x60C5;&#x51B5;&#xFF1A;Ghost&#x5B98;&#x65B9;&#x4F30;&#x8BA1;&#x4E0D;&#x4F1A;&#x8FD9;&#x4E48;&#x505A;&#x7684;&#xFF0C;&#x5B83;&#x4EEC;&#x90FD;&#x58F0;&#x660E;&#x8FC7;&#x8FDE;&#x5A92;&#x4F53;&#x5E93;&#x90FD;&#x4E0D;&#x505A;&#x3002;</p><p>&#x66B4;&#x529B;&#x7684;&#x529E;&#x6CD5;&#xFF1A;&#x53CD;&#x6B63;&#x672C;&#x6765;&#x7F51;&#x7AD9;&#x4E5F;&#x662F;nginx&#x53CD;&#x4EE3;&#x7684;&#xFF0C;&#x5728;&#x76F8;&#x540C;&#x57DF;&#x540D;&#x4E0B;&#x76F4;&#x63A5;&#x591A;&#x914D;&#x7F6E;&#x4E00;&#x4E2A;location&#x5728;&#x6839;&#x4E4B;&#x524D;&#x8FDB;&#x884C;&#x5339;&#x914D;&#xFF0C;&#x7528;&#x4E8E;&#x52A0;&#x8F7D;&#x6A21;&#x578B;&#x3002;</p><pre><code class="language-nginx">location /live2d/ {
    root /somepath/somepath;
    autoindex on;
}</code></pre><p>&#x5982;&#x4E0A;&#xFF0C;&#x628A;&#x6A21;&#x578B;&#x653E;&#x5728;/somepath/somepath/live2d&#x76EE;&#x5F55;&#x4E0B;&#xFF0C;&#x7136;&#x540E;&#x5728;themes&#x7684;js&#x91CC;&#x4ECE;/live2d/&#x52A0;&#x8F7D;&#x6A21;&#x578B;&#x5C31;&#x884C;&#x4E86;&#x3002;</p>]]></content:encoded></item><item><title><![CDATA[Data Structure (3) - BST & AVL Tree]]></title><description><![CDATA[<h1 id="binary-search-tree">Binary Search Tree</h1><p>A BST (binary search tree) is a tree T such:</p><ul><li>T = {} or</li><li>T = {r, T<sub>L</sub>, T<sub>R</sub>} where all nodes in T<sub>L</sub> has keys less than r, all nodes in T<sub>R</sub> has keys greater than r, and T<sub>L</sub> and T<sub>R</sub> are BSTs.</li></ul>]]></description><link>https://blog.fluere.cloud/data-structure-part3-bst-avl/</link><guid isPermaLink="false">61590e7dc76a54582562d55e</guid><category><![CDATA[C++]]></category><category><![CDATA[数据结构]]></category><dc:creator><![CDATA[栗子℃（EN）]]></dc:creator><pubDate>Fri, 17 Sep 2021 03:47:00 GMT</pubDate><content:encoded><![CDATA[<h1 id="binary-search-tree">Binary Search Tree</h1><p>A BST (binary search tree) is a tree T such:</p><ul><li>T = {} or</li><li>T = {r, T<sub>L</sub>, T<sub>R</sub>} where all nodes in T<sub>L</sub> has keys less than r, all nodes in T<sub>R</sub> has keys greater than r, and T<sub>L</sub> and T<sub>R</sub> are BSTs.</li></ul><pre><code class="language-cpp">// BST.h
#pragma once

template &lt;typename K, typename V&gt;
class BST {
    public:
        BST();
        void insert(const K &amp; key, const V &amp; value);
        void remove(const K &amp; key);
        K find(const K &amp; key) const;
        
    private:
        struct TreeNode {
            TreeNode *left, * right;
            K key;
            V value;
            TreeNode(const K &amp; k, const V &amp; v): key(k), value(v), left(NULL), right(NULL){}
        }
        
        TreeNode *root_;
    
}


// cpp
template &lt;typename K, typename V&gt;
V BST::find(const K &amp; key) {
    TreeNode * node = _find(root_,key);
    if(node) {
        return node-&gt;value;
    } else {
        // deal with null case
        return V();
    }
}


template &lt;typename K, typename V&gt;
TreeNode *&amp; _find(TreeNode *&amp;node, const K &amp; key) {
// returns a pointer reference of a treenode
// so if we want to find then update/insert, we can do it

    if(node == nullptr) { return node;} // can&apos;t find the key
    if(node-&gt;k==key) { // find exactly the key
        return root;
    } else {
        if(node-&gt;k &gt; key) {
            return _find(node-&gt;left, key);
        } else {
            return _find(node-&gt;right, key);
        }
    }
}


template &lt;typename K, typename V&gt;
void BST::insert(const K &amp; key, const V &amp; value) {
	_insert(root_, key, value);
}

template &lt;typename K, typename V&gt;
_insert(TreeNode *&amp; root, const K &amp; key, const V &amp; value) {
    TreeNode *&amp; loc = _find(root,key);
    if(loc == nullptr) {
        loc = new TreeNode(key, value);
    }
}
</code></pre><p>BST Remove:</p><p>IOP (In-order predecessor): left tree right most child. Largest node that is less than root.</p><p>IOS (In-order successor): right tree left most child. Smallest node that is larger than node.</p><p>When removing a node in BST:</p><ul><li>find the key</li><li>find IOP or IOS</li><li>swap node with key and IOP/IOS</li><li>delete the node with key (it&apos;s 0-child delete now)</li></ul><p>BST Worst Cases: </p><ul><li>find: O(h)</li><li>insert: O(h)</li><li>remove: O(h) &#xA0; &#x2014;&gt; O(h)+O(h) &#xA0;(find the node to remove, and find it&apos;s IOP/IOS)</li><li>traversal: O(n)</li></ul><p>For all BST, Lower bound: O(lg(n)) (when it is balanced), Upper bound: O(n) (when it is like a stick)</p><p>For all BST with n nodes (randomly sequenced), the average height of the trees is lg(n) </p><p>A balanced BST (O(h) for find, insert, delete, where h=lg(n), and O(n) for traverse) is a better choice for dictionary than sorted array (O(n) for insert, delete, traverse, O(lg(n) for find) and sorted list (O(n) for find, insert, delete, traverse).</p><h2 id="height-balanced-tree">Height Balanced Tree</h2><p>Height Balance: b = height(T<sub>R</sub>)-height(T<sub>L</sub>)</p><p>A tree T is height balanced if :</p><ul><li>T = {}</li><li>T = {r, T<sub>L</sub>, T<sub>R</sub>}, |b|&lt;=1, and T<sub>L</sub> and T<sub>R</sub> are balanced trees</li></ul><p>A complete tree is always balanced.</p><figure class="kg-card kg-image-card kg-card-hascaption"><img src="https://strapi.fluere.cloud/uploads/tree_example_balanced_and_not_balanced_32d2780691.png" class="kg-image" alt loading="lazy"><figcaption>Examples: balanced trees and imbalanced trees</figcaption></figure><h2 id="bst-rotation">BST Rotation</h2><ul><li>4 kinds of rotations (L, R, LR, RL) (LR &amp; RL for elbows)</li><li>All rotations are local (subtrees are not impacted)</li><li>All rotations are constant time: O(1)</li><li>BST property maintained</li></ul><p>Goal: produce trees of height h=O(log(n)) using only O(log(n)) time for each operation.</p><figure class="kg-card kg-image-card kg-card-hascaption"><img src="https://strapi.fluere.cloud/uploads/tree_BST_rotation_e5311483e8.png" class="kg-image" alt loading="lazy"><figcaption>BST Rotation</figcaption></figure><figure class="kg-card kg-image-card kg-card-hascaption"><img src="https://strapi.fluere.cloud/uploads/tree_AVL_rotations_043bd5bebd.png" class="kg-image" alt loading="lazy"><figcaption>AVL Rotations (L, R, RL, LR)</figcaption></figure><p>We call these trees AVL (Adelson-Velokii-Landis) trees.</p><h1 id="avl-tree">AVL Tree</h1><p>Three issues for consideration:</p><ul><li>Rotations: to fix imbalance</li><li>Maintaining height: track height of each node</li><li>Detect imbalance: when a node become out of balance</li></ul><p>When a new node is inserted, only when the tree becomes taller, it becomes imbalanced.</p><p>Insert into an AVL tree:</p><ul><li>Insert at proper place &#x2013; O(h)</li><li>Check for imbalance &#x2013; O(1)</li><li>Rotate if necessary &#x2013; O(1)</li><li>Update height</li></ul><pre><code class="language-cpp">struct TreeNode {
    T key;
    unsigned height;
    TreeNode *left;
    TreeNode *right;
}


template &lt;class T&gt;
void AVLTree::_insert(const T &amp;x, TreeNode&lt;T&gt; *&amp; t) {
    if(t==NULL) { // base case. insert into empty tree
        t = new TreeNode(x,0,NULL,NULL);
    }
    else if(x &lt; t-&gt;key) { // insert into left sub-tree
        _insert(x, t-&gt;left);
        int balance = height(t-&gt;right) - height(t-&gt;left);
        // helper func to handle when t doesn&apos;t have a left or right child
        int leftBalance = height(t-&gt;left-&gt;right) - height(t-&gt;left-&gt;left);
        
        if(balance == 2) { // out of balance
        	if(leftBalance == -1) { // a stick
            	rotateR(t);
            }
            else { // elbow
            	rotateLR(t);
            }
        }
    }
    else if(x &gt; t-&gt;key) { // insert into right sub-tree
        _insert(x, t-&gt;right);
        int balance = height(t-&gt;right) - height(t-&gt;left);
        int rightBalance = height(t-&gt;right-&gt;right) - height(t-&gt;right-&gt;left);
        if(balance == 2) { // out of balance
            if(rightBalance == 1) { // a stick
            	rotateL(t);
            } else { // elbow
                rotateRL(t)l
            }
        }
    } 
    
    t-&gt;height = 1 + max(height(t-&gt;left), height(t-&gt;right));
}</code></pre><h2 id="analysis">Analysis</h2><p>Find runs at O(h)</p><p>Insert runs at O(h) find + O(1) add + O(1) rebalance, total O(h)</p><p>Remove runs at O(h) find + O(h) find IOP/IOS + O(1) remove + h*O(1) rebalance, total O(h)</p><p>Where <strong>h</strong> is O(log(n)). </p><h3 id="minimum-number-of-nodes">Minimum number of nodes</h3><p>Let N(h) be smallest number of nodes in an AVL tree of height h:</p><p>N(-1) = 0, </p><p>N(0) = 1, </p><p>N(1) = 2,</p><p>... </p><p>N(h) = 1 + N(h-1) + N(h-2),</p><p>which is, root + a sub tree of (h-1) and a sub tree of (h-2), balance = 1 or -1</p><p>So, N(h) &gt; N(h-1) + N(h-2) &gt;= 2*N(h-2) &gt;= 2<sup>h/2</sup> &#xA0;(when h&gt;=1)</p><p><strong>An AVL tree of height h has at least 2<sup>h/2</sup> nodes.</strong></p><p>Invert it:</p><p>n &gt;= N(h) &gt; 2<sup>h/2</sup>,</p><p>n&gt;= 2<sup>h/2</sup></p><p>log(n) &gt;= h/2</p><p>h&lt;= 2 log(n) &#x2014; where log(n) &gt; 0</p><p><strong>So for an AVL tree, h=O(log(n))</strong></p><h1 id="summary-of-balanced-bst-trees">Summary of Balanced BST Trees</h1><h2 id="avl">AVL</h2><ul><li>Max height: 1.44 * lg(n)</li><li>Rotations: </li><li>find &#x2013; 0 rotation</li><li>insert &#x2013; 1 rotation at most (LR, RL count as 1)</li><li>remove &#x2013; h rotations at most, where h is O(log(n))</li></ul><h2 id="red-black-tree">Red-Black Tree</h2><ul><li>Max height: 2 * lg(n)</li><li>Constant numbers of rotations:</li><li>insert &#x2013; 2 rotations at most</li><li>remove &#x2013; 3 rotations at most</li><li>Works better with modern hardware (more caches and predicters)</li><li>(C++&apos;s standard map is Red-Black tree)</li></ul><pre><code class="language-cpp">// map API
map&lt;string,int&gt; grades;
//....
grades[&quot;someone&quot;] = 100;
cout &lt;&lt; &quot;Someone&apos;s grade is: &quot; &lt;&lt; grades[&quot;someone&quot;] &lt;&lt; endl;
grades.erase(&quot;someone&quot;);

</code></pre><p></p><h2 id="pros-of-cons-of-bst">Pros of Cons of BST</h2><h3 id="pros">Pros</h3><p>Running time O(log(n)), improvement over lists and arrays as dictionary.</p><p>Good for:</p><ul><li>Approximate find: Each step (the sub-tree) is closer to target</li><li>Range and nearest neighbor find</li></ul><h3 id="cons">Cons</h3><ul><li>Never O(1)</li><li>In-memory requirement: following pointer is O(1) and fast -&gt; assume everything is in memory (if it points to resources elsewhere, like on another computer or database over Internet, it could be slow) </li></ul><p></p><p>ref: https://courses.engr.illinois.edu/cs225/fa2020/</p>]]></content:encoded></item><item><title><![CDATA[基础复习 —— 排序]]></title><description><![CDATA[<h1 id="%E6%80%A7%E8%83%BD%E6%80%BB%E8%A7%88">&#x6027;&#x80FD;&#x603B;&#x89C8;</h1><figure class="kg-card kg-image-card kg-card-hascaption"><img src="https://blog.fluere.cloud/content/images/2021/09/image.png" class="kg-image" alt loading="lazy" width="2000" height="617" srcset="https://blog.fluere.cloud/content/images/size/w600/2021/09/image.png 600w, https://blog.fluere.cloud/content/images/size/w1000/2021/09/image.png 1000w, https://blog.fluere.cloud/content/images/size/w1600/2021/09/image.png 1600w, https://blog.fluere.cloud/content/images/size/w2400/2021/09/image.png 2400w" sizes="(min-width: 720px) 720px"><figcaption>ref: <a href="https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/pxal47/">https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/pxal47/</a></figcaption></figure><h2 id="%E6%80%A7%E8%B4%A8">&#x6027;&#x8D28;</h2><h3 id="%E7%A8%B3%E5%AE%9A%E6%80%A7">&#x7A33;&#x5B9A;&#x6027;</h3><p>&#x6309;&#x6BD4;&#x8F83;&#x7684;&#x6807;&#x51C6;&#xFF0C;&#x4E24;&#x5143;&#x7D20;&#x76F8;&#x7B49;&#x65F6;&#xFF0C;&#x4E0D;&#x4F1A;&#x6539;&#x53D8;&#x8FD9;&#x4E24;&#x4E2A;&#x5143;&#x7D20;&#x7684;&#x76F8;&#x5BF9;&#x987A;&#x5E8F;&#x3002;</p><p>&#x610F;&#x4E49;&#x662F;&#xFF0C;</p>]]></description><link>https://blog.fluere.cloud/algorithm-sort/</link><guid isPermaLink="false">60fe742b1b949b031d124ccb</guid><category><![CDATA[基础复习]]></category><category><![CDATA[算法]]></category><dc:creator><![CDATA[栗子℃]]></dc:creator><pubDate>Wed, 15 Sep 2021 10:13:00 GMT</pubDate><content:encoded><![CDATA[<h1 id="%E6%80%A7%E8%83%BD%E6%80%BB%E8%A7%88">&#x6027;&#x80FD;&#x603B;&#x89C8;</h1><figure class="kg-card kg-image-card kg-card-hascaption"><img src="https://blog.fluere.cloud/content/images/2021/09/image.png" class="kg-image" alt loading="lazy" width="2000" height="617" srcset="https://blog.fluere.cloud/content/images/size/w600/2021/09/image.png 600w, https://blog.fluere.cloud/content/images/size/w1000/2021/09/image.png 1000w, https://blog.fluere.cloud/content/images/size/w1600/2021/09/image.png 1600w, https://blog.fluere.cloud/content/images/size/w2400/2021/09/image.png 2400w" sizes="(min-width: 720px) 720px"><figcaption>ref: <a href="https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/pxal47/">https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/pxal47/</a></figcaption></figure><h2 id="%E6%80%A7%E8%B4%A8">&#x6027;&#x8D28;</h2><h3 id="%E7%A8%B3%E5%AE%9A%E6%80%A7">&#x7A33;&#x5B9A;&#x6027;</h3><p>&#x6309;&#x6BD4;&#x8F83;&#x7684;&#x6807;&#x51C6;&#xFF0C;&#x4E24;&#x5143;&#x7D20;&#x76F8;&#x7B49;&#x65F6;&#xFF0C;&#x4E0D;&#x4F1A;&#x6539;&#x53D8;&#x8FD9;&#x4E24;&#x4E2A;&#x5143;&#x7D20;&#x7684;&#x76F8;&#x5BF9;&#x987A;&#x5E8F;&#x3002;</p><p>&#x610F;&#x4E49;&#x662F;&#xFF0C;&#x5728;&#x6309;&#x67D0;&#x4E00;&#x6807;&#x51C6;&#x8FDB;&#x884C;&#x6392;&#x5E8F;&#x65F6;&#xFF0C;&#x4E0D;&#x6253;&#x4E71;&#x6309;&#x5176;&#x4ED6;&#x6807;&#x51C6;&#x5DF2;&#x7ECF;&#x6392;&#x597D;&#x7684;&#x987A;&#x5E8F;&#x3002;</p><p>&#x4F8B;&#xFF1A;&#xFF08;A, 2), (B, 1), (C, 3), (D, 2) &#x672C;&#x8EAB;&#x5DF2;&#x7ECF;&#x6309;&#x5B57;&#x6BCD;&#x6392;&#x5E8F;&#xFF0C;&#x82E5;&#x5BF9;&#x6570;&#x5B57;&#x6392;&#x5E8F;&#xFF0C;&#x975E;&#x7A33;&#x5B9A;&#x7684;&#x6392;&#x5E8F;&#x53EF;&#x80FD;&#x4F1A;&#x5C06;D&#x653E;&#x5230;A&#x524D;&#x9762;&#xFF0C;&#x800C;&#x7A33;&#x5B9A;&#x7684;&#x6392;&#x5E8F;&#x53EF;&#x4EE5;&#x786E;&#x4FDD;A, D&#x95F4;&#x987A;&#x5E8F;&#x4E0D;&#x53D8;&#x3002;</p><h3 id="%E5%B0%B1%E5%9C%B0%E6%80%A7">&#x5C31;&#x5730;&#x6027;</h3><p>&#x662F;&#x5426;&#x9700;&#x8981;&#x989D;&#x5916;&#x7684;&#x7A7A;&#x95F4;&#x5B58;&#x653E;&#x8F85;&#x52A9;&#x6570;&#x7EC4;&#xFF0C;&#x8FD8;&#x662F;&#x76F4;&#x63A5;&#x5728;&#x8981;&#x6392;&#x5E8F;&#x7684;&#x6570;&#x7EC4;&#x4E0A;&#x64CD;&#x4F5C;&#x5C31;&#x53EF;&#x4EE5;&#x3002;</p><p>&#x53E6;&#x5916;&#xFF0C;&#x65E0;&#x5173;&#x5C31;&#x5730;&#x6027;&#xFF0C;&#x5982;&#x679C;&#x5E0C;&#x671B;&#x8F93;&#x5165;&#x7684;&#x6570;&#x7EC4;&#x4E0D;&#x8981;&#x88AB;&#x6539;&#x52A8;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x6392;&#x5E8F;&#x524D;&#x5148;&#x590D;&#x5236;&#x6570;&#x7EC4;&#x518D;&#x64CD;&#x4F5C;&#x3002;</p><pre><code class="language-java">// &#x4F8B;&#xFF0C;&#x5229;&#x7528;Arrays.copyOf
int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);</code></pre><h3 id="%E8%87%AA%E9%80%82%E5%BA%94%E6%80%A7">&#x81EA;&#x9002;&#x5E94;&#x6027;</h3><p>&#x65F6;&#x95F4;&#x590D;&#x6742;&#x5EA6;&#x53D7;&#x5143;&#x7D20;&#x5206;&#x5E03;&#x5F71;&#x54CD;&#x3002;</p><p>&#x80FD;&#x627E;&#x5230;&#x7684;&#x76F8;&#x5173;&#x8D44;&#x6599;&#x4E0D;&#x591A;&#xFF0C;&#x53EF;&#x80FD;&#x8FD9;&#x4E2A;&#x8BF4;&#x6CD5;&#x4E0D;&#x592A;&#x5E38;&#x89C1;&#xFF0C;&#x4F46;&#x7528;&#x5904;&#x5E94;&#x8BE5;&#x662F;&#xFF0C;&#x5728;&#x5DF2;&#x7ECF;&#x8FD1;&#x4F3C;&#x662F;&#x6709;&#x5E8F;&#xFF08;&#x77E5;&#x9053;&#x65E0;&#x5E8F;&#x5143;&#x7D20;&#x5E76;&#x4E0D;&#x591A;&#xFF09;&#x7684;&#x6761;&#x4EF6;&#x4E0B;&#xFF0C;&#x6709;&#x81EA;&#x9002;&#x5E94;&#x6027;&#x7684;&#x6392;&#x5E8F;&#x7B97;&#x6CD5;&#x62E5;&#x6709;&#x66F4;&#x4F4E;&#x7684;&#x65F6;&#x95F4;&#x590D;&#x6742;&#x5EA6;&#x3002;</p><h3 id="%E6%AF%94%E8%BE%83%E7%B1%BB">&#x6BD4;&#x8F83;&#x7C7B;</h3><p>&#x57FA;&#x4E8E;&#x5143;&#x7D20;&#x4E4B;&#x95F4;&#x7684;&#x6BD4;&#x8F83;&#xFF08;&gt;, &lt;, =&#xFF09;&#x6765;&#x8FDB;&#x884C;&#x6392;&#x5E8F;&#x3002;</p><p></p><h2 id="%E9%80%9F%E5%BA%A6">&#x901F;&#x5EA6;</h2><p>&#x4E00;&#x822C;&#x60C5;&#x51B5;&#x4E0B;&#xFF1A;&#x5FEB;&#x901F;&gt;&#x5F52;&#x5E76;&gt;&#x5E0C;&#x5C14;&gt;&#x5806;</p><figure class="kg-card kg-code-card"><pre><code>&#x6570;&#x636E;&#x89C4;&#x6A21;		&#x5FEB;&#x901F;&#x6392;&#x5E8F;		&#x5F52;&#x5E76;&#x6392;&#x5E8F;		&#x5E0C;&#x5C14;&#x6392;&#x5E8F;		&#x5806;&#x6392;&#x5E8F;
1000&#x4E07;		0.75		1.22		1.77		3.57
5000&#x4E07;		3.78		6.29		9.48		26.54
1&#x4EBF;		7.65		13.06		18.79		61.31</code></pre><figcaption>&#x53C2;&#x8003;&#x6570;&#x636E;&#xFF08;&#x5355;&#x4F4D;&#xFF1A;&#x79D2;&#xFF09;&#xFF1A;<a href="https://blog.csdn.net/qq_39521554/article/details/79364718">https://blog.csdn.net/qq_39521554/article/details/79364718</a></figcaption></figure><p></p><h1 id="%E5%86%92%E6%B3%A1%E6%8E%92%E5%BA%8F">&#x5192;&#x6CE1;&#x6392;&#x5E8F;</h1><p>{&#x65E0;&#x5E8F;&#xFF0C;&#x6709;&#x5E8F;}&#xFF0C;&#x6BCF;&#x6B21;&#x6BD4;&#x8F83;&#x65E0;&#x5E8F;&#x90E8;&#x5206;&#x4E2D;&#x76F8;&#x90BB;&#x7684;&#x4E24;&#x4E2A;&#x5143;&#x7D20;&#xFF0C;&#x987A;&#x5E8F;&#x9519;&#x8BEF;&#x5C31;&#x4EA4;&#x6362;&#x8FC7;&#x6765;&#x3002;&#x6BCF;&#x8F6E;&#x5FAA;&#x73AF;&#x53EF;&#x4EE5;&#x786E;&#x4FDD;&#x4E00;&#x4E2A;&#x6700;&#x5927;/&#x6700;&#x5C0F;&#x88AB;&#x627E;&#x5230;&#x5E76;&#x653E;&#x5728;&#x6700;&#x8FB9;&#x7F18;&#xFF08;&#x6210;&#x4E3A;&#x6574;&#x4E2A;&#x5E8F;&#x5217;&#x540E;&#x65B9;&#x6709;&#x5E8F;&#x5E8F;&#x5217;&#x7684;&#x4E00;&#x90E8;&#x5206;&#xFF09;&#x3002;</p><pre><code class="language-java">public static void bubbleSort(int[] arr) {
	for (int i = 0; i &lt; arr.length-1 ; i++) {
		boolean flag = true; // &#x8DF3;&#x51FA;&#x65D7;&#x5E1C;
		// &#x5982;&#x679C;&#x4E00;&#x8F6E;&#x5185;&#x5FAA;&#x73AF;&#x4E2D;&#x6CA1;&#x6709;&#x8FDB;&#x884C;&#x8FC7;&#x4EA4;&#x6362;
		// &#x8BF4;&#x660E;&#x524D;&#x65B9;&#x7684;&#x987A;&#x5E8F;&#x5DF2;&#x7ECF;&#x5168;&#x90E8;&#x662F;&#x6B63;&#x786E;&#x7684;&#xFF0C;&#x53EF;&#x4EE5;&#x505C;&#x6B62;&#x7EE7;&#x7EED;&#x6392;&#x5E8F;&#x4E86;

		for (int j = 0; j &lt; arr.length - i; j++) {
			// i&#x6BCF;&#x52A0;1&#xFF0C;&#x540E;&#x9762;&#x5C31;&#x591A;&#x4E00;&#x4F4D;&#x5DF2;&#x7ECF;&#x6392;&#x597D;&#x7684;&#xFF0C;&#x4E0D;&#x8BB8;&#x8981;&#x8FDB;&#x5165;&#x4E0B;&#x8F6E;&#x5185;&#x5FAA;&#x73AF;
			if (arr[j] &gt; arr[j + 1]) {
				int tmp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = tmp;

				flag = false; // &#x6807;&#x8BB0;&#x6709;&#x8FC7;&#x4EA4;&#x6362;
			}
		}

		if (flag) { // &#x6CA1;&#x6709;&#x4EA4;&#x6362;&#xFF0C;&#x5DF2;&#x7ECF;&#x6392;&#x597D;&#x4E86;
			break;
		}
	}
}
</code></pre><p></p><h1 id="%E9%80%89%E6%8B%A9%E6%8E%92%E5%BA%8F">&#x9009;&#x62E9;&#x6392;&#x5E8F;</h1><p>&#x6BCF;&#x6B21;&#x5FAA;&#x73AF;&#x90FD;&#x627E;&#x5230;&#x6700;&#x5927;/&#x6700;&#x5C0F;&#x5143;&#x7D20;&#x7684;&#x4F4D;&#x7F6E;&#xFF0C;&#x7136;&#x540E;&#x4EA4;&#x6362;&#x5230;&#x5F00;&#x5934;/&#x7ED3;&#x5C3E;&#x3002;</p><pre><code class="language-java">public static void selectionSort(int[] arr) {
	for (int i = 0; i &lt; arr.length - 1; i++) {
		// i &#x4E3A;&#x7B2C;(i+1)&#x5C0F;&#x5143;&#x7D20;&#x5E94;&#x8BE5;&#x5728;&#x7684;&#x4F4D;&#x7F6E;
		// arr.length - 1 &#x56E0;&#x4E3A;&#x540E;&#x9762;&#x53EA;&#x5269;&#x4E00;&#x4E2A;&#x5143;&#x7D20;&#x7684;&#x65F6;&#x5019;&#x5C31;&#x4E0D;&#x7528;&#x4EA4;&#x6362;&#x4E86;&#x3002;&#x3002;&#x3002;
		
		int min = i; // &#x7528;&#x4E8E;&#x8BB0;&#x5F55;&#x6700;&#x5C0F;&#x5143;&#x7D20;&#x4F4D;&#x7F6E;&#xFF0C;&#x4E4B;&#x540E;&#x4EA4;&#x6362;&#x5230;&#x4F4D;&#x7F6E;i

		for (int j = i + 1; j &lt; arr.length; j++) {
			if (arr[j] &lt; arr[min]) {
				min = j;
			}
		}

		if (i != min) {
			int tmp = arr[i];
			arr[i] = arr[min];
			arr[min] = tmp;
		}

	}
}</code></pre><p></p><h1 id="%E6%8F%92%E5%85%A5%E6%8E%92%E5%BA%8F">&#x63D2;&#x5165;&#x6392;&#x5E8F;</h1><p>&#x7C7B;&#x4F3C;&#x6574;&#x7406;&#x6251;&#x514B;&#x724C;&#xFF0C;&#x628A;&#x6574;&#x4E2A;&#x6570;&#x7EC4;&#x770B;&#x505A; {&#x6709;&#x5E8F;&#x5E8F;&#x5217;&#xFF0C;&#x65E0;&#x5E8F;&#x5E8F;&#x5217;} &#xFF08;&#x4E00;&#x5F00;&#x59CB;&#x53EA;&#x628A;&#x7B2C;&#x4E00;&#x4E2A;&#x5143;&#x7D20;&#x770B;&#x505A;&#x6709;&#x5E8F;&#xFF0C;&#x540E;&#x9762;&#x5168;&#x662F;&#x65E0;&#x5E8F;&#xFF09;&#x3002;&#x4ECE;&#x65E0;&#x5E8F;&#x5E8F;&#x5217;&#x91CC;&#x62FF;&#x4E1C;&#x897F;&#x5EFA;&#x9020;&#x524D;&#x9762;&#x7684;&#x6709;&#x5E8F;&#x5E8F;&#x5217;&#xFF0C;&#x76F4;&#x5230;&#x65E0;&#x5E8F;&#x5E8F;&#x5217;&#x6D88;&#x8017;&#x5B8C;&#x3002;</p><p>&#x6BCF;&#x6B21;&#x628A;&#x65E0;&#x5E8F;&#x5E8F;&#x5217;&#x4E2D;&#x7B2C;&#x4E00;&#x4E2A;&#x5143;&#x7D20;&#x63D2;&#x8FDB;&#x6709;&#x5E8F;&#x5E8F;&#x5217;&#x5B83;&#x8BE5;&#x5728;&#x7684;&#x5730;&#x65B9;&#xFF08;&#x6709;&#x5E8F;&#x5E8F;&#x5217;&#x957F;&#x5EA6;+1&#xFF0C;&#x65E0;&#x5E8F;&#x5E8F;&#x5217;&#x957F;&#x5EA6;-1&#xFF09;&#x3002;</p><pre><code class="language-java">public static void insertionSort(int[] arr) {
	for (int i = 1; i &lt; arr.length; i++) {
		// &#x7B2C;0&#x4E2A;&#x8DF3;&#x8FC7;&#xFF0C;&#x76F4;&#x63A5;&#x770B;&#x505A;&#x53EA;&#x6709;1&#x4E2A;&#x5143;&#x7D20;&#x7684;&#x6709;&#x5E8F;&#x5E8F;&#x5217;
		// &#x65E0;&#x5E8F;&#x4ECE;&#x7B2C;1&#x4E2A;&#x5F00;&#x59CB;
		int tmp = arr[i]; 

		int j = i; 
		while (j &gt; 0 &amp;&amp; tmp &lt; arr[j - 1]) {
			// &#x62FF;&#x7740;tmp&#x90A3;&#x4E2A;&#x65E0;&#x5E8F;&#xFF0C;&#x628A;&#x6BD4;&#x5B83;&#x5927;&#x7684;&#x6709;&#x5E8F;&#x5F80;&#x53F3;&#x79FB;&#x817E;&#x5730;&#x65B9;
			arr[j] = arr[j - 1]; 
			j--;
		}

		if (j != i) {
			arr[j] = tmp; // &#x628A;tmp&#x63D2;&#x8FC7;&#x53BB;
		}

	}
}</code></pre><p></p><h1 id="%E5%B8%8C%E5%B0%94%E6%8E%92%E5%BA%8F">&#x5E0C;&#x5C14;&#x6392;&#x5E8F;</h1><p>&#x6BCF;&#x6B21;&#x4EE5;&#x4E00;&#x5B9A;&#x7684;&#x6B65;&#x957F;&#x628A;&#x6574;&#x4E2A;&#x5E8F;&#x5217;&#x5212;&#x5206;&#x6210;&#x82E5;&#x5E72;&#x5B50;&#x5E8F;&#x5217;&#xFF0C;&#x5206;&#x522B;&#x5BF9;&#x8FD9;&#x4E9B;&#x5B50;&#x5E8F;&#x5217;&#x8FDB;&#x884C;&#x63D2;&#x5165;&#x6392;&#x5E8F;&#xFF0C;&#x5E76;&#x9010;&#x6E10;&#x51CF;&#x5C0F;&#x6B65;&#x957F;&#xFF0C;&#x76F4;&#x81F3;&#x6700;&#x540E;&#x6B65;&#x957F;&#x4E3A;1&#x3002;</p><p>[3, 5, 9, 2, 4, 1, 6, 7, 8, 10]</p><p>&#x4F8B;&#xFF0C;&#x6B65;&#x957F;&#x4E3A;5&#x65F6;&#xFF0C;&#x5B50;&#x5E8F;&#x5217;&#x5206;&#x522B;&#x4E3A;&#xFF1A;</p><p>[3, 1], [5,6], [9,7], [2,8], [4,10]&#xFF0C;&#x5206;&#x522B;&#x6392;&#x5E8F;&#x540E; --&gt; [1,3], [5,6], [7,9], [2,8], [4,10]</p><p>&#x6B64;&#x8F6E;&#x6392;&#x5E8F;&#x540E;--&gt; [1,5,7,2,4,3,6,9,8,10]</p><p>&#xFF08;&#x5982;&#x6BCF;&#x6B21;&#x6B65;&#x957F;&#x7F29;&#x77ED;&#x81F3;&#x539F;&#x6765;&#x7684;&#x4E00;&#x534A;&#x540E;&#x5411;&#x4E0B;&#x53D6;&#x6574;&#xFF09;&#x7F29;&#x77ED;&#x6B65;&#x957F;&#x81F3;2&#xFF0C;&#x6B64;&#x65F6;&#x5B50;&#x5E8F;&#x5217;&#x4E3A;&#xFF1A;</p><p>[1,7,4,6,8], [5,2,3,9,10]&#xFF0C; &#x5206;&#x522B;&#x6392;&#x5E8F;&#x540E; --&gt; [1,4,6,7,8], [2,3,5,9,10]</p><p>&#x6B64;&#x8F6E;&#x6392;&#x5E8F;&#x540E; --&gt; [1,2,4,3,6,5,7,9,8,10]</p><p>&#x6700;&#x540E;&#x7ECF;&#x4E00;&#x8F6E;&#x6B65;&#x957F;&#x4E3A;1&#x7684;&#x63D2;&#x5165;&#x6392;&#x5E8F;</p><pre><code class="language-java">public static void shellSort(int[] arr) {
    int length = arr.length;
    int temp;
    for (int step = length / 2; step &gt;= 1; step /= 2) {
        for (int i = step; i &lt; length; i++) {
            temp = arr[i];
            int j = i - step;
            while (j &gt;= 0 &amp;&amp; arr[j] &gt; temp) {
                arr[j + step] = arr[j];
                j -= step;
            }
            arr[j + step] = temp;
        }
    }
}</code></pre><ul><li>&#x5E0C;&#x5C14;&#x6392;&#x5E8F;&#x4E2D;&#x6BD4;&#x8F83;&#x7CDF;&#x7CD5;&#x7684;&#x60C5;&#x51B5;&#xFF1A;&#x4E34;&#x8FD1;&#x51E0;&#x8F6E;step&#x4E92;&#x4E3A;&#x500D;&#x6570;&#x7684;&#x60C5;&#x51B5;&#xFF0C;&#x4F1A;&#x5BFC;&#x81F4;&#x4E0A;&#x4E00;&#x8F6E;&#x5DF2;&#x7ECF;&#x6392;&#x597D;&#x5E8F;&#x7684;&#x6570;&#x5B57;&#x4EEC;&#x53C8;&#x88AB;&#x5F52;&#x5728;&#x540C;&#x4E00;&#x7EC4;&#x8FDB;&#x884C;&#x6392;&#x5E8F;&#xFF0C;&#x964D;&#x4F4E;&#x6548;&#x7387;</li></ul><h1 id="%E5%BF%AB%E9%80%9F%E6%8E%92%E5%BA%8F">&#x5FEB;&#x901F;&#x6392;&#x5E8F;</h1><p>&#x4F7F;&#x7528;&#x5206;&#x6CBB;&#x6CD5;&#xFF08;Divide and conquer&#xFF09;&#x7684;&#x4E00;&#x79CD;&#x6392;&#x5E8F;&#xFF0C;&#x901F;&#x5EA6;&#x901A;&#x5E38;&#x8F83;&#x5FEB;&#x3002;&#x6BCF;&#x6B21;&#x9009;&#x62E9;&#x4E00;&#x4E2A;&#x503C;&#x4E3A;&#x57FA;&#x51C6;&#xFF08;&#x4E00;&#x822C;&#x76F4;&#x63A5;&#x53D6;&#x5E8F;&#x5217;&#x7B2C;&#x4E00;&#x4E2A;&#x503C;&#xFF09;&#xFF0C;&#x628A;&#x6BD4;&#x5B83;&#x5C0F;&#x7684;&#x5168;&#x632A;&#x52A8;&#x5230;&#x5B83;&#x5DE6;&#x8FB9;&#x3002;&#x7136;&#x540E;&#xFF0C;&#x4EE5;&#x8FD9;&#x4E2A;&#x503C;&#x4E3A;&#x5206;&#x754C;&#xFF0C;&#x5BF9;&#x5B83;&#x5DE6;&#x8FB9;&#x3001;&#x53F3;&#x8FB9;&#x7684;&#x5E8F;&#x5217;&#x5206;&#x522B;&#x8FDB;&#x884C;&#x5FEB;&#x901F;&#x6392;&#x5E8F;&#xFF08;&#x9012;&#x5F52;&#xFF09;&#xFF0C;&#x76F4;&#x5230;&#x6BCF;&#x4E2A;&#x8981;&#x6392;&#x5E8F;&#x7684;&#x5E8F;&#x5217;&#x957F;&#x5EA6;&#x90FD;&#x4E3A;1&#xFF0C;&#x5C31;&#x6392;&#x597D;&#x4E86;&#x3002;</p><pre><code class="language-java">public class QuickSort {
    public static void main(String[] args) {
    	// &#x5B9E;&#x9A8C;
        int[] arr = {3,1,8,2,4,5,2,-1,0,9,10,-2,-5,4,6};
        sort(arr);
    }
    
    public static int[] sort(int[]arr){
    	quickSort(arr,0,arr.length-1);
        return arr;
    }
    
    private static void quickSort(int[] arr, int left, int right) {
        if(left&lt;right){// &#x5219;&#x5305;&#x542B;left,right&#x7684;&#x5E8F;&#x5217;&#x957F;&#x5EA6;&gt;=2
            int partition_index = partition(arr, left, right);
            quickSort(arr,left, partition_index-1);
            quickSort(arr,partition_index+1, right);
        }// &#x5373;&#xFF0C;&#x5F53;&#x6BCF;&#x4E2A;&#x5B50;&#x5E8F;&#x5217;&#x53EA;&#x5269;1&#x4E2A;&#x5143;&#x7D20;&#xFF0C;&#x7ED3;&#x675F;&#x6392;&#x5E8F;
    }
    
    private static int partition(int[] arr, int left, int right) {
        //int left_ = left, right_ = right; // &#x53EA;&#x662F;&#x4E3A;&#x4E86;&#x6253;&#x5370;&#xFF0C;&#x4E0D;&#x8981;&#x5728;&#x610F;
        
        int base = arr[left]; // &#x57FA;&#x51C6;&#x503C;
        while(left&lt;right){
            while(left&lt;right &amp;&amp; arr[right]&gt;=base){
            	// &#x4ECE;&#x53F3;&#x5411;&#x5DE6;&#x627E;&#x5C0F;&#x4E8E;base&#x7684;&#x5143;&#x7D20;
                // &#x5927;&#x4E8E;base&#x7684;&#x4E0D;&#x7BA1;&#xFF0C;&#x76F4;&#x63A5;&#x8DF3;&#x8FC7;
                // &#x5982;&#x679C;&#x4E00;&#x8DEF;&#x90FD;&#x6CA1;&#x6709;&#x5C0F;&#x4E8E;base&#x7684;&#xFF0C;&#x5185;&#x5916;&#x5FAA;&#x73AF;&#x5C31;&#x90FD;&#x7ED3;&#x675F;&#x4E86;&#x3002;&#x3002;&#x3002;
                right--;
            }
            arr[left] = arr[right]; //&#x6362;&#x5230;&#x6700;&#x5DE6;&#x8FB9;&#xFF08;base&#x7684;&#x4F4D;&#x7F6E;&#xFF09;
            while(left&lt;right &amp;&amp; arr[left]&lt;=base){
            	// &#x4ECE;&#x5DE6;&#x5F80;&#x53F3;&#x627E;&#x5927;&#x4E8E;base&#x7684;&#x5143;&#x7D20;
                left++;
            }
            arr[right] = arr[left]; // &#x653E;&#x5230;&#x521A;&#x624D;&#x521A;&#x624D;&#x90A3;&#x4E2A;&#x5C0F;&#x4E8E;base&#x5143;&#x7D20;&#x7684;&#x4F4D;&#x7F6E;
        }
        arr[left] = base; // &#x51FA;&#x5FAA;&#x73AF;&#xFF0C;left,right&#x6307;&#x5411;&#x540C;&#x4E00;&#x4E2A;&#x4F4D;&#x7F6E;
        // &#x5DE6;&#x8FB9;&#x6CA1;&#x6709;&#x6BD4;base&#x5927;&#x7684;,&#x53F3;&#x8FB9;&#x6CA1;&#x6709;&#x6BD4;base&#x5C0F;&#x7684;
        
        /*
        System.out.println(&quot;---partition----base:&quot;+base+&quot;(pos:&quot;+left+&quot;)&quot;);
        System.out.println(&quot;---range: pos&quot;+left_+&quot;(val in outcome:&quot;+arr[left_]+
                           &quot;) ~ pos&quot;+right_+&quot;(val in outcome:&quot;+arr[right_]+&quot;)&quot;);
        System.out.println(&quot;---outcome---&quot;+Arrays.toString(arr));
        System.out.println(); // &#x6253;&#x5370;&#x7ED3;&#x679C;&#xFF08;&#x5FEB;&#x901F;&#x6392;&#x5E8F;&#x7684;&#x4E2D;&#x95F4;&#x8FC7;&#x7A0B;&#xFF09;
        */
        return left; // &#x8FD4;&#x56DE;base/partition&#x7684;&#x4F4D;&#x7F6E;
    }
    
}</code></pre><ul><li>&#x6BCF;&#x6B21;&#x6267;&#x884C;QuickSort&#x5176;&#x5B9E;&#x662F;&#x628A;&#x57FA;&#x51C6;&#x503C;&#x653E;&#x5728;&#x5B83;&#x6B63;&#x786E;&#x7684;&#x4F4D;&#x7F6E;&#xFF08;&#x5DE6;&#x8FB9;&#x6240;&#x6709;&#x6570;&#x6BD4;&#x5B83;&#x5C0F;&#xFF0C;&#x53F3;&#x8FB9;&#x6240;&#x6709;&#x6570;&#x6BD4;&#x5B83;&#x5927;&#xFF0C;&#x90A3;&#x4E48;&#x5B83;&#x4E00;&#x5B9A;&#x5DF2;&#x7ECF;&#x5728;&#x6B63;&#x786E;&#x4F4D;&#x7F6E;&#x4E0A;&#xFF09;</li></ul><h1 id="%E5%BD%92%E5%B9%B6%E6%8E%92%E5%BA%8F">&#x5F52;&#x5E76;&#x6392;&#x5E8F;</h1><ol><li>&#x628A;&#x6574;&#x4E2A;&#x65E0;&#x5E8F;&#x5E8F;&#x5217;&#x91CC;&#x7684;&#x5143;&#x7D20;&#x62C6;&#x6210;&#x4E00;&#x4E2A;&#x4E00;&#x4E2A;&#x7684;</li><li>&#x62FF;&#x4E24;&#x4E2A;&#x5355;&#x4E2A;&#x5143;&#x7D20;&#x7EC4;&#x6210;&#x6709;&#x5E8F;&#x5E8F;&#x5217;</li><li>&#x62FF;&#x4E24;&#x4E2A;&#x7531;&#x4E24;&#x4E2A;&#x5355;&#x4E2A;&#x5143;&#x7D20;&#x7EC4;&#x6210;&#x6709;&#x5E8F;&#x5E8F;&#x5217;&#x7EC4;&#x6210;&#x6709;&#x5E8F;&#x5E8F;&#x5217;</li><li>&#x62FF;&#x4E24;&#x4E2A;&#x7531;&#x4E24;&#x4E2A;&#x6709;&#x5E8F;&#x5E8F;&#x5217;&#x7EC4;&#x6210;&#x7684;&#x6709;&#x5E8F;&#x5E8F;&#x5217;&#x7EC4;&#x6210;&#x6709;&#x5E8F;&#x5E8F;&#x5217;</li><li>&#x62FF;&#x4E24;&#x4E2A;&#x7531;&#x4E24;&#x4E2A;&#x7531;&#x4E24;&#x4E2A;&#x6709;&#x5E8F;&#x5E8F;&#x5217;&#x7EC4;&#x6210;&#x7684;&#x6709;&#x5E8F;&#x5E8F;&#x5217;&#x7EC4;&#x6210;&#x7684;&#x6709;&#x5E8F;&#x5E8F;&#x5217;&#x7EC4;&#x6210;&#x6709;&#x5E8F;&#x5E8F;&#x5217;</li><li>&#x2026;&#x2026;</li></ol><p>&#x4F60;&#x61C2;&#x7684;:)</p><p>(&#x5176;&#x5B9E;&#x5927;&#x6982;&#x62FF;3&#x3001;4&#x3001;5&#x4E2A;&#x4E5F;&#x65E0;&#x6240;&#x8C13;&#xFF0C;&#x53EA;&#x662F;&#x4E24;&#x4E2A;&#x5199;&#x7740;&#x6BD4;&#x8F83;&#x65B9;&#x4FBF;&#x2026;&#x2026;)</p><pre><code class="language-java">public class MergeSort {
    public static void main(String[] args) {
    	// &#x6D4B;&#x8BD5;
        int[] arr = {3,1,8,2,4,5,2,-1,0,9,10,-2,-5,4,6,-11,0,0,0,3,4};
        int[] res = sort(arr);
        System.out.println(&quot;---final result: &quot;+Arrays.toString(res));
    }
    
    public static int[] sort(int[]arr){
    	int[] res = mergeSort(arr);
        return res;
    }
    
    private static int[] mergeSort(int[] arr) {
        if(arr.length&gt;=2){ //&#x6298;&#x534A;
            int mid = arr.length/2; // &#x62C6;&#x5206;&#x62C6;&#x5206;&#xFF0C;&#x76F4;&#x5230;&#x6BCF;&#x4E2A;&#x5217;&#x8868;&#x53EA;&#x5269;&#x4E00;&#x4E2A;&#x5143;&#x7D20;
            int[] left = mergeSort(Arrays.copyOfRange(arr, 0, mid));
            int[] right = mergeSort(Arrays.copyOfRange(arr, mid, arr.length));
            return merge(left,right); // &#x7136;&#x540E;&#x628A;&#x4E24;&#x4E2A;&#x6709;&#x5E8F;&#x5E8F;&#x5217;&#x91CD;&#x7EC4;
        }
        return arr; //&#x4F1A;&#x4E00;&#x76F4;&#x9012;&#x5F52;&#x5230;&#x6BCF;&#x4E2A;&#x5E8F;&#x5217;&#x53EA;&#x5269;1&#x4E2A;&#x5143;&#x7D20;
    }
    
    private static int[] merge(int[] l1, int[] l2){
    	// l1&#x3001;l2&#x90FD;&#x662F;&#x6709;&#x5E8F;&#x5E8F;&#x5217;
        int i=0, j=0,c=0;
        int len1 = l1.length;
        int len2 = l2.length;
        int len3 = len1+len2;
        int[] res = new int[len3];
        while(i&lt;len1 &amp;&amp; j&lt;len2){ // &#x4E24;&#x4E2A;&#x5E8F;&#x5217;&#x90FD;&#x8FD8;&#x6709;&#x5143;&#x7D20;&#x65F6;
        	// &#x63D2;&#x5C0F;&#x7684;&#x90A3;&#x4E2A;&#x5143;&#x7D20;&#xFF0C;&#x7136;&#x540E;&#x79FB;&#x52A8;&#x6307;&#x9488;&#x5230;&#x4E0B;&#x4E00;&#x4F4D;
            // &#x4E5F;&#x6709;&#x4E0D;&#x8BB0;&#x5F55;&#x6307;&#x9488;&#x4F4D;&#x7F6E;&#xFF0C;&#x770B;&#x505A;&#x4E24;&#x4E2A;&#x961F;&#x5217;&#xFF0C;&#x6BCF;&#x6B21;poll&#x5E8F;&#x5217;&#x5F00;&#x5934;&#x7684;
            if(l1[i]&lt;=l2[j]){
                res[c++] = l1[i++];
            }
            else {
                res[c++] = l2[j++];
            }
        }
        // &#x6709;&#x4E00;&#x4E2A;&#x5E8F;&#x5217;&#x7A7A;&#x4E86;
        // &#x628A;&#x53E6;&#x4E00;&#x4E2A;&#x5E8F;&#x5217;&#x91CC;&#x5269;&#x4E0B;&#x7684;&#x76F4;&#x63A5;&#x5168;&#x585E;&#x8FDB;result
        while(i&lt;len1){
            res[c++] = l1[i++];
        }
        while(j&lt;len2){
            res[c++] = l2[j++];
        }
        
        /*
        // &#x6253;&#x5370;&#x672C;&#x8F6E;&#x5408;&#x5E76;&#x7684;&#x7ED3;&#x679C;
        System.out.println(&quot;---merge&quot;);
        System.out.println(&quot;---l1: &quot;+Arrays.toString(l1));
        System.out.println(&quot;---l2: &quot;+Arrays.toString(l2));
        System.out.println(&quot;---outcome: &quot;+Arrays.toString(res));
        System.out.println();*/
        
        return res; // res&#x4F9D;&#x7136;&#x662F;&#x4E2A;&#x6709;&#x5E8F;&#x5E8F;&#x5217;
    }
    
}</code></pre><p></p><h1 id="%E5%A0%86%E6%8E%92%E5%BA%8F">&#x5806;&#x6392;&#x5E8F;</h1><p>&#x4E00;&#x79CD;&#x4F7F;&#x7528;&#x6700;&#x5927;/&#x6700;&#x5C0F;&#x5806;&#x6765;&#x8BB0;&#x5F55;&#x90E8;&#x5206;&#x6BD4;&#x8F83;&#x7684;&#x8FC7;&#x7A0B;&#xFF0C;&#x4ECE;&#x800C;&#x52A0;&#x901F;&#x7684;&#x9009;&#x62E9;&#x6392;&#x5E8F;&#x3002;&#x4E0D;&#x4F7F;&#x7528;&#x989D;&#x5916;&#x7A7A;&#x95F4;&#x7684;&#x539F;&#x5730;&#x6392;&#x5E8F;&#xFF0C;&#x4F46;&#x4E0D;&#x7A33;&#x5B9A;&#x3002;&#x4E5F;&#x53EF;&#x4EE5;&#x7528;&#x6765;&#x5BFB;&#x627E;&#x6700;&#x5927;/&#x6700;&#x5C0F;&#x7684;N&#x4E2A;&#x503C;&#xFF08;top N&#x95EE;&#x9898;&#xFF09;&#x3002;</p><p>&#x770B;&#x4E0A;&#x53BB;&#x7684;&#x6837;&#x5B50;&#x662F;&#xFF1A;</p><p>{&#x6700;&#x5927;&#xFF08;&#x6216;&#x6700;&#x5C0F;&#xFF09;&#x5806;&#xFF1A;&#x8BB0;&#x5F55;&#x4E86;&#x90E8;&#x5206;&#x6BD4;&#x8F83;&#x8FC7;&#x7A0B;&#x7684;&#x65E0;&#x5E8F;&#x5E8F;&#x5217;&#xFF0C;&#x6709;&#x5E8F;&#x5E8F;&#x5217;}</p><h2 id="%E5%A0%86">&#x5806;</h2><p>&#x62FF;&#x6570;&#x7EC4;&#x5B58;&#x50A8;&#x7684;&#x5B8C;&#x5168;&#x4E8C;&#x53C9;&#x6811;&#x3002;N<sub>i</sub> &#x7684;&#x7236;&#x8282;&#x70B9;&#x4E3A;N<sub>(i-1)/2</sub> &#xFF0C;&#x5DE6;&#x5B50;&#x8282;&#x70B9;&#x4E3A;N<sub>2i+1</sub> &#xFF0C;&#x53F3;&#x5B50;&#x8282;&#x70B9;&#x4E3A;N<sub>2i+2</sub> &#x3002;</p><p>&#x6700;&#x5927;&#x5806;&#xFF1A;&#x6BCF;&#x4E2A;&#x8282;&#x70B9;&#x5927;&#x4E8E;&#x7B49;&#x4E8E;&#x5176;&#x5B50;&#x8282;&#x70B9;</p><p>&#x6700;&#x5C0F;&#x5806;&#xFF1A;&#x6BCF;&#x4E2A;&#x8282;&#x70B9;&#x5C0F;&#x4E8E;&#x7B49;&#x4E8E;&#x5176;&#x5B50;&#x8282;&#x70B9;</p><h2 id="%E6%AD%A5%E9%AA%A4">&#x6B65;&#x9AA4;</h2><ol><li>&#x6784;&#x5EFA;&#x4E00;&#x4E2A;&#x6700;&#x5927;&#xFF08;&#x6700;&#x5C0F;&#xFF09;&#x5806;&#xFF0C;&#x5373;&#x5BFB;&#x627E;&#x65E0;&#x5E8F;&#x533A;&#x4E2D;&#x7684;&#x6700;&#x5927;&#xFF08;&#x6700;&#x5C0F;&#xFF09;&#x503C;</li><li>&#x628A;&#x5806;&#x9876;&#xFF08;&#x5806;&#x91CC;&#x7684;&#x6700;&#x5927;/&#x6700;&#x5C0F;&#x503C;&#xFF09;&#x4E0E;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x5143;&#x7D20;&#x4EA4;&#x6362;&#x3002;&#x6B64;&#x65F6;&#xFF0C;len&#x7F29;&#x77ED;1&#xFF0C;&#x628A;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x5143;&#x7D20;&#x4ECE;&#x5806;&#x7684;&#x8303;&#x56F4;&#x79FB;&#x9664;&#xFF08;&#x628A;&#x6700;&#x5927;/&#x6700;&#x5C0F;&#x503C;&#x5212;&#x5165;&#x6709;&#x5E8F;&#x5E8F;&#x5217;&#x90E8;&#x5206;&#xFF09;&#x3002;</li><li>&#x7EF4;&#x62A4;&#x5806;/&#x65E0;&#x5E8F;&#x533A;&#xFF0C;&#x4F7F;&#x5B83;&#x91CD;&#x65B0;&#x6210;&#x4E3A;&#x6700;&#x5927;&#xFF08;&#x6700;&#x5C0F;&#xFF09;&#x5806;&#xFF08;&#x5373;&#x4ECE;&#x65E0;&#x5E8F;&#x533A;&#x627E;&#x51FA;&#x6700;&#x5927;/&#x6700;&#x5C0F;&#x503C;&#x3002;</li><li>&#x91CD;&#x590D;&#x7B2C;&#x4E8C;&#x3001;&#x4E09;&#x6B65;&#xFF0C;&#x76F4;&#x81F3;&#x6240;&#x6709;&#x5143;&#x7D20;&#x4ECE;&#x65E0;&#x5E8F;&#x533A;&#xFF08;&#x5806;&#xFF09;&#x5212;&#x5206;&#x81F3;&#x6709;&#x5E8F;&#x533A;&#x3002;</li></ol><pre><code class="language-java">public class HeapSort {
    public static void main(String[] args) {
        int[] arr = {3,1,8,2,4,5,2,-1,0,9,10,-2,-5,4,6,-11,0,0,0,3,4};
        sort(arr);
        System.out.println(&quot;---final result: &quot;+Arrays.toString(arr));
    }
    
    public static void sort(int[]arr){
        int len = arr.length;
        
        // &#x6784;&#x5EFA;&#x6700;&#x5927;&#x5806;
        // &#x4ECE;&#x5012;&#x6570;&#x7B2C;&#x4E8C;&#x5C42;&#xFF08;&#x975E;&#x53F6;&#x5B50;&#xFF09;&#xFF0C;&#x4F9D;&#x6B21;&#x4E0E;&#x5B50;&#x8282;&#x70B9;&#x6BD4;&#x8F83;&#x5E76;&#x628A;&#x8F83;&#x5927;&#x7684;&#x4EA4;&#x6362;&#x4E0A;&#x53BB;
        for (int i = (len-2) / 2; i &gt;= 0; i--) {
            heapify(arr, i, len);
        }
        
    	for (int i = len - 1; i &gt; 0; i--) {
            swap(arr, 0, i); // &#x53D6;&#x5806;&#x9876;&#x7684;&#x6700;&#x5927;&#x503C;&#xFF0C;&#x4E0E;&#x6700;&#x540E;&#x7684;&#x8282;&#x70B9;&#x4EA4;&#x6362;
            len--; // &#x628A;&#x8FD9;&#x8F6E;&#x7684;&#x6700;&#x5927;&#x503C;&#x5212;&#x5206;&#x51FA;&#x53BB;
            heapify(arr, 0, len); // &#x5806;&#x7684;&#x90E8;&#x5206;&#x91CD;&#x65B0;&#x6392;&#x5E8F;
        }
    }


    private static void heapify(int[] arr, int i, int len) {
        int left = 2 * i + 1;
        int right = 2 * i + 2;
        int largest = i;

        if (left &lt; len &amp;&amp; arr[left] &gt; arr[largest]) {
        	// &#x82E5;&#x5DE6;&#x5B50;&#x8282;&#x70B9;&#x5B58;&#x5728;&#xFF0C;&#x4E14;&#x6BD4;&#x7236;&#x8282;&#x70B9;&#x5927;
            largest = left;
        }

        if (right &lt; len &amp;&amp; arr[right] &gt; arr[largest]) {
        	// &#x53F3;&#x5B50;&#x8282;&#x70B9;&#x5B58;&#x5728;&#xFF0C;&#x4E14;&#x6BD4;&#x7236;&#x8282;&#x70B9;&#x5927;
            largest = right;
        }

        if (largest != i) {
            swap(arr, i, largest); // &#x628A;&#x8F83;&#x5927;&#x7684;&#x8282;&#x70B9;&#x6362;&#x4E0A;&#x53BB;
            heapify(arr, largest, len); //largest&#x73B0;&#x5728;&#x662F;&#x539F;&#x6765;&#x7684;i
            // &#x770B;&#x4EA4;&#x662F;&#x5426;&#x9700;&#x8981;&#x628A;&#x5B83;&#x7EE7;&#x7EED;&#x5F80;&#x4E0B;&#x7F6E;&#x6362;
        }
    }

    private static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}</code></pre><p></p><p></p><h1 id="%E5%9F%BA%E6%95%B0%E6%8E%92%E5%BA%8F">&#x57FA;&#x6570;&#x6392;&#x5E8F;</h1><p>&#x6309;&#x4F4D;&#x6392;&#x5E8F;&#xFF0C;&#x7528;&#x4E8E;&#x6392;&#x5E8F;&#x6574;&#x6570;&#x6216;&#x53EF;&#x4EE5;&#x7528;&#x6574;&#x6570;&#x8868;&#x793A;&#x7684;&#x6570;&#x636E;&#xFF08;&#x5982;&#x65E5;&#x671F;&#x3001;&#x5B57;&#x7B26;&#xFF09;&#x3002;</p><p>&#x4F8B;&#xFF1A;[12, 25, 11, 15, 46, 42, 23, 35, 40,13]</p><p>&#x6309;&#x4E2A;&#x4F4D;&#x6392;&#x5E8F;</p><p>0: 40----1: 11----2:12, 42----3: 23, 13---- 5: 25, 15, 35-&#x2014;6:46</p><p>[40, 11, 12, 42, 23, 13, 25, 15, 35, 46]</p><p>&#x6309;&#x5341;&#x4F4D;&#x6392;&#x5E8F;</p><p>1: 11, 12, 13, 15----2: 23, 25-&#x2014;3:35-&#x2014;4:40, 42, 46</p><p>[11, 12, 13, 15, 23, 25, 35, 40, 42, 46] </p><figure class="kg-card kg-code-card"><pre><code class="language-javascript">//LSD Radix Sort
var counter = [];
function radixSort(arr, maxDigit) {
    var mod = 10;
    var dev = 1;
    for (var i = 0; i &lt; maxDigit; i++, dev *= 10, mod *= 10) {
        for(var j = 0; j &lt; arr.length; j++) {
            var bucket = parseInt((arr[j] % mod) / dev);
            if(counter[bucket]==null) {
                counter[bucket] = [];
            }
            counter[bucket].push(arr[j]);
        }
        var pos = 0;
        for(var j = 0; j &lt; counter.length; j++) {
            var value = null;
            if(counter[j]!=null) {
                while ((value = counter[j].shift()) != null) {
                      arr[pos++] = value;
                }
          }
        }
    }
    return arr;
}</code></pre><figcaption>https://www.runoob.com/w3cnote/radix-sort.html</figcaption></figure><h1 id="%E6%A1%B6%E6%8E%92%E5%BA%8F">&#x6876;&#x6392;&#x5E8F;</h1><p>&#x50CF;&#x662F;&#x641E;HashMap&#x4E00;&#x6837;&#x5148;&#x641E;&#x4E00;&#x5806;&#x6876;&#xFF0C;&#x7136;&#x540E;&#x6309;&#x67D0;&#x79CD;&#x51FD;&#x6570;&#x6620;&#x5C04;&#x628A;&#x5143;&#x7D20;&#x585E;&#x8FDB;&#x5404;&#x4E2A;&#x6876;&#xFF08;&#x4F46;&#x5176;&#x5B9E;&#x57FA;&#x672C;&#x5C31;&#x662F;&#x628A;&#x6574;&#x4E2A;&#x533A;&#x95F4;&#x5206;&#x6210;n&#x6BB5;&#xFF0C;&#x7136;&#x540E;&#x628A;&#x5C5E;&#x4E8E;&#x8BE5;&#x6BB5;&#x7684;&#x5143;&#x7D20;&#x653E;&#x8FDB;&#x8BE5;&#x6BB5;&#xFF09;&#xFF0C;&#x6BCF;&#x4E2A;&#x6876;&#x5206;&#x522B;&#x6392;&#x5E8F;&#xFF0C;&#x6700;&#x540E;&#x518D;&#x628A;&#x6BCF;&#x4E2A;&#x6876;&#x91CC;&#x6392;&#x597D;&#x5E8F;&#x7684;&#x5012;&#x51FA;&#x6765;&#x62FC;&#x5728;&#x4E00;&#x8D77;&#x3002;&#x3002;&#x3002;</p><figure class="kg-card kg-code-card"><pre><code class="language-javascript">function bucketSort(arr, bucketSize) {
    if (arr.length === 0) {
      return arr;
    }

    var i;
    var minValue = arr[0];
    var maxValue = arr[0];
    for (i = 1; i &lt; arr.length; i++) {
      if (arr[i] &lt; minValue) {
          minValue = arr[i];
      } else if (arr[i] &gt; maxValue) {
          maxValue = arr[i];
      }
    }

    //&#x6876;&#x7684;&#x521D;&#x59CB;&#x5316;
    var DEFAULT_BUCKET_SIZE = 5; // &#x8BBE;&#x7F6E;&#x6876;&#x7684;&#x9ED8;&#x8BA4;&#x5BB9;&#x91CF;&#x4E3A;5
    bucketSize = bucketSize || DEFAULT_BUCKET_SIZE;
    var bucketCount = Math.floor((maxValue - minValue) / bucketSize) + 1;  
    var buckets = new Array(bucketCount);
    for (i = 0; i &lt; buckets.length; i++) {
        buckets[i] = [];
    }

    //&#x5229;&#x7528;&#x6620;&#x5C04;&#x51FD;&#x6570;&#x5C06;&#x6570;&#x636E;&#x5206;&#x914D;&#x5230;&#x5404;&#x4E2A;&#x6876;&#x4E2D;
    for (i = 0; i &lt; arr.length; i++) {
        buckets[Math.floor((arr[i] - minValue) / bucketSize)].push(arr[i]);
    }

    arr.length = 0;
    for (i = 0; i &lt; buckets.length; i++) {
        insertionSort(buckets[i]);   // &#x5BF9;&#x6BCF;&#x4E2A;&#x6876;&#x8FDB;&#x884C;&#x6392;&#x5E8F;&#xFF0C;&#x8FD9;&#x91CC;&#x4F7F;&#x7528;&#x4E86;&#x63D2;&#x5165;&#x6392;&#x5E8F;
        for (var j = 0; j &lt; buckets[i].length; j++) {
            arr.push(buckets[i][j]);                      
        }
    }

    return arr;
}</code></pre><figcaption>https://www.runoob.com/w3cnote/bucket-sort.html</figcaption></figure>]]></content:encoded></item></channel></rss>