mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-12 15:36:05 +00:00
deploy
This commit is contained in:
@@ -201,7 +201,13 @@
|
||||
|
||||
<li class="md-select__item">
|
||||
<a href="/" hreflang="zh" class="md-select__link">
|
||||
中文
|
||||
简体中文
|
||||
</a>
|
||||
</li>
|
||||
|
||||
<li class="md-select__item">
|
||||
<a href="/zh-hant/" hreflang="zh-Hant" class="md-select__link">
|
||||
繁體中文
|
||||
</a>
|
||||
</li>
|
||||
|
||||
@@ -393,7 +399,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
0.1 About This Book
|
||||
0.1 About this book
|
||||
</span>
|
||||
|
||||
|
||||
@@ -414,7 +420,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
0.2 How to Read
|
||||
0.2 How to read
|
||||
</span>
|
||||
|
||||
|
||||
@@ -491,7 +497,7 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2m0 16H5V5h14v14M6.2 7.7h5v1.5h-5V7.7m6.8 8.1h5v1.5h-5v-1.5m0-2.6h5v1.5h-5v-1.5M8 18h1.5v-2h2v-1.5h-2v-2H8v2H6V16h2v2m6.1-7.1 1.4-1.4 1.4 1.4 1.1-1-1.4-1.4L18 7.1 16.9 6l-1.4 1.4L14.1 6 13 7.1l1.4 1.4L13 9.9l1.1 1Z"/></svg>
|
||||
|
||||
<span class="md-ellipsis">
|
||||
Chapter 1. Introduction to Algorithms
|
||||
Chapter 1. Introduction to algorithms
|
||||
</span>
|
||||
|
||||
|
||||
@@ -507,7 +513,7 @@
|
||||
<nav class="md-nav" data-md-level="1" aria-labelledby="__nav_2_label" aria-expanded="false">
|
||||
<label class="md-nav__title" for="__nav_2">
|
||||
<span class="md-nav__icon md-icon"></span>
|
||||
Chapter 1. Introduction to Algorithms
|
||||
Chapter 1. Introduction to algorithms
|
||||
</label>
|
||||
<ul class="md-nav__list" data-md-scrollfix>
|
||||
|
||||
@@ -524,7 +530,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
1.1 Algorithms are Everywhere
|
||||
1.1 Algorithms are everywhere
|
||||
</span>
|
||||
|
||||
|
||||
@@ -545,7 +551,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
1.2 What is an Algorithm
|
||||
1.2 What is an algorithm
|
||||
</span>
|
||||
|
||||
|
||||
@@ -626,7 +632,7 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M6 2h12v6l-4 4 4 4v6H6v-6l4-4-4-4V2m10 14.5-4-4-4 4V20h8v-3.5m-4-5 4-4V4H8v3.5l4 4M10 6h4v.75l-2 2-2-2V6Z"/></svg>
|
||||
|
||||
<span class="md-ellipsis">
|
||||
Chapter 2. Complexity Analysis
|
||||
Chapter 2. Complexity analysis
|
||||
</span>
|
||||
|
||||
|
||||
@@ -642,7 +648,7 @@
|
||||
<nav class="md-nav" data-md-level="1" aria-labelledby="__nav_3_label" aria-expanded="false">
|
||||
<label class="md-nav__title" for="__nav_3">
|
||||
<span class="md-nav__icon md-icon"></span>
|
||||
Chapter 2. Complexity Analysis
|
||||
Chapter 2. Complexity analysis
|
||||
</label>
|
||||
<ul class="md-nav__list" data-md-scrollfix>
|
||||
|
||||
@@ -659,7 +665,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
2.1 Algorithm Efficiency Assessment
|
||||
2.1 Algorithm efficiency assessment
|
||||
</span>
|
||||
|
||||
|
||||
@@ -680,7 +686,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
2.2 Iteration and Recursion
|
||||
2.2 Iteration and recursion
|
||||
</span>
|
||||
|
||||
|
||||
@@ -701,7 +707,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
2.3 Time Complexity
|
||||
2.3 Time complexity
|
||||
</span>
|
||||
|
||||
|
||||
@@ -722,7 +728,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
2.4 Space Complexity
|
||||
2.4 Space complexity
|
||||
</span>
|
||||
|
||||
|
||||
@@ -803,7 +809,7 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M11 13.5v8H3v-8h8m-2 2H5v4h4v-4M12 2l5.5 9h-11L12 2m0 3.86L10.08 9h3.84L12 5.86M17.5 13c2.5 0 4.5 2 4.5 4.5S20 22 17.5 22 13 20 13 17.5s2-4.5 4.5-4.5m0 2a2.5 2.5 0 0 0-2.5 2.5 2.5 2.5 0 0 0 2.5 2.5 2.5 2.5 0 0 0 2.5-2.5 2.5 2.5 0 0 0-2.5-2.5Z"/></svg>
|
||||
|
||||
<span class="md-ellipsis">
|
||||
Chapter 3. Data Structures
|
||||
Chapter 3. Data structures
|
||||
</span>
|
||||
|
||||
|
||||
@@ -819,7 +825,7 @@
|
||||
<nav class="md-nav" data-md-level="1" aria-labelledby="__nav_4_label" aria-expanded="false">
|
||||
<label class="md-nav__title" for="__nav_4">
|
||||
<span class="md-nav__icon md-icon"></span>
|
||||
Chapter 3. Data Structures
|
||||
Chapter 3. Data structures
|
||||
</label>
|
||||
<ul class="md-nav__list" data-md-scrollfix>
|
||||
|
||||
@@ -836,7 +842,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
3.1 Classification of Data Structures
|
||||
3.1 Classification of data structures
|
||||
</span>
|
||||
|
||||
|
||||
@@ -857,7 +863,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
3.2 Fundamental Data Types
|
||||
3.2 Fundamental data types
|
||||
</span>
|
||||
|
||||
|
||||
@@ -878,7 +884,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
3.3 Number Encoding *
|
||||
3.3 Number encoding *
|
||||
</span>
|
||||
|
||||
|
||||
@@ -899,7 +905,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
3.4 Character Encoding *
|
||||
3.4 Character encoding *
|
||||
</span>
|
||||
|
||||
|
||||
@@ -982,7 +988,7 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M3 5v14h17V5H3m4 2v2H5V7h2m-2 6v-2h2v2H5m0 2h2v2H5v-2m13 2H9v-2h9v2m0-4H9v-2h9v2m0-4H9V7h9v2Z"/></svg>
|
||||
|
||||
<span class="md-ellipsis">
|
||||
Chapter 4. Array and Linked List
|
||||
Chapter 4. Array and linked list
|
||||
</span>
|
||||
|
||||
|
||||
@@ -998,7 +1004,7 @@
|
||||
<nav class="md-nav" data-md-level="1" aria-labelledby="__nav_5_label" aria-expanded="true">
|
||||
<label class="md-nav__title" for="__nav_5">
|
||||
<span class="md-nav__icon md-icon"></span>
|
||||
Chapter 4. Array and Linked List
|
||||
Chapter 4. Array and linked list
|
||||
</label>
|
||||
<ul class="md-nav__list" data-md-scrollfix>
|
||||
|
||||
@@ -1036,7 +1042,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
4.2 Linked List
|
||||
4.2 Linked list
|
||||
</span>
|
||||
|
||||
|
||||
@@ -1101,17 +1107,17 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#431-common-list-operations" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
4.3.1 Common List Operations
|
||||
4.3.1 Common list operations
|
||||
</span>
|
||||
</a>
|
||||
|
||||
<nav class="md-nav" aria-label="4.3.1 Common List Operations">
|
||||
<nav class="md-nav" aria-label="4.3.1 Common list operations">
|
||||
<ul class="md-nav__list">
|
||||
|
||||
<li class="md-nav__item">
|
||||
<a href="#1-initializing-a-list" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
1. Initializing a List
|
||||
1. Initializing a list
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@@ -1120,7 +1126,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#2-accessing-elements" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
2. Accessing Elements
|
||||
2. Accessing elements
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@@ -1129,7 +1135,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#3-inserting-and-removing-elements" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
3. Inserting and Removing Elements
|
||||
3. Inserting and removing elements
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@@ -1138,7 +1144,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#4-iterating-the-list" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
4. Iterating the List
|
||||
4. Iterating the list
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@@ -1147,7 +1153,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#5-concatenating-lists" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
5. Concatenating Lists
|
||||
5. Concatenating lists
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@@ -1156,7 +1162,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#6-sorting-the-list" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
6. Sorting the List
|
||||
6. Sorting the list
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@@ -1170,7 +1176,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#432-list-implementation" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
4.3.2 List Implementation
|
||||
4.3.2 List implementation
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@@ -1196,7 +1202,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
4.4 Memory and Cache
|
||||
4.4 Memory and cache
|
||||
</span>
|
||||
|
||||
|
||||
@@ -1275,7 +1281,7 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M17.36 20.2v-5.38h1.79V22H3v-7.18h1.8v5.38h12.56M6.77 14.32l.37-1.76 8.79 1.85-.37 1.76-8.79-1.85m1.16-4.21.76-1.61 8.14 3.78-.76 1.62-8.14-3.79m2.26-3.99 1.15-1.38 6.9 5.76-1.15 1.37-6.9-5.75m4.45-4.25L20 9.08l-1.44 1.07-5.36-7.21 1.44-1.07M6.59 18.41v-1.8h8.98v1.8H6.59Z"/></svg>
|
||||
|
||||
<span class="md-ellipsis">
|
||||
Chapter 5. Stack and Queue
|
||||
Chapter 5. Stack and queue
|
||||
</span>
|
||||
|
||||
|
||||
@@ -1291,7 +1297,7 @@
|
||||
<nav class="md-nav" data-md-level="1" aria-labelledby="__nav_6_label" aria-expanded="false">
|
||||
<label class="md-nav__title" for="__nav_6">
|
||||
<span class="md-nav__icon md-icon"></span>
|
||||
Chapter 5. Stack and Queue
|
||||
Chapter 5. Stack and queue
|
||||
</label>
|
||||
<ul class="md-nav__list" data-md-scrollfix>
|
||||
|
||||
@@ -1350,7 +1356,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
5.3 Double-ended Queue
|
||||
5.3 Double-ended queue
|
||||
</span>
|
||||
|
||||
|
||||
@@ -1429,7 +1435,7 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M19.3 17.89c1.32-2.1.7-4.89-1.41-6.21a4.52 4.52 0 0 0-6.21 1.41C10.36 15.2 11 18 13.09 19.3c1.47.92 3.33.92 4.8 0L21 22.39 22.39 21l-3.09-3.11m-2-.62c-.98.98-2.56.97-3.54 0-.97-.98-.97-2.56.01-3.54.97-.97 2.55-.97 3.53 0 .96.99.95 2.57-.03 3.54h.03M19 4H5a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h5.81a6.3 6.3 0 0 1-1.31-2H5v-4h4.18c.16-.71.43-1.39.82-2H5V8h6v2.81a6.3 6.3 0 0 1 2-1.31V8h6v2a6.499 6.499 0 0 1 2 2V6a2 2 0 0 0-2-2Z"/></svg>
|
||||
|
||||
<span class="md-ellipsis">
|
||||
Chapter 6. Hash Table
|
||||
Chapter 6. Hash table
|
||||
</span>
|
||||
|
||||
|
||||
@@ -1445,7 +1451,7 @@
|
||||
<nav class="md-nav" data-md-level="1" aria-labelledby="__nav_7_label" aria-expanded="false">
|
||||
<label class="md-nav__title" for="__nav_7">
|
||||
<span class="md-nav__icon md-icon"></span>
|
||||
Chapter 6. Hash Table
|
||||
Chapter 6. Hash table
|
||||
</label>
|
||||
<ul class="md-nav__list" data-md-scrollfix>
|
||||
|
||||
@@ -1462,7 +1468,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
6.1 Hash Table
|
||||
6.1 Hash table
|
||||
</span>
|
||||
|
||||
|
||||
@@ -1483,7 +1489,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
6.2 Hash Collision
|
||||
6.2 Hash collision
|
||||
</span>
|
||||
|
||||
|
||||
@@ -1504,7 +1510,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
6.3 Hash Algorithm
|
||||
6.3 Hash algorithm
|
||||
</span>
|
||||
|
||||
|
||||
@@ -2079,17 +2085,17 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#431-common-list-operations" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
4.3.1 Common List Operations
|
||||
4.3.1 Common list operations
|
||||
</span>
|
||||
</a>
|
||||
|
||||
<nav class="md-nav" aria-label="4.3.1 Common List Operations">
|
||||
<nav class="md-nav" aria-label="4.3.1 Common list operations">
|
||||
<ul class="md-nav__list">
|
||||
|
||||
<li class="md-nav__item">
|
||||
<a href="#1-initializing-a-list" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
1. Initializing a List
|
||||
1. Initializing a list
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@@ -2098,7 +2104,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#2-accessing-elements" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
2. Accessing Elements
|
||||
2. Accessing elements
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@@ -2107,7 +2113,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#3-inserting-and-removing-elements" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
3. Inserting and Removing Elements
|
||||
3. Inserting and removing elements
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@@ -2116,7 +2122,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#4-iterating-the-list" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
4. Iterating the List
|
||||
4. Iterating the list
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@@ -2125,7 +2131,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#5-concatenating-lists" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
5. Concatenating Lists
|
||||
5. Concatenating lists
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@@ -2134,7 +2140,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#6-sorting-the-list" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
6. Sorting the List
|
||||
6. Sorting the list
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@@ -2148,7 +2154,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#432-list-implementation" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
4.3.2 List Implementation
|
||||
4.3.2 List implementation
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@@ -2199,8 +2205,8 @@
|
||||
<p>When implementing lists using arrays, <strong>the immutability of length reduces the practicality of the list</strong>. This is because predicting the amount of data to be stored in advance is often challenging, making it difficult to choose an appropriate list length. If the length is too small, it may not meet the requirements; if too large, it may waste memory space.</p>
|
||||
<p>To solve this problem, we can implement lists using a "dynamic array." It inherits the advantages of arrays and can dynamically expand during program execution.</p>
|
||||
<p>In fact, <strong>many programming languages' standard libraries implement lists using dynamic arrays</strong>, such as Python's <code>list</code>, Java's <code>ArrayList</code>, C++'s <code>vector</code>, and C#'s <code>List</code>. In the following discussion, we will consider "list" and "dynamic array" as synonymous concepts.</p>
|
||||
<h2 id="431-common-list-operations">4.3.1 Common List Operations<a class="headerlink" href="#431-common-list-operations" title="Permanent link">¶</a></h2>
|
||||
<h3 id="1-initializing-a-list">1. Initializing a List<a class="headerlink" href="#1-initializing-a-list" title="Permanent link">¶</a></h3>
|
||||
<h2 id="431-common-list-operations">4.3.1 Common list operations<a class="headerlink" href="#431-common-list-operations" title="Permanent link">¶</a></h2>
|
||||
<h3 id="1-initializing-a-list">1. Initializing a list<a class="headerlink" href="#1-initializing-a-list" title="Permanent link">¶</a></h3>
|
||||
<p>We typically use two initialization methods: "without initial values" and "with initial values".</p>
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="1:13"><input checked="checked" id="__tabbed_1_1" name="__tabbed_1" type="radio" /><input id="__tabbed_1_2" name="__tabbed_1" type="radio" /><input id="__tabbed_1_3" name="__tabbed_1" type="radio" /><input id="__tabbed_1_4" name="__tabbed_1" type="radio" /><input id="__tabbed_1_5" name="__tabbed_1" type="radio" /><input id="__tabbed_1_6" name="__tabbed_1" type="radio" /><input id="__tabbed_1_7" name="__tabbed_1" type="radio" /><input id="__tabbed_1_8" name="__tabbed_1" type="radio" /><input id="__tabbed_1_9" name="__tabbed_1" type="radio" /><input id="__tabbed_1_10" name="__tabbed_1" type="radio" /><input id="__tabbed_1_11" name="__tabbed_1" type="radio" /><input id="__tabbed_1_12" name="__tabbed_1" type="radio" /><input id="__tabbed_1_13" name="__tabbed_1" type="radio" /><div class="tabbed-labels"><label for="__tabbed_1_1">Python</label><label for="__tabbed_1_2">C++</label><label for="__tabbed_1_3">Java</label><label for="__tabbed_1_4">C#</label><label for="__tabbed_1_5">Go</label><label for="__tabbed_1_6">Swift</label><label for="__tabbed_1_7">JS</label><label for="__tabbed_1_8">TS</label><label for="__tabbed_1_9">Dart</label><label for="__tabbed_1_10">Rust</label><label for="__tabbed_1_11">C</label><label for="__tabbed_1_12">Kotlin</label><label for="__tabbed_1_13">Zig</label></div>
|
||||
<div class="tabbed-content">
|
||||
@@ -2304,7 +2310,7 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<h3 id="2-accessing-elements">2. Accessing Elements<a class="headerlink" href="#2-accessing-elements" title="Permanent link">¶</a></h3>
|
||||
<h3 id="2-accessing-elements">2. Accessing elements<a class="headerlink" href="#2-accessing-elements" title="Permanent link">¶</a></h3>
|
||||
<p>Lists are essentially arrays, thus they can access and update elements in <span class="arithmatex">\(O(1)\)</span> time, which is very efficient.</p>
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="2:13"><input checked="checked" id="__tabbed_2_1" name="__tabbed_2" type="radio" /><input id="__tabbed_2_2" name="__tabbed_2" type="radio" /><input id="__tabbed_2_3" name="__tabbed_2" type="radio" /><input id="__tabbed_2_4" name="__tabbed_2" type="radio" /><input id="__tabbed_2_5" name="__tabbed_2" type="radio" /><input id="__tabbed_2_6" name="__tabbed_2" type="radio" /><input id="__tabbed_2_7" name="__tabbed_2" type="radio" /><input id="__tabbed_2_8" name="__tabbed_2" type="radio" /><input id="__tabbed_2_9" name="__tabbed_2" type="radio" /><input id="__tabbed_2_10" name="__tabbed_2" type="radio" /><input id="__tabbed_2_11" name="__tabbed_2" type="radio" /><input id="__tabbed_2_12" name="__tabbed_2" type="radio" /><input id="__tabbed_2_13" name="__tabbed_2" type="radio" /><div class="tabbed-labels"><label for="__tabbed_2_1">Python</label><label for="__tabbed_2_2">C++</label><label for="__tabbed_2_3">Java</label><label for="__tabbed_2_4">C#</label><label for="__tabbed_2_5">Go</label><label for="__tabbed_2_6">Swift</label><label for="__tabbed_2_7">JS</label><label for="__tabbed_2_8">TS</label><label for="__tabbed_2_9">Dart</label><label for="__tabbed_2_10">Rust</label><label for="__tabbed_2_11">C</label><label for="__tabbed_2_12">Kotlin</label><label for="__tabbed_2_13">Zig</label></div>
|
||||
<div class="tabbed-content">
|
||||
@@ -2405,7 +2411,7 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<h3 id="3-inserting-and-removing-elements">3. Inserting and Removing Elements<a class="headerlink" href="#3-inserting-and-removing-elements" title="Permanent link">¶</a></h3>
|
||||
<h3 id="3-inserting-and-removing-elements">3. Inserting and removing elements<a class="headerlink" href="#3-inserting-and-removing-elements" title="Permanent link">¶</a></h3>
|
||||
<p>Compared to arrays, lists offer more flexibility in adding and removing elements. While adding elements to the end of a list is an <span class="arithmatex">\(O(1)\)</span> operation, the efficiency of inserting and removing elements elsewhere in the list remains the same as in arrays, with a time complexity of <span class="arithmatex">\(O(n)\)</span>.</p>
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="3:13"><input checked="checked" id="__tabbed_3_1" name="__tabbed_3" type="radio" /><input id="__tabbed_3_2" name="__tabbed_3" type="radio" /><input id="__tabbed_3_3" name="__tabbed_3" type="radio" /><input id="__tabbed_3_4" name="__tabbed_3" type="radio" /><input id="__tabbed_3_5" name="__tabbed_3" type="radio" /><input id="__tabbed_3_6" name="__tabbed_3" type="radio" /><input id="__tabbed_3_7" name="__tabbed_3" type="radio" /><input id="__tabbed_3_8" name="__tabbed_3" type="radio" /><input id="__tabbed_3_9" name="__tabbed_3" type="radio" /><input id="__tabbed_3_10" name="__tabbed_3" type="radio" /><input id="__tabbed_3_11" name="__tabbed_3" type="radio" /><input id="__tabbed_3_12" name="__tabbed_3" type="radio" /><input id="__tabbed_3_13" name="__tabbed_3" type="radio" /><div class="tabbed-labels"><label for="__tabbed_3_1">Python</label><label for="__tabbed_3_2">C++</label><label for="__tabbed_3_3">Java</label><label for="__tabbed_3_4">C#</label><label for="__tabbed_3_5">Go</label><label for="__tabbed_3_6">Swift</label><label for="__tabbed_3_7">JS</label><label for="__tabbed_3_8">TS</label><label for="__tabbed_3_9">Dart</label><label for="__tabbed_3_10">Rust</label><label for="__tabbed_3_11">C</label><label for="__tabbed_3_12">Kotlin</label><label for="__tabbed_3_13">Zig</label></div>
|
||||
<div class="tabbed-content">
|
||||
@@ -2617,7 +2623,7 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<h3 id="4-iterating-the-list">4. Iterating the List<a class="headerlink" href="#4-iterating-the-list" title="Permanent link">¶</a></h3>
|
||||
<h3 id="4-iterating-the-list">4. Iterating the list<a class="headerlink" href="#4-iterating-the-list" title="Permanent link">¶</a></h3>
|
||||
<p>Similar to arrays, lists can be iterated either by using indices or by directly iterating through each element.</p>
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="4:13"><input checked="checked" id="__tabbed_4_1" name="__tabbed_4" type="radio" /><input id="__tabbed_4_2" name="__tabbed_4" type="radio" /><input id="__tabbed_4_3" name="__tabbed_4" type="radio" /><input id="__tabbed_4_4" name="__tabbed_4" type="radio" /><input id="__tabbed_4_5" name="__tabbed_4" type="radio" /><input id="__tabbed_4_6" name="__tabbed_4" type="radio" /><input id="__tabbed_4_7" name="__tabbed_4" type="radio" /><input id="__tabbed_4_8" name="__tabbed_4" type="radio" /><input id="__tabbed_4_9" name="__tabbed_4" type="radio" /><input id="__tabbed_4_10" name="__tabbed_4" type="radio" /><input id="__tabbed_4_11" name="__tabbed_4" type="radio" /><input id="__tabbed_4_12" name="__tabbed_4" type="radio" /><input id="__tabbed_4_13" name="__tabbed_4" type="radio" /><div class="tabbed-labels"><label for="__tabbed_4_1">Python</label><label for="__tabbed_4_2">C++</label><label for="__tabbed_4_3">Java</label><label for="__tabbed_4_4">C#</label><label for="__tabbed_4_5">Go</label><label for="__tabbed_4_6">Swift</label><label for="__tabbed_4_7">JS</label><label for="__tabbed_4_8">TS</label><label for="__tabbed_4_9">Dart</label><label for="__tabbed_4_10">Rust</label><label for="__tabbed_4_11">C</label><label for="__tabbed_4_12">Kotlin</label><label for="__tabbed_4_13">Zig</label></div>
|
||||
<div class="tabbed-content">
|
||||
@@ -2782,7 +2788,7 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<h3 id="5-concatenating-lists">5. Concatenating Lists<a class="headerlink" href="#5-concatenating-lists" title="Permanent link">¶</a></h3>
|
||||
<h3 id="5-concatenating-lists">5. Concatenating lists<a class="headerlink" href="#5-concatenating-lists" title="Permanent link">¶</a></h3>
|
||||
<p>Given a new list <code>nums1</code>, we can append it to the end of the original list.</p>
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="5:13"><input checked="checked" id="__tabbed_5_1" name="__tabbed_5" type="radio" /><input id="__tabbed_5_2" name="__tabbed_5" type="radio" /><input id="__tabbed_5_3" name="__tabbed_5" type="radio" /><input id="__tabbed_5_4" name="__tabbed_5" type="radio" /><input id="__tabbed_5_5" name="__tabbed_5" type="radio" /><input id="__tabbed_5_6" name="__tabbed_5" type="radio" /><input id="__tabbed_5_7" name="__tabbed_5" type="radio" /><input id="__tabbed_5_8" name="__tabbed_5" type="radio" /><input id="__tabbed_5_9" name="__tabbed_5" type="radio" /><input id="__tabbed_5_10" name="__tabbed_5" type="radio" /><input id="__tabbed_5_11" name="__tabbed_5" type="radio" /><input id="__tabbed_5_12" name="__tabbed_5" type="radio" /><input id="__tabbed_5_13" name="__tabbed_5" type="radio" /><div class="tabbed-labels"><label for="__tabbed_5_1">Python</label><label for="__tabbed_5_2">C++</label><label for="__tabbed_5_3">Java</label><label for="__tabbed_5_4">C#</label><label for="__tabbed_5_5">Go</label><label for="__tabbed_5_6">Swift</label><label for="__tabbed_5_7">JS</label><label for="__tabbed_5_8">TS</label><label for="__tabbed_5_9">Dart</label><label for="__tabbed_5_10">Rust</label><label for="__tabbed_5_11">C</label><label for="__tabbed_5_12">Kotlin</label><label for="__tabbed_5_13">Zig</label></div>
|
||||
<div class="tabbed-content">
|
||||
@@ -2865,7 +2871,7 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<h3 id="6-sorting-the-list">6. Sorting the List<a class="headerlink" href="#6-sorting-the-list" title="Permanent link">¶</a></h3>
|
||||
<h3 id="6-sorting-the-list">6. Sorting the list<a class="headerlink" href="#6-sorting-the-list" title="Permanent link">¶</a></h3>
|
||||
<p>Once the list is sorted, we can employ algorithms commonly used in array-related algorithm problems, such as "binary search" and "two-pointer" algorithms.</p>
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="6:13"><input checked="checked" id="__tabbed_6_1" name="__tabbed_6" type="radio" /><input id="__tabbed_6_2" name="__tabbed_6" type="radio" /><input id="__tabbed_6_3" name="__tabbed_6" type="radio" /><input id="__tabbed_6_4" name="__tabbed_6" type="radio" /><input id="__tabbed_6_5" name="__tabbed_6" type="radio" /><input id="__tabbed_6_6" name="__tabbed_6" type="radio" /><input id="__tabbed_6_7" name="__tabbed_6" type="radio" /><input id="__tabbed_6_8" name="__tabbed_6" type="radio" /><input id="__tabbed_6_9" name="__tabbed_6" type="radio" /><input id="__tabbed_6_10" name="__tabbed_6" type="radio" /><input id="__tabbed_6_11" name="__tabbed_6" type="radio" /><input id="__tabbed_6_12" name="__tabbed_6" type="radio" /><input id="__tabbed_6_13" name="__tabbed_6" type="radio" /><div class="tabbed-labels"><label for="__tabbed_6_1">Python</label><label for="__tabbed_6_2">C++</label><label for="__tabbed_6_3">Java</label><label for="__tabbed_6_4">C#</label><label for="__tabbed_6_5">Go</label><label for="__tabbed_6_6">Swift</label><label for="__tabbed_6_7">JS</label><label for="__tabbed_6_8">TS</label><label for="__tabbed_6_9">Dart</label><label for="__tabbed_6_10">Rust</label><label for="__tabbed_6_11">C</label><label for="__tabbed_6_12">Kotlin</label><label for="__tabbed_6_13">Zig</label></div>
|
||||
<div class="tabbed-content">
|
||||
@@ -2934,13 +2940,13 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<h2 id="432-list-implementation">4.3.2 List Implementation<a class="headerlink" href="#432-list-implementation" title="Permanent link">¶</a></h2>
|
||||
<h2 id="432-list-implementation">4.3.2 List implementation<a class="headerlink" href="#432-list-implementation" title="Permanent link">¶</a></h2>
|
||||
<p>Many programming languages come with built-in lists, including Java, C++, Python, etc. Their implementations tend to be intricate, featuring carefully considered settings for various parameters, like initial capacity and expansion factors. Readers who are curious can delve into the source code for further learning.</p>
|
||||
<p>To enhance our understanding of how lists work, we will attempt to implement a simplified version of a list, focusing on three crucial design aspects:</p>
|
||||
<ul>
|
||||
<li><strong>Initial Capacity</strong>: Choose a reasonable initial capacity for the array. In this example, we choose 10 as the initial capacity.</li>
|
||||
<li><strong>Size Recording</strong>: Declare a variable <code>size</code> to record the current number of elements in the list, updating in real-time with element insertion and deletion. With this variable, we can locate the end of the list and determine whether expansion is needed.</li>
|
||||
<li><strong>Expansion Mechanism</strong>: If the list reaches full capacity upon an element insertion, an expansion process is required. This involves creating a larger array based on the expansion factor, and then transferring all elements from the current array to the new one. In this example, we stipulate that the array size should double with each expansion.</li>
|
||||
<li><strong>Initial capacity</strong>: Choose a reasonable initial capacity for the array. In this example, we choose 10 as the initial capacity.</li>
|
||||
<li><strong>Size recording</strong>: Declare a variable <code>size</code> to record the current number of elements in the list, updating in real-time with element insertion and deletion. With this variable, we can locate the end of the list and determine whether expansion is needed.</li>
|
||||
<li><strong>Expansion mechanism</strong>: If the list reaches full capacity upon an element insertion, an expansion process is required. This involves creating a larger array based on the expansion factor, and then transferring all elements from the current array to the new one. In this example, we stipulate that the array size should double with each expansion.</li>
|
||||
</ul>
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="7:14"><input checked="checked" id="__tabbed_7_1" name="__tabbed_7" type="radio" /><input id="__tabbed_7_2" name="__tabbed_7" type="radio" /><input id="__tabbed_7_3" name="__tabbed_7" type="radio" /><input id="__tabbed_7_4" name="__tabbed_7" type="radio" /><input id="__tabbed_7_5" name="__tabbed_7" type="radio" /><input id="__tabbed_7_6" name="__tabbed_7" type="radio" /><input id="__tabbed_7_7" name="__tabbed_7" type="radio" /><input id="__tabbed_7_8" name="__tabbed_7" type="radio" /><input id="__tabbed_7_9" name="__tabbed_7" type="radio" /><input id="__tabbed_7_10" name="__tabbed_7" type="radio" /><input id="__tabbed_7_11" name="__tabbed_7" type="radio" /><input id="__tabbed_7_12" name="__tabbed_7" type="radio" /><input id="__tabbed_7_13" name="__tabbed_7" type="radio" /><input id="__tabbed_7_14" name="__tabbed_7" type="radio" /><div class="tabbed-labels"><label for="__tabbed_7_1">Python</label><label for="__tabbed_7_2">C++</label><label for="__tabbed_7_3">Java</label><label for="__tabbed_7_4">C#</label><label for="__tabbed_7_5">Go</label><label for="__tabbed_7_6">Swift</label><label for="__tabbed_7_7">JS</label><label for="__tabbed_7_8">TS</label><label for="__tabbed_7_9">Dart</label><label for="__tabbed_7_10">Rust</label><label for="__tabbed_7_11">C</label><label for="__tabbed_7_12">Kotlin</label><label for="__tabbed_7_13">Ruby</label><label for="__tabbed_7_14">Zig</label></div>
|
||||
<div class="tabbed-content">
|
||||
@@ -4396,7 +4402,7 @@ aria-label="Footer"
|
||||
<a
|
||||
href="../linked_list/"
|
||||
class="md-footer__link md-footer__link--prev"
|
||||
aria-label="Previous: 4.2 Linked List"
|
||||
aria-label="Previous: 4.2 Linked list"
|
||||
rel="prev"
|
||||
>
|
||||
<div class="md-footer__button md-icon">
|
||||
@@ -4408,7 +4414,7 @@ aria-label="Footer"
|
||||
Previous
|
||||
</span>
|
||||
<div class="md-ellipsis">
|
||||
4.2 Linked List
|
||||
4.2 Linked list
|
||||
</div>
|
||||
</div>
|
||||
</a>
|
||||
@@ -4420,7 +4426,7 @@ aria-label="Footer"
|
||||
<a
|
||||
href="../ram_and_cache/"
|
||||
class="md-footer__link md-footer__link--next"
|
||||
aria-label="Next: 4.4 Memory and Cache"
|
||||
aria-label="Next: 4.4 Memory and cache"
|
||||
rel="next"
|
||||
>
|
||||
<div class="md-footer__title">
|
||||
@@ -4428,7 +4434,7 @@ aria-label="Footer"
|
||||
Next
|
||||
</span>
|
||||
<div class="md-ellipsis">
|
||||
4.4 Memory and Cache
|
||||
4.4 Memory and cache
|
||||
</div>
|
||||
</div>
|
||||
<div class="md-footer__button md-icon">
|
||||
@@ -4521,7 +4527,7 @@ aria-label="Footer"
|
||||
<nav class="md-footer__inner md-grid" aria-label="Footer" >
|
||||
|
||||
|
||||
<a href="../linked_list/" class="md-footer__link md-footer__link--prev" aria-label="Previous: 4.2 Linked List">
|
||||
<a href="../linked_list/" class="md-footer__link md-footer__link--prev" aria-label="Previous: 4.2 Linked list">
|
||||
<div class="md-footer__button md-icon">
|
||||
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
|
||||
@@ -4531,20 +4537,20 @@ aria-label="Footer"
|
||||
Previous
|
||||
</span>
|
||||
<div class="md-ellipsis">
|
||||
4.2 Linked List
|
||||
4.2 Linked list
|
||||
</div>
|
||||
</div>
|
||||
</a>
|
||||
|
||||
|
||||
|
||||
<a href="../ram_and_cache/" class="md-footer__link md-footer__link--next" aria-label="Next: 4.4 Memory and Cache">
|
||||
<a href="../ram_and_cache/" class="md-footer__link md-footer__link--next" aria-label="Next: 4.4 Memory and cache">
|
||||
<div class="md-footer__title">
|
||||
<span class="md-footer__direction">
|
||||
Next
|
||||
</span>
|
||||
<div class="md-ellipsis">
|
||||
4.4 Memory and Cache
|
||||
4.4 Memory and cache
|
||||
</div>
|
||||
</div>
|
||||
<div class="md-footer__button md-icon">
|
||||
|
||||
Reference in New Issue
Block a user