Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0fafa6e

Browse files
feat: add solutions to lc problem: No.3125 (doocs#2656)
1 parent 951919d commit 0fafa6e

File tree

12 files changed

+253
-25
lines changed

12 files changed

+253
-25
lines changed

‎solution/0400-0499/0422.Valid Word Square/README.md‎

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<p>给你一个单词序列,判断其是否形成了一个有效的单词方块。</p>
1212

13-
<p>有效的单词方块是指此由单词序列组成的文字方块的&nbsp;第 k 行 和&nbsp;第 k 列 (0 &le; <em>k</em> &lt; max(行数, 列数)) 所显示的字符串完全相同。</p>
13+
<p>有效的单词方块是指此由单词序列组成的文字方块的&nbsp;第 k 行 和&nbsp;第 k 列 (0 <em>k</em> &lt; max(行数, 列数)) 所显示的字符串完全相同。</p>
1414

1515
<p><strong>注意:</strong></p>
1616

@@ -24,22 +24,25 @@
2424

2525
<p><strong>示例 1:</strong></p>
2626

27-
<pre><strong>输入:</strong>
27+
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0400-0499/0422.Valid%20Word%20Square/images/1713840723-rPoAKZ-image.png" style="width: 333px; height: 333px;" /></strong></p>
28+
29+
<pre>
30+
<strong>输入:</strong>
2831
[
29-
&quot;abcd&quot;,
30-
&quot;bnrt&quot;,
31-
&quot;crmy&quot;,
32-
&quot;dtye&quot;
32+
"abcd",
33+
"bnrt",
34+
"crmy",
35+
"dtye"
3336
]
3437

3538
<strong>输出:</strong>
3639
true
3740

3841
<strong>解释:</strong>
39-
第 1 行和第 1 列都是 &quot;abcd&quot;
40-
第 2 行和第 2 列都是 &quot;bnrt&quot;
41-
第 3 行和第 3 列都是 &quot;crmy&quot;
42-
第 4 行和第 4 列都是 &quot;dtye&quot;
42+
第 1 行和第 1 列都是 "abcd"
43+
第 2 行和第 2 列都是 "bnrt"
44+
第 3 行和第 3 列都是 "crmy"
45+
第 4 行和第 4 列都是 "dtye"
4346

4447
因此,这是一个有效的单词方块。
4548
</pre>
@@ -48,22 +51,25 @@ true
4851

4952
<p><strong>示例 2:</strong></p>
5053

51-
<pre><strong>输入:</strong>
54+
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0400-0499/0422.Valid%20Word%20Square/images/1713840732-EvBDzU-image.png" style="width: 333px; height: 333px;" /></strong></p>
55+
56+
<pre>
57+
<strong>输入:</strong>
5258
[
53-
&quot;abcd&quot;,
54-
&quot;bnrt&quot;,
55-
&quot;crm&quot;,
56-
&quot;dt&quot;
59+
"abcd",
60+
"bnrt",
61+
"crm",
62+
"dt"
5763
]
5864

5965
<strong>输出:</strong>
6066
true
6167

6268
<strong>解释:</strong>
63-
第 1 行和第 1 列都是 &quot;abcd&quot;
64-
第 2 行和第 2 列都是 &quot;bnrt&quot;
65-
第 3 行和第 3 列都是 &quot;crm&quot;
66-
第 4 行和第 4 列都是 &quot;dt&quot;
69+
第 1 行和第 1 列都是 "abcd"
70+
第 2 行和第 2 列都是 "bnrt"
71+
第 3 行和第 3 列都是 "crm"
72+
第 4 行和第 4 列都是 "dt"
6773

6874
因此,这是一个有效的单词方块。
6975
</pre>
@@ -72,19 +78,22 @@ true
7278

7379
<p><strong>示例 3:</strong></p>
7480

75-
<pre><strong>输入:</strong>
81+
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0400-0499/0422.Valid%20Word%20Square/images/1713840738-tdOYTB-image.png" style="width: 333px; height: 333px;" /></strong></p>
82+
83+
<pre>
84+
<strong>输入:</strong>
7685
[
77-
&quot;ball&quot;,
78-
&quot;area&quot;,
79-
&quot;read&quot;,
80-
&quot;lady&quot;
86+
"ball",
87+
"area",
88+
"read",
89+
"lady"
8190
]
8291

8392
<strong>输出:</strong>
8493
false
8594

8695
<strong>解释:</strong>
87-
第 3 行是 &quot;read&quot; ,然而第 3 列是 &quot;lead&quot;
96+
第 3 行是 "read" ,然而第 3 列是 "lead"
8897

8998
因此,这 <strong>不是</strong> 一个有效的单词方块。
9099
</pre>
15.1 KB
Loading[フレーム]
12.9 KB
Loading[フレーム]
13.4 KB
Loading[フレーム]
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# [3125. Maximum Number That Makes Result of Bitwise AND Zero 🔒](https://leetcode.cn/problems/maximum-number-that-makes-result-of-bitwise-and-zero)
2+
3+
[English Version](/solution/3100-3199/3125.Maximum%20Number%20That%20Makes%20Result%20of%20Bitwise%20AND%20Zero/README_EN.md)
4+
5+
<!-- tags: -->
6+
7+
## 题目描述
8+
9+
<!-- 这里写题目描述 -->
10+
11+
Given an integer <code>n</code>, return the <strong>maximum</strong> integer <code>x</code> such that <code>x &lt;= n</code>, and the bitwise <code>AND</code> of all the numbers in the range <code>[x, n]</code> is 0.
12+
13+
<p>&nbsp;</p>
14+
<p><strong class="example">Example 1:</strong></p>
15+
16+
<div class="example-block">
17+
<p><strong>Input:</strong> <span class="example-io">n = 7</span></p>
18+
19+
<p><strong>Output:</strong> <span class="example-io">3</span></p>
20+
21+
<p><strong>Explanation:</strong></p>
22+
23+
<p>The bitwise <code>AND</code> of <code>[6, 7]</code> is 6.<br />
24+
The bitwise <code>AND</code> of <code>[5, 6, 7]</code> is 4.<br />
25+
The bitwise <code>AND</code> of <code>[4, 5, 6, 7]</code> is 4.<br />
26+
The bitwise <code>AND</code> of <code>[3, 4, 5, 6, 7]</code> is 0.</p>
27+
</div>
28+
29+
<p><strong class="example">Example 2:</strong></p>
30+
31+
<div class="example-block">
32+
<p><strong>Input:</strong> <span class="example-io">n = 9</span></p>
33+
34+
<p><strong>Output:</strong> <span class="example-io">7</span></p>
35+
36+
<p><strong>Explanation:</strong></p>
37+
38+
<p>The bitwise <code>AND</code> of <code>[7, 8, 9]</code> is 0.</p>
39+
</div>
40+
41+
<p><strong class="example">Example 3:</strong></p>
42+
43+
<div class="example-block">
44+
<p><strong>Input:</strong> <span class="example-io">n = 17</span></p>
45+
46+
<p><strong>Output:</strong> <span class="example-io">15</span></p>
47+
48+
<p><strong>Explanation:</strong></p>
49+
50+
<p>The bitwise <code>AND</code> of <code>[15, 16, 17]</code> is 0.</p>
51+
</div>
52+
53+
<p>&nbsp;</p>
54+
<p><strong>Constraints:</strong></p>
55+
56+
<ul>
57+
<li><code>1 &lt;= n &lt;= 10<sup>15</sup></code></li>
58+
</ul>
59+
60+
## 解法
61+
62+
### 方法一:位运算
63+
64+
我们可以找到 $n$ 的二进制表示中最高位的 1ドル,ドル那么最大的 $x$ 一定小于 $n$ 且该位为 0ドル,ドル其他低位均为 1ドル$。即 $x = 2^{\text{最高位的位数} - 1} - 1$。
65+
66+
````python
67+
68+
<!-- tabs:start -->
69+
70+
```python
71+
class Solution:
72+
def maxNumber(self, n: int) -> int:
73+
return (1 << (n.bit_length() - 1)) - 1
74+
````
75+
76+
```java
77+
class Solution {
78+
public long maxNumber(long n) {
79+
return (1L << (63 - Long.numberOfLeadingZeros(n))) - 1;
80+
}
81+
}
82+
```
83+
84+
```cpp
85+
class Solution {
86+
public:
87+
long long maxNumber(long long n) {
88+
return (1LL << (63 - __builtin_clzll(n))) - 1;
89+
}
90+
};
91+
```
92+
93+
```go
94+
func maxNumber(n int64) int64 {
95+
return int64(1<<(bits.Len64(uint64(n))-1)) - 1
96+
}
97+
```
98+
99+
<!-- tabs:end -->
100+
101+
<!-- end -->
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# [3125. Maximum Number That Makes Result of Bitwise AND Zero 🔒](https://leetcode.com/problems/maximum-number-that-makes-result-of-bitwise-and-zero)
2+
3+
[中文文档](/solution/3100-3199/3125.Maximum%20Number%20That%20Makes%20Result%20of%20Bitwise%20AND%20Zero/README.md)
4+
5+
<!-- tags: -->
6+
7+
## Description
8+
9+
Given an integer <code>n</code>, return the <strong>maximum</strong> integer <code>x</code> such that <code>x &lt;= n</code>, and the bitwise <code>AND</code> of all the numbers in the range <code>[x, n]</code> is 0.
10+
11+
<p>&nbsp;</p>
12+
<p><strong class="example">Example 1:</strong></p>
13+
14+
<div class="example-block">
15+
<p><strong>Input:</strong> <span class="example-io">n = 7</span></p>
16+
17+
<p><strong>Output:</strong> <span class="example-io">3</span></p>
18+
19+
<p><strong>Explanation:</strong></p>
20+
21+
<p>The bitwise <code>AND</code> of <code>[6, 7]</code> is 6.<br />
22+
The bitwise <code>AND</code> of <code>[5, 6, 7]</code> is 4.<br />
23+
The bitwise <code>AND</code> of <code>[4, 5, 6, 7]</code> is 4.<br />
24+
The bitwise <code>AND</code> of <code>[3, 4, 5, 6, 7]</code> is 0.</p>
25+
</div>
26+
27+
<p><strong class="example">Example 2:</strong></p>
28+
29+
<div class="example-block">
30+
<p><strong>Input:</strong> <span class="example-io">n = 9</span></p>
31+
32+
<p><strong>Output:</strong> <span class="example-io">7</span></p>
33+
34+
<p><strong>Explanation:</strong></p>
35+
36+
<p>The bitwise <code>AND</code> of <code>[7, 8, 9]</code> is 0.</p>
37+
</div>
38+
39+
<p><strong class="example">Example 3:</strong></p>
40+
41+
<div class="example-block">
42+
<p><strong>Input:</strong> <span class="example-io">n = 17</span></p>
43+
44+
<p><strong>Output:</strong> <span class="example-io">15</span></p>
45+
46+
<p><strong>Explanation:</strong></p>
47+
48+
<p>The bitwise <code>AND</code> of <code>[15, 16, 17]</code> is 0.</p>
49+
</div>
50+
51+
<p>&nbsp;</p>
52+
<p><strong>Constraints:</strong></p>
53+
54+
<ul>
55+
<li><code>1 &lt;= n &lt;= 10<sup>15</sup></code></li>
56+
</ul>
57+
58+
## Solutions
59+
60+
### Solution 1: Bit Manipulation
61+
62+
We can find the highest bit of 1ドル$ in the binary representation of $n$. The maximum $x$ must be less than $n$ and this bit is 0ドル,ドル and all other lower bits are 1ドル,ドル i.e., $x = 2^{\text{number of the highest bit}} - 1$. This is because $x \text{ and } (x + 1) = 0$ must hold.
63+
64+
The time complexity is $O(\log n),ドル and the space complexity is $O(1)$.
65+
66+
<!-- tabs:start -->
67+
68+
```python
69+
class Solution:
70+
def maxNumber(self, n: int) -> int:
71+
return (1 << (n.bit_length() - 1)) - 1
72+
```
73+
74+
```java
75+
class Solution {
76+
public long maxNumber(long n) {
77+
return (1L << (63 - Long.numberOfLeadingZeros(n))) - 1;
78+
}
79+
}
80+
```
81+
82+
```cpp
83+
class Solution {
84+
public:
85+
long long maxNumber(long long n) {
86+
return (1LL << (63 - __builtin_clzll(n))) - 1;
87+
}
88+
};
89+
```
90+
91+
```go
92+
func maxNumber(n int64) int64 {
93+
return int64(1<<(bits.Len64(uint64(n))-1)) - 1
94+
}
95+
```
96+
97+
<!-- tabs:end -->
98+
99+
<!-- end -->
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution {
2+
public:
3+
long long maxNumber(long long n) {
4+
return (1LL << (63 - __builtin_clzll(n))) - 1;
5+
}
6+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
func maxNumber(n int64) int64 {
2+
return int64(1<<(bits.Len64(uint64(n))-1)) - 1
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution {
2+
public long maxNumber(long n) {
3+
return (1L << (63 - Long.numberOfLeadingZeros(n))) - 1;
4+
}
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def maxNumber(self, n: int) -> int:
3+
return (1 << (n.bit_length() - 1)) - 1

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /