Skip to content

feat: add solutions to lc problem: No.3125 #2656

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 34 additions & 25 deletions solution/0400-0499/0422.Valid Word Square/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

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

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

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

Expand All @@ -24,22 +24,25 @@

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

<pre><strong>输入:</strong>
<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>

<pre>
<strong>输入:</strong>
[
&quot;abcd&quot;,
&quot;bnrt&quot;,
&quot;crmy&quot;,
&quot;dtye&quot;
"abcd",
"bnrt",
"crmy",
"dtye"
]

<strong>输出:</strong>
true

<strong>解释:</strong>
第 1 行和第 1 列都是 &quot;abcd&quot;
第 2 行和第 2 列都是 &quot;bnrt&quot;
第 3 行和第 3 列都是 &quot;crmy&quot;
第 4 行和第 4 列都是 &quot;dtye&quot;
第 1 行和第 1 列都是 "abcd"
第 2 行和第 2 列都是 "bnrt"
第 3 行和第 3 列都是 "crmy"
第 4 行和第 4 列都是 "dtye"

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

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

<pre><strong>输入:</strong>
<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>

<pre>
<strong>输入:</strong>
[
&quot;abcd&quot;,
&quot;bnrt&quot;,
&quot;crm&quot;,
&quot;dt&quot;
"abcd",
"bnrt",
"crm",
"dt"
]

<strong>输出:</strong>
true

<strong>解释:</strong>
第 1 行和第 1 列都是 &quot;abcd&quot;
第 2 行和第 2 列都是 &quot;bnrt&quot;
第 3 行和第 3 列都是 &quot;crm&quot;
第 4 行和第 4 列都是 &quot;dt&quot;
第 1 行和第 1 列都是 "abcd"
第 2 行和第 2 列都是 "bnrt"
第 3 行和第 3 列都是 "crm"
第 4 行和第 4 列都是 "dt"

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

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

<pre><strong>输入:</strong>
<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>

<pre>
<strong>输入:</strong>
[
&quot;ball&quot;,
&quot;area&quot;,
&quot;read&quot;,
&quot;lady&quot;
"ball",
"area",
"read",
"lady"
]

<strong>输出:</strong>
false

<strong>解释:</strong>
第 3 行是 &quot;read&quot; ,然而第 3 列是 &quot;lead&quot;
第 3 行是 "read" ,然而第 3 列是 "lead"

因此,这 <strong>不是</strong> 一个有效的单词方块。
</pre>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# [3125. Maximum Number That Makes Result of Bitwise AND Zero 🔒](https://leetcode.cn/problems/maximum-number-that-makes-result-of-bitwise-and-zero)

[English Version](/solution/3100-3199/3125.Maximum%20Number%20That%20Makes%20Result%20of%20Bitwise%20AND%20Zero/README_EN.md)

<!-- tags: -->

## 题目描述

<!-- 这里写题目描述 -->

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.

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 7</span></p>

<p><strong>Output:</strong> <span class="example-io">3</span></p>

<p><strong>Explanation:</strong></p>

<p>The bitwise <code>AND</code> of <code>[6, 7]</code> is 6.<br />
The bitwise <code>AND</code> of <code>[5, 6, 7]</code> is 4.<br />
The bitwise <code>AND</code> of <code>[4, 5, 6, 7]</code> is 4.<br />
The bitwise <code>AND</code> of <code>[3, 4, 5, 6, 7]</code> is 0.</p>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 9</span></p>

<p><strong>Output:</strong> <span class="example-io">7</span></p>

<p><strong>Explanation:</strong></p>

<p>The bitwise <code>AND</code> of <code>[7, 8, 9]</code> is 0.</p>
</div>

<p><strong class="example">Example 3:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 17</span></p>

<p><strong>Output:</strong> <span class="example-io">15</span></p>

<p><strong>Explanation:</strong></p>

<p>The bitwise <code>AND</code> of <code>[15, 16, 17]</code> is 0.</p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= n &lt;= 10<sup>15</sup></code></li>
</ul>

## 解法

### 方法一:位运算

我们可以找到 $n$ 的二进制表示中最高位的 $1$,那么最大的 $x$ 一定小于 $n$ 且该位为 $0$,其他低位均为 $1$。即 $x = 2^{\text{最高位的位数} - 1} - 1$。

````python

<!-- tabs:start -->

```python
class Solution:
def maxNumber(self, n: int) -> int:
return (1 << (n.bit_length() - 1)) - 1
````

```java
class Solution {
public long maxNumber(long n) {
return (1L << (63 - Long.numberOfLeadingZeros(n))) - 1;
}
}
```

```cpp
class Solution {
public:
long long maxNumber(long long n) {
return (1LL << (63 - __builtin_clzll(n))) - 1;
}
};
```

```go
func maxNumber(n int64) int64 {
return int64(1<<(bits.Len64(uint64(n))-1)) - 1
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# [3125. Maximum Number That Makes Result of Bitwise AND Zero 🔒](https://leetcode.com/problems/maximum-number-that-makes-result-of-bitwise-and-zero)

[中文文档](/solution/3100-3199/3125.Maximum%20Number%20That%20Makes%20Result%20of%20Bitwise%20AND%20Zero/README.md)

<!-- tags: -->

## Description

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.

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 7</span></p>

<p><strong>Output:</strong> <span class="example-io">3</span></p>

<p><strong>Explanation:</strong></p>

<p>The bitwise <code>AND</code> of <code>[6, 7]</code> is 6.<br />
The bitwise <code>AND</code> of <code>[5, 6, 7]</code> is 4.<br />
The bitwise <code>AND</code> of <code>[4, 5, 6, 7]</code> is 4.<br />
The bitwise <code>AND</code> of <code>[3, 4, 5, 6, 7]</code> is 0.</p>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 9</span></p>

<p><strong>Output:</strong> <span class="example-io">7</span></p>

<p><strong>Explanation:</strong></p>

<p>The bitwise <code>AND</code> of <code>[7, 8, 9]</code> is 0.</p>
</div>

<p><strong class="example">Example 3:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 17</span></p>

<p><strong>Output:</strong> <span class="example-io">15</span></p>

<p><strong>Explanation:</strong></p>

<p>The bitwise <code>AND</code> of <code>[15, 16, 17]</code> is 0.</p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= n &lt;= 10<sup>15</sup></code></li>
</ul>

## Solutions

### Solution 1: Bit Manipulation

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.

The time complexity is $O(\log n)$, and the space complexity is $O(1)$.

<!-- tabs:start -->

```python
class Solution:
def maxNumber(self, n: int) -> int:
return (1 << (n.bit_length() - 1)) - 1
```

```java
class Solution {
public long maxNumber(long n) {
return (1L << (63 - Long.numberOfLeadingZeros(n))) - 1;
}
}
```

```cpp
class Solution {
public:
long long maxNumber(long long n) {
return (1LL << (63 - __builtin_clzll(n))) - 1;
}
};
```

```go
func maxNumber(n int64) int64 {
return int64(1<<(bits.Len64(uint64(n))-1)) - 1
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Solution {
public:
long long maxNumber(long long n) {
return (1LL << (63 - __builtin_clzll(n))) - 1;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
func maxNumber(n int64) int64 {
return int64(1<<(bits.Len64(uint64(n))-1)) - 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Solution {
public long maxNumber(long n) {
return (1L << (63 - Long.numberOfLeadingZeros(n))) - 1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Solution:
def maxNumber(self, n: int) -> int:
return (1 << (n.bit_length() - 1)) - 1
1 change: 1 addition & 0 deletions solution/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3135,6 +3135,7 @@
| 3122 | [使矩阵满足条件的最少操作次数](/solution/3100-3199/3122.Minimum%20Number%20of%20Operations%20to%20Satisfy%20Conditions/README.md) | `数组`,`动态规划`,`矩阵` | 中等 | 第 394 场周赛 |
| 3123 | [最短路径中的边](/solution/3100-3199/3123.Find%20Edges%20in%20Shortest%20Paths/README.md) | `深度优先搜索`,`广度优先搜索`,`图`,`最短路`,`堆(优先队列)` | 困难 | 第 394 场周赛 |
| 3124 | [查找最长的电话](/solution/3100-3199/3124.Find%20Longest%20Calls/README.md) | `数据库` | 中等 | 🔒 |
| 3125 | [Maximum Number That Makes Result of Bitwise AND Zero](/solution/3100-3199/3125.Maximum%20Number%20That%20Makes%20Result%20of%20Bitwise%20AND%20Zero/README.md) | | 中等 | 🔒 |

## 版权

Expand Down
1 change: 1 addition & 0 deletions solution/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -3133,6 +3133,7 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> on
| 3122 | [Minimum Number of Operations to Satisfy Conditions](/solution/3100-3199/3122.Minimum%20Number%20of%20Operations%20to%20Satisfy%20Conditions/README_EN.md) | `Array`,`Dynamic Programming`,`Matrix` | Medium | Weekly Contest 394 |
| 3123 | [Find Edges in Shortest Paths](/solution/3100-3199/3123.Find%20Edges%20in%20Shortest%20Paths/README_EN.md) | `Depth-First Search`,`Breadth-First Search`,`Graph`,`Shortest Path`,`Heap (Priority Queue)` | Hard | Weekly Contest 394 |
| 3124 | [Find Longest Calls](/solution/3100-3199/3124.Find%20Longest%20Calls/README_EN.md) | `Database` | Medium | 🔒 |
| 3125 | [Maximum Number That Makes Result of Bitwise AND Zero](/solution/3100-3199/3125.Maximum%20Number%20That%20Makes%20Result%20of%20Bitwise%20AND%20Zero/README_EN.md) | | Medium | 🔒 |

## Copyright

Expand Down