From 5e779298d105bbe7d5a32d8b6e0d0d1b69b8e08b Mon Sep 17 00:00:00 2001 From: Doocs Bot Date: Fri, 8 Aug 2025 20:06:02 +0800 Subject: [PATCH 1/2] chore: auto update starcharts --- images/starcharts.svg | 64520 ++++++++++++++++++++-------------------- 1 file changed, 32312 insertions(+), 32208 deletions(-) diff --git a/images/starcharts.svg b/images/starcharts.svg index 57804d6c601d9..fa40a02040215 100644 --- a/images/starcharts.svg +++ b/images/starcharts.svg @@ -1,4 +1,4 @@ - + \n2018-09-252019-08-022020-06-092021-04-172022-02-222022-12-312023-11-082024-09-142025-07-23Time2019-08-042020-06-132021-04-232022-03-022023-01-102023-11-202024-09-282025-08-08Time04400 \ No newline at end of file +L 949 15 +L 949 15 +L 949 15 +L 950 15 +L 950 15 +L 950 15 +L 950 15 +L 950 15 +L 950 15 +L 950 15 +L 950 15 +L 950 15 +L 950 15 +L 950 15 +L 950 15 +L 950 15 +L 950 15 +L 950 15 +L 950 15 +L 950 15 +L 950 15 +L 950 15 +L 950 15 +L 950 15 +L 950 15 +L 950 15 +L 950 15 +L 950 15" style="stroke-width:2;stroke:rgba(129,199,239,1.0);fill:none"/> \ No newline at end of file From 28073f850a079faee5b350dd06042f74fff812e5 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sat, 9 Aug 2025 07:53:34 +0800 Subject: [PATCH 2/2] feat: add solutions to lc problem: No.0231 (#4631) No.0231.Power of Two --- .../0200-0299/0231.Power of Two/README.md | 34 ++++++++++++++---- .../0200-0299/0231.Power of Two/README_EN.md | 36 ++++++++++++++++--- .../0200-0299/0231.Power of Two/Solution.rs | 5 +++ .../0200-0299/0231.Power of Two/Solution2.js | 2 +- .../0200-0299/0231.Power of Two/Solution2.rs | 5 +++ .../0200-0299/0231.Power of Two/Solution2.ts | 2 +- 6 files changed, 72 insertions(+), 12 deletions(-) create mode 100644 solution/0200-0299/0231.Power of Two/Solution.rs create mode 100644 solution/0200-0299/0231.Power of Two/Solution2.rs diff --git a/solution/0200-0299/0231.Power of Two/README.md b/solution/0200-0299/0231.Power of Two/README.md index 123794f747746..58c8aebfb77a2 100644 --- a/solution/0200-0299/0231.Power of Two/README.md +++ b/solution/0200-0299/0231.Power of Two/README.md @@ -67,7 +67,9 @@ tags: ### 方法一:位运算 -$\texttt{n\&(n-1)}$ 可将最后一个二进制形式的 $n$ 的最后一位 $1$ 移除,若移除后为 $0$,说明 $n$ 是 $2$ 的幂。 +根据位运算的性质,执行 $\texttt{n\&(n-1)}$ 可以消去二进制形式的 $n$ 的最后一位 $1$。因此,如果 $n \gt 0$,并且满足 $\texttt{n\&(n-1)}$ 结果为 $0$,则说明 $n$ 是 $2$ 的幂。 + +时间复杂度 $O(1)$,空间复杂度 $O(1)$。 @@ -116,6 +118,16 @@ function isPowerOfTwo(n: number): boolean { } ``` +#### Rust + +```rust +impl Solution { + pub fn is_power_of_two(n: i32) -> bool { + n > 0 && (n & (n - 1)) == 0 + } +} +``` + #### JavaScript ```js @@ -134,11 +146,11 @@ var isPowerOfTwo = function (n) { -### 方法二:lowbit +### 方法二:Lowbit -$\texttt{n\&(-n)}$ 可以得到 $n$ 的最后一位 $1$ 表示的十进制数,若与 $n$ 相等,说明 $n$ 是 $2$ 的幂。 +根据 $\text{lowbit}$ 的定义,我们知道 $\text{lowbit}(x) = x \& (-x)$,可以得到 $n$ 的最后一位 $1$ 表示的十进制数。因此,如果 $n > 0$,并且满足 $\text{lowbit}(n)$ 等于 $n$,则说明 $n$ 是 $2$ 的幂。 -注意:要满足 $n$ 是 $2$ 的幂次方,需要保证 $n$ 大于 $0$。 +时间复杂度 $O(1)$,空间复杂度 $O(1)$。 @@ -183,7 +195,17 @@ func isPowerOfTwo(n int) bool { ```ts function isPowerOfTwo(n: number): boolean { - return n > 0 && (n & (n - 1)) === 0; + return n > 0 && n === (n & -n); +} +``` + +#### Rust + +```rust +impl Solution { + pub fn is_power_of_two(n: i32) -> bool { + n > 0 && n == (n & (-n)) + } } ``` @@ -195,7 +217,7 @@ function isPowerOfTwo(n: number): boolean { * @return {boolean} */ var isPowerOfTwo = function (n) { - return n > 0 && n == (n & -n); + return n > 0 && n === (n & -n); }; ``` diff --git a/solution/0200-0299/0231.Power of Two/README_EN.md b/solution/0200-0299/0231.Power of Two/README_EN.md index 53b22983ca354..30345d3519512 100644 --- a/solution/0200-0299/0231.Power of Two/README_EN.md +++ b/solution/0200-0299/0231.Power of Two/README_EN.md @@ -62,7 +62,11 @@ tags: -### Solution 1 +### Solution 1: Bit Manipulation + +According to the properties of bit manipulation, executing $\texttt{n\&(n-1)}$ can eliminate the last bit $1$ in the binary form of $n$. Therefore, if $n > 0$ and $\texttt{n\&(n-1)}$ results in $0$, then $n$ is a power of $2$. + +The time complexity is $O(1)$, and the space complexity is $O(1)$. @@ -111,6 +115,16 @@ function isPowerOfTwo(n: number): boolean { } ``` +#### Rust + +```rust +impl Solution { + pub fn is_power_of_two(n: i32) -> bool { + n > 0 && (n & (n - 1)) == 0 + } +} +``` + #### JavaScript ```js @@ -129,7 +143,11 @@ var isPowerOfTwo = function (n) { -### Solution 2 +### Solution 2: Lowbit + +According to the definition of $\text{lowbit}$, we know that $\text{lowbit}(x) = x \& (-x)$, which can get the decimal number represented by the last bit $1$ of $n$. Therefore, if $n > 0$ and $\text{lowbit}(n)$ equals $n$, then $n$ is a power of $2$. + +The time complexity is $O(1)$, and the space complexity is $O(1)$. @@ -174,7 +192,17 @@ func isPowerOfTwo(n int) bool { ```ts function isPowerOfTwo(n: number): boolean { - return n > 0 && (n & (n - 1)) === 0; + return n > 0 && n === (n & -n); +} +``` + +#### Rust + +```rust +impl Solution { + pub fn is_power_of_two(n: i32) -> bool { + n > 0 && n == (n & (-n)) + } } ``` @@ -186,7 +214,7 @@ function isPowerOfTwo(n: number): boolean { * @return {boolean} */ var isPowerOfTwo = function (n) { - return n > 0 && n == (n & -n); + return n > 0 && n === (n & -n); }; ``` diff --git a/solution/0200-0299/0231.Power of Two/Solution.rs b/solution/0200-0299/0231.Power of Two/Solution.rs new file mode 100644 index 0000000000000..d07cc7d8a7e7a --- /dev/null +++ b/solution/0200-0299/0231.Power of Two/Solution.rs @@ -0,0 +1,5 @@ +impl Solution { + pub fn is_power_of_two(n: i32) -> bool { + n > 0 && (n & (n - 1)) == 0 + } +} diff --git a/solution/0200-0299/0231.Power of Two/Solution2.js b/solution/0200-0299/0231.Power of Two/Solution2.js index d0b7a66d9343c..1576a49ffb8e6 100644 --- a/solution/0200-0299/0231.Power of Two/Solution2.js +++ b/solution/0200-0299/0231.Power of Two/Solution2.js @@ -3,5 +3,5 @@ * @return {boolean} */ var isPowerOfTwo = function (n) { - return n > 0 && n == (n & -n); + return n > 0 && n === (n & -n); }; diff --git a/solution/0200-0299/0231.Power of Two/Solution2.rs b/solution/0200-0299/0231.Power of Two/Solution2.rs new file mode 100644 index 0000000000000..40010a60b63ba --- /dev/null +++ b/solution/0200-0299/0231.Power of Two/Solution2.rs @@ -0,0 +1,5 @@ +impl Solution { + pub fn is_power_of_two(n: i32) -> bool { + n > 0 && n == (n & (-n)) + } +} diff --git a/solution/0200-0299/0231.Power of Two/Solution2.ts b/solution/0200-0299/0231.Power of Two/Solution2.ts index bfb580cec5184..4bdd7d6c71707 100644 --- a/solution/0200-0299/0231.Power of Two/Solution2.ts +++ b/solution/0200-0299/0231.Power of Two/Solution2.ts @@ -1,3 +1,3 @@ function isPowerOfTwo(n: number): boolean { - return n > 0 && (n & (n - 1)) === 0; + return n > 0 && n === (n & -n); }