diff --git a/.github/workflows/pretter-lint.yml b/.github/workflows/pretter-lint.yml index d10eb6e5a21d0..e4d2fb03c592c 100644 --- a/.github/workflows/pretter-lint.yml +++ b/.github/workflows/pretter-lint.yml @@ -1,8 +1,8 @@ name: prettier-linter on: - push: {} - pull_request: {} + push: {} + pull_request: {} jobs: prettier: diff --git a/lcci/10.11.Peaks and Valleys/README_EN.md b/lcci/10.11.Peaks and Valleys/README_EN.md index 364c11b3a853a..f02231607335e 100644 --- a/lcci/10.11.Peaks and Valleys/README_EN.md +++ b/lcci/10.11.Peaks and Valleys/README_EN.md @@ -20,6 +20,12 @@ ## Solutions +**Solution 1: Sorting** + +We first sort the array, and then traverse the array and swap the elements at even indices with their next element. + +The time complexity is $O(n \times \log n),ドル and the space complexity is $O(\log n)$. Here, $n$ is the length of the array. + ### **Python3** diff --git a/lcci/16.01.Swap Numbers/README.md b/lcci/16.01.Swap Numbers/README.md index 0af9830be41ea..0dd5cc45598d5 100644 --- a/lcci/16.01.Swap Numbers/README.md +++ b/lcci/16.01.Swap Numbers/README.md @@ -19,7 +19,25 @@ -异或运算。 +**方法一:位运算** + +我们可以使用异或运算 $\oplus$ 来实现两个数的交换。 + +异或运算有以下三个性质。 + +- 任何数和 0ドル$ 做异或运算,结果仍然是原来的数,即 $a \oplus 0=a$。 +- 任何数和其自身做异或运算,结果是 0ドル,ドル即 $a \oplus a=0$。 +- 异或运算满足交换律和结合律,即 $a \oplus b \oplus a=b \oplus a \oplus a=b \oplus (a \oplus a)=b \oplus 0=b$。 + +因此,我们可以对 $numbers$ 中的两个数 $a$ 和 $b$ 进行如下操作: + +- $a=a \oplus b,ドル此时 $a$ 中存储了两个数的异或结果; +- $b=a \oplus b,ドル此时 $b$ 中存储了原来 $a$ 的值; +- $a=a \oplus b,ドル此时 $a$ 中存储了原来 $b$ 的值; + +这样,我们就可以实现在不使用临时变量的情况下对两个数进行交换。 + +时间复杂度 $O(1),ドル空间复杂度 $O(1)$。 diff --git a/lcci/16.01.Swap Numbers/README_EN.md b/lcci/16.01.Swap Numbers/README_EN.md index b099c0dbec463..bf8df6356de4b 100644 --- a/lcci/16.01.Swap Numbers/README_EN.md +++ b/lcci/16.01.Swap Numbers/README_EN.md @@ -24,6 +24,26 @@ ## Solutions +**Solution 1: Bitwise Operation** + +We can use the XOR operation $\oplus$ to implement the swap of two numbers. + +The XOR operation has the following three properties: + +- Any number XORed with 0ドル$ remains unchanged, i.e., $a \oplus 0=a$. +- Any number XORed with itself results in 0ドル,ドル i.e., $a \oplus a=0$. +- The XOR operation satisfies the commutative and associative laws, i.e., $a \oplus b \oplus a=b \oplus a \oplus a=b \oplus (a \oplus a)=b \oplus 0=b$. + +Therefore, we can perform the following operations on two numbers $a$ and $b$ in the array $numbers$: + +- $a=a \oplus b,ドル now $a$ stores the XOR result of the two numbers; +- $b=a \oplus b,ドル now $b$ stores the original value of $a$; +- $a=a \oplus b,ドル now $a$ stores the original value of $b$; + +In this way, we can swap two numbers without using a temporary variable. + +The time complexity is $O(1),ドル and the space complexity is $O(1)$. + ### **Python3** diff --git a/lcci/16.02.Words Frequency/README.md b/lcci/16.02.Words Frequency/README.md index 8846f6f4910c7..b4113b6e2277e 100644 --- a/lcci/16.02.Words Frequency/README.md +++ b/lcci/16.02.Words Frequency/README.md @@ -5,6 +5,7 @@ ## 题目描述 +

设计一个方法,找出任意指定单词在一本书中的出现频率。

你的实现应该支持如下操作: