Skip to content

Commit 71f8fdc

Browse files
authored
feat: add new lc problems (#2171)
1 parent f10e4c9 commit 71f8fdc

File tree

34 files changed

+20007
-17941
lines changed

34 files changed

+20007
-17941
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# [2980. 检查按位或是否存在尾随零](https://leetcode.cn/problems/check-if-bitwise-or-has-trailing-zeros)
2+
3+
[English Version](/solution/2900-2999/2980.Check%20if%20Bitwise%20OR%20Has%20Trailing%20Zeros/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个<strong> 正整数 </strong>数组 <code>nums</code> 。</p>
10+
11+
<p>你需要检查是否可以从数组中选出 <strong>两个或更多 </strong>元素,满足这些元素的按位或运算( <code>OR</code>)结果的二进制表示中 <strong>至少</strong><strong> </strong>存在一个尾随零。</p>
12+
13+
<p>例如,数字 <code>5</code> 的二进制表示是 <code>"101"</code>,不存在尾随零,而数字 <code>4</code> 的二进制表示是 <code>"100"</code>,存在两个尾随零。</p>
14+
15+
<p>如果可以选择两个或更多元素,其按位或运算结果存在尾随零,返回 <code>true</code>;否则,返回<em> </em><code>false</code> 。</p>
16+
17+
<p>&nbsp;</p>
18+
19+
<p><strong class="example">示例 1:</strong></p>
20+
21+
<pre>
22+
<strong>输入:</strong>nums = [1,2,3,4,5]
23+
<strong>输出:</strong>true
24+
<strong>解释:</strong>如果选择元素 2 和 4,按位或运算结果是 6,二进制表示为 "110" ,存在一个尾随零。
25+
</pre>
26+
27+
<p><strong class="example">示例 2:</strong></p>
28+
29+
<pre>
30+
<strong>输入:</strong>nums = [2,4,8,16]
31+
<strong>输出:</strong>true
32+
<strong>解释:</strong>如果选择元素 2 和 4,按位或运算结果是 6,二进制表示为 "110",存在一个尾随零。
33+
其他按位或运算结果存在尾随零的可能选择方案包括:(2, 8), (2, 16), (4, 8), (4, 16), (8, 16), (2, 4, 8), (2, 4, 16), (2, 8, 16), (4, 8, 16), 以及 (2, 4, 8, 16) 。
34+
</pre>
35+
36+
<p><strong class="example">示例 3:</strong></p>
37+
38+
<pre>
39+
<strong>输入:</strong>nums = [1,3,5,7,9]
40+
<strong>输出:</strong>false
41+
<strong>解释:</strong>不存在按位或运算结果存在尾随零的选择方案。
42+
</pre>
43+
44+
<p>&nbsp;</p>
45+
46+
<p><strong>提示:</strong></p>
47+
48+
<ul>
49+
<li><code>2 &lt;= nums.length &lt;= 100</code></li>
50+
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
51+
</ul>
52+
53+
## 解法
54+
55+
<!-- 这里可写通用的实现逻辑 -->
56+
57+
<!-- tabs:start -->
58+
59+
### **Python3**
60+
61+
<!-- 这里可写当前语言的特殊实现逻辑 -->
62+
63+
```python
64+
65+
```
66+
67+
### **Java**
68+
69+
<!-- 这里可写当前语言的特殊实现逻辑 -->
70+
71+
```java
72+
73+
```
74+
75+
### **C++**
76+
77+
```cpp
78+
79+
```
80+
81+
### **Go**
82+
83+
```go
84+
85+
```
86+
87+
### **...**
88+
89+
```
90+
91+
```
92+
93+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# [2980. Check if Bitwise OR Has Trailing Zeros](https://leetcode.com/problems/check-if-bitwise-or-has-trailing-zeros)
2+
3+
[中文文档](/solution/2900-2999/2980.Check%20if%20Bitwise%20OR%20Has%20Trailing%20Zeros/README.md)
4+
5+
## Description
6+
7+
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p>
8+
9+
<p>You have to check if it is possible to select <strong>two or more</strong> elements in the array such that the bitwise <code>OR</code> of the selected elements has <strong>at least </strong>one trailing zero in its binary representation.</p>
10+
11+
<p>For example, the binary representation of <code>5</code>, which is <code>&quot;101&quot;</code>, does not have any trailing zeros, whereas the binary representation of <code>4</code>, which is <code>&quot;100&quot;</code>, has two trailing zeros.</p>
12+
13+
<p>Return <code>true</code> <em>if it is possible to select two or more elements whose bitwise</em> <code>OR</code> <em>has trailing zeros, return</em> <code>false</code> <em>otherwise</em>.</p>
14+
15+
<p>&nbsp;</p>
16+
<p><strong class="example">Example 1:</strong></p>
17+
18+
<pre>
19+
<strong>Input:</strong> nums = [1,2,3,4,5]
20+
<strong>Output:</strong> true
21+
<strong>Explanation:</strong> If we select the elements 2 and 4, their bitwise OR is 6, which has the binary representation &quot;110&quot; with one trailing zero.
22+
</pre>
23+
24+
<p><strong class="example">Example 2:</strong></p>
25+
26+
<pre>
27+
<strong>Input:</strong> nums = [2,4,8,16]
28+
<strong>Output:</strong> true
29+
<strong>Explanation: </strong>If we select the elements 2 and 4, their bitwise OR is 6, which has the binary representation &quot;110&quot; with one trailing zero.
30+
Other possible ways to select elements to have trailing zeroes in the binary representation of their bitwise OR are: (2, 8), (2, 16), (4, 8), (4, 16), (8, 16), (2, 4, 8), (2, 4, 16), (2, 8, 16), (4, 8, 16), and (2, 4, 8, 16).
31+
</pre>
32+
33+
<p><strong class="example">Example 3:</strong></p>
34+
35+
<pre>
36+
<strong>Input:</strong> nums = [1,3,5,7,9]
37+
<strong>Output:</strong> false
38+
<strong>Explanation:</strong> There is no possible way to select two or more elements to have trailing zeros in the binary representation of their bitwise OR.
39+
</pre>
40+
41+
<p>&nbsp;</p>
42+
<p><strong>Constraints:</strong></p>
43+
44+
<ul>
45+
<li><code>2 &lt;= nums.length &lt;= 100</code></li>
46+
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
47+
</ul>
48+
49+
## Solutions
50+
51+
<!-- tabs:start -->
52+
53+
### **Python3**
54+
55+
```python
56+
57+
```
58+
59+
### **Java**
60+
61+
```java
62+
63+
```
64+
65+
### **C++**
66+
67+
```cpp
68+
69+
```
70+
71+
### **Go**
72+
73+
```go
74+
75+
```
76+
77+
### **...**
78+
79+
```
80+
81+
```
82+
83+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# [2981. 找出出现至少三次的最长特殊子字符串 I](https://leetcode.cn/problems/find-longest-special-substring-that-occurs-thrice-i)
2+
3+
[English Version](/solution/2900-2999/2981.Find%20Longest%20Special%20Substring%20That%20Occurs%20Thrice%20I/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个仅由小写英文字母组成的字符串 <code>s</code> 。</p>
10+
11+
<p>如果一个字符串仅由单一字符组成,那么它被称为 <strong>特殊 </strong>字符串。例如,字符串 <code>"abc"</code> 不是特殊字符串,而字符串 <code>"ddd"</code>、<code>"zz"</code> 和 <code>"f"</code> 是特殊字符串。</p>
12+
13+
<p>返回在 <code>s</code> 中出现 <strong>至少三次 </strong>的<strong> 最长特殊子字符串 </strong>的长度,如果不存在出现至少三次的特殊子字符串,则返回 <code>-1</code> 。</p>
14+
15+
<p><strong>子字符串 </strong>是字符串中的一个连续<strong> 非空 </strong>字符序列。</p>
16+
17+
<p>&nbsp;</p>
18+
19+
<p><strong class="example">示例 1:</strong></p>
20+
21+
<pre>
22+
<strong>输入:</strong>s = "aaaa"
23+
<strong>输出:</strong>2
24+
<strong>解释:</strong>出现三次的最长特殊子字符串是 "aa" :子字符串 "<em><strong>aa</strong></em>aa"、"a<em><strong>aa</strong></em>a" 和 "aa<em><strong>aa</strong></em>"。
25+
可以证明最大长度是 2 。
26+
</pre>
27+
28+
<p><strong class="example">示例 2:</strong></p>
29+
30+
<pre>
31+
<strong>输入:</strong>s = "abcdef"
32+
<strong>输出:</strong>-1
33+
<strong>解释:</strong>不存在出现至少三次的特殊子字符串。因此返回 -1 。
34+
</pre>
35+
36+
<p><strong class="example">示例 3:</strong></p>
37+
38+
<pre>
39+
<strong>输入:</strong>s = "abcaba"
40+
<strong>输出:</strong>1
41+
<strong>解释:</strong>出现三次的最长特殊子字符串是 "a" :子字符串 "<em><strong>a</strong></em>bcaba"、"abc<em><strong>a</strong></em>ba" 和 "abcab<em><strong>a</strong></em>"。
42+
可以证明最大长度是 1 。
43+
</pre>
44+
45+
<p>&nbsp;</p>
46+
47+
<p><strong>提示:</strong></p>
48+
49+
<ul>
50+
<li><code>3 &lt;= s.length &lt;= 50</code></li>
51+
<li><code>s</code> 仅由小写英文字母组成。</li>
52+
</ul>
53+
54+
## 解法
55+
56+
<!-- 这里可写通用的实现逻辑 -->
57+
58+
<!-- tabs:start -->
59+
60+
### **Python3**
61+
62+
<!-- 这里可写当前语言的特殊实现逻辑 -->
63+
64+
```python
65+
66+
```
67+
68+
### **Java**
69+
70+
<!-- 这里可写当前语言的特殊实现逻辑 -->
71+
72+
```java
73+
74+
```
75+
76+
### **C++**
77+
78+
```cpp
79+
80+
```
81+
82+
### **Go**
83+
84+
```go
85+
86+
```
87+
88+
### **...**
89+
90+
```
91+
92+
```
93+
94+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# [2981. Find Longest Special Substring That Occurs Thrice I](https://leetcode.com/problems/find-longest-special-substring-that-occurs-thrice-i)
2+
3+
[中文文档](/solution/2900-2999/2981.Find%20Longest%20Special%20Substring%20That%20Occurs%20Thrice%20I/README.md)
4+
5+
## Description
6+
7+
<p>You are given a string <code>s</code> that consists of lowercase English letters.</p>
8+
9+
<p>A string is called <strong>special</strong> if it is made up of only a single character. For example, the string <code>&quot;abc&quot;</code> is not special, whereas the strings <code>&quot;ddd&quot;</code>, <code>&quot;zz&quot;</code>, and <code>&quot;f&quot;</code> are special.</p>
10+
11+
<p>Return <em>the length of the <strong>longest special substring</strong> of </em><code>s</code> <em>which occurs <strong>at least thrice</strong></em>, <em>or </em><code>-1</code><em> if no special substring occurs at least thrice</em>.</p>
12+
13+
<p>A <strong>substring</strong> is a contiguous <strong>non-empty</strong> sequence of characters within a string.</p>
14+
15+
<p>&nbsp;</p>
16+
<p><strong class="example">Example 1:</strong></p>
17+
18+
<pre>
19+
<strong>Input:</strong> s = &quot;aaaa&quot;
20+
<strong>Output:</strong> 2
21+
<strong>Explanation:</strong> The longest special substring which occurs thrice is &quot;aa&quot;: substrings &quot;<u><strong>aa</strong></u>aa&quot;, &quot;a<u><strong>aa</strong></u>a&quot;, and &quot;aa<u><strong>aa</strong></u>&quot;.
22+
It can be shown that the maximum length achievable is 2.
23+
</pre>
24+
25+
<p><strong class="example">Example 2:</strong></p>
26+
27+
<pre>
28+
<strong>Input:</strong> s = &quot;abcdef&quot;
29+
<strong>Output:</strong> -1
30+
<strong>Explanation:</strong> There exists no special substring which occurs at least thrice. Hence return -1.
31+
</pre>
32+
33+
<p><strong class="example">Example 3:</strong></p>
34+
35+
<pre>
36+
<strong>Input:</strong> s = &quot;abcaba&quot;
37+
<strong>Output:</strong> 1
38+
<strong>Explanation:</strong> The longest special substring which occurs thrice is &quot;a&quot;: substrings &quot;<u><strong>a</strong></u>bcaba&quot;, &quot;abc<u><strong>a</strong></u>ba&quot;, and &quot;abcab<u><strong>a</strong></u>&quot;.
39+
It can be shown that the maximum length achievable is 1.
40+
</pre>
41+
42+
<p>&nbsp;</p>
43+
<p><strong>Constraints:</strong></p>
44+
45+
<ul>
46+
<li><code>3 &lt;= s.length &lt;= 50</code></li>
47+
<li><code>s</code> consists of only lowercase English letters.</li>
48+
</ul>
49+
50+
## Solutions
51+
52+
<!-- tabs:start -->
53+
54+
### **Python3**
55+
56+
```python
57+
58+
```
59+
60+
### **Java**
61+
62+
```java
63+
64+
```
65+
66+
### **C++**
67+
68+
```cpp
69+
70+
```
71+
72+
### **Go**
73+
74+
```go
75+
76+
```
77+
78+
### **...**
79+
80+
```
81+
82+
```
83+
84+
<!-- tabs:end -->

0 commit comments

Comments
 (0)