Skip to content

Commit 1aad380

Browse files
authored
feat: add biweekly contest 132 (doocs#3080)
1 parent 3407664 commit 1aad380

File tree

15 files changed

+1081
-1
lines changed

15 files changed

+1081
-1
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
---
2+
comments: true
3+
difficulty: 困难
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3171.Find%20Subarray%20With%20Bitwise%20OR%20Closest%20to%20K/README.md
5+
tags:
6+
- 位运算
7+
- 线段树
8+
- 数组
9+
- 二分查找
10+
---
11+
12+
<!-- problem:start -->
13+
14+
# [3171. 找到按位与最接近 K 的子数组](https://leetcode.cn/problems/find-subarray-with-bitwise-or-closest-to-k)
15+
16+
[English Version](/solution/3100-3199/3171.Find%20Subarray%20With%20Bitwise%20OR%20Closest%20to%20K/README_EN.md)
17+
18+
## 题目描述
19+
20+
<!-- description:start -->
21+
22+
<p>给你一个数组&nbsp;<code>nums</code>&nbsp;和一个整数&nbsp;<code>k</code>&nbsp;。你需要找到&nbsp;<code>nums</code>&nbsp;的一个&nbsp;<span data-keyword="subarray-nonempty">子数组</span>&nbsp;,满足子数组中所有元素按位与运算&nbsp;<code>AND</code>&nbsp;的值与 <code>k</code>&nbsp;的 <strong>绝对差</strong>&nbsp;尽可能 <strong>小</strong>&nbsp;。换言之,你需要选择一个子数组&nbsp;<code>nums[l..r]</code>&nbsp;满足&nbsp;<code>|k - (nums[l] AND nums[l + 1] ... AND nums[r])|</code>&nbsp;最小。</p>
23+
24+
<p>请你返回 <strong>最小</strong>&nbsp;的绝对差值。</p>
25+
26+
<p><strong>子数组</strong>是数组中连续的&nbsp;<strong>非空</strong>&nbsp;元素序列。</p>
27+
28+
<p>&nbsp;</p>
29+
30+
<p><strong class="example">示例 1:</strong></p>
31+
32+
<div class="example-block">
33+
<p><span class="example-io"><b>输入:</b>nums = [1,2,4,5], k = 3</span></p>
34+
35+
<p><span class="example-io"><b>输出:</b>1</span></p>
36+
37+
<p><strong>解释:</strong></p>
38+
39+
<p>子数组&nbsp;<code>nums[2..3]</code> 的按位&nbsp;<code>AND</code>&nbsp;运算值为 4 ,得到最小差值&nbsp;<code>|3 - 4| = 1</code>&nbsp;。</p>
40+
</div>
41+
42+
<p><strong class="example">示例 2:</strong></p>
43+
44+
<div class="example-block">
45+
<p><span class="example-io"><b>输入:</b>nums = [1,2,1,2], k = 2</span></p>
46+
47+
<p><span class="example-io"><b>输出:</b>0</span></p>
48+
49+
<p><strong>解释:</strong></p>
50+
51+
<p>子数组&nbsp;<code>nums[1..1]</code> 的按位&nbsp;<code>AND</code>&nbsp;运算值为 2 ,得到最小差值&nbsp;<code>|2 - 2| = 0</code>&nbsp;。</p>
52+
</div>
53+
54+
<p><strong class="example">示例 3:</strong></p>
55+
56+
<div class="example-block">
57+
<p><span class="example-io"><b>输入:</b>nums = [1], k = 10</span></p>
58+
59+
<p><span class="example-io"><b>输出:</b>9</span></p>
60+
61+
<p><strong>解释:</strong></p>
62+
63+
<p>只有一个子数组,按位&nbsp;<code>AND</code>&nbsp;运算值为 1 ,得到最小差值&nbsp;<code>|10 - 1| = 9</code>&nbsp;。</p>
64+
</div>
65+
66+
<p>&nbsp;</p>
67+
68+
<p><strong>提示:</strong></p>
69+
70+
<ul>
71+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
72+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
73+
<li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li>
74+
</ul>
75+
76+
<!-- description:end -->
77+
78+
## 解法
79+
80+
<!-- solution:start -->
81+
82+
### 方法一
83+
84+
<!-- tabs:start -->
85+
86+
#### Python3
87+
88+
```python
89+
90+
```
91+
92+
#### Java
93+
94+
```java
95+
96+
```
97+
98+
#### C++
99+
100+
```cpp
101+
102+
```
103+
104+
#### Go
105+
106+
```go
107+
108+
```
109+
110+
<!-- tabs:end -->
111+
112+
<!-- solution:end -->
113+
114+
<!-- problem:end -->
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
---
2+
comments: true
3+
difficulty: Hard
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3171.Find%20Subarray%20With%20Bitwise%20OR%20Closest%20to%20K/README_EN.md
5+
tags:
6+
- Bit Manipulation
7+
- Segment Tree
8+
- Array
9+
- Binary Search
10+
---
11+
12+
<!-- problem:start -->
13+
14+
# [3171. Find Subarray With Bitwise OR Closest to K](https://leetcode.com/problems/find-subarray-with-bitwise-or-closest-to-k)
15+
16+
[中文文档](/solution/3100-3199/3171.Find%20Subarray%20With%20Bitwise%20OR%20Closest%20to%20K/README.md)
17+
18+
## Description
19+
20+
<!-- description:start -->
21+
22+
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p>
23+
24+
<p>Return the <strong>minimum</strong> possible value of the absolute difference.</p>
25+
26+
<p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p>
27+
28+
<p>&nbsp;</p>
29+
<p><strong class="example">Example 1:</strong></p>
30+
31+
<div class="example-block">
32+
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p>
33+
34+
<p><strong>Output:</strong> 0</p>
35+
36+
<p><strong>Explanation:</strong></p>
37+
38+
<p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p>
39+
</div>
40+
41+
<p><strong class="example">Example 2:</strong></p>
42+
43+
<div class="example-block">
44+
<p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p>
45+
46+
<p><strong>Output:</strong> 1</p>
47+
48+
<p><strong>Explanation:</strong></p>
49+
50+
<p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p>
51+
</div>
52+
53+
<p><strong class="example">Example 3:</strong></p>
54+
55+
<div class="example-block">
56+
<p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p>
57+
58+
<p><strong>Output:</strong> <span class="example-io">9</span></p>
59+
60+
<p><strong>Explanation:</strong></p>
61+
62+
<p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p>
63+
</div>
64+
65+
<p>&nbsp;</p>
66+
<p><strong>Constraints:</strong></p>
67+
68+
<ul>
69+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
70+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
71+
<li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li>
72+
</ul>
73+
74+
<!-- description:end -->
75+
76+
## Solutions
77+
78+
<!-- solution:start -->
79+
80+
### Solution 1
81+
82+
<!-- tabs:start -->
83+
84+
#### Python3
85+
86+
```python
87+
88+
```
89+
90+
#### Java
91+
92+
```java
93+
94+
```
95+
96+
#### C++
97+
98+
```cpp
99+
100+
```
101+
102+
#### Go
103+
104+
```go
105+
106+
```
107+
108+
<!-- tabs:end -->
109+
110+
<!-- solution:end -->
111+
112+
<!-- problem:end -->
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
---
2+
comments: true
3+
difficulty: 简单
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3174.Clear%20Digits/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3174. 清除数字](https://leetcode.cn/problems/clear-digits)
10+
11+
[English Version](/solution/3100-3199/3174.Clear%20Digits/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>给你一个字符串&nbsp;<code>s</code>&nbsp;。</p>
18+
19+
<p>你的任务是重复以下操作删除 <strong>所有</strong>&nbsp;数字字符:</p>
20+
21+
<ul>
22+
<li>删除 <strong>第一个数字字符</strong>&nbsp;以及它左边 <strong>最近</strong>&nbsp;的 <strong>非数字</strong>&nbsp;字符。</li>
23+
</ul>
24+
25+
<p>请你返回删除所有数字字符以后剩下的字符串。</p>
26+
27+
<p>&nbsp;</p>
28+
29+
<p><strong class="example">示例 1:</strong></p>
30+
31+
<div class="example-block">
32+
<p><span class="example-io"><b>输入:</b>s = "abc"</span></p>
33+
34+
<p><span class="example-io"><b>输出:</b>"abc"</span></p>
35+
36+
<p><strong>解释:</strong></p>
37+
38+
<p>字符串中没有数字。<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p>
39+
</div>
40+
41+
<p><strong class="example">示例 2:</strong></p>
42+
43+
<div class="example-block">
44+
<p><span class="example-io"><b>输入:</b>s = "cb34"</span></p>
45+
46+
<p><span class="example-io"><b>输出:</b>""</span></p>
47+
48+
<p><b>解释:</b></p>
49+
50+
<p>一开始,我们对&nbsp;<code>s[2]</code>&nbsp;执行操作,<code>s</code>&nbsp;变为&nbsp;<code>"c4"</code>&nbsp;。</p>
51+
52+
<p>然后对&nbsp;<code>s[1]</code>&nbsp;执行操作,<code>s</code>&nbsp;变为&nbsp;<code>""</code>&nbsp;。</p>
53+
</div>
54+
55+
<p>&nbsp;</p>
56+
57+
<p><strong>提示:</strong></p>
58+
59+
<ul>
60+
<li><code>1 &lt;= s.length &lt;= 100</code></li>
61+
<li><code>s</code>&nbsp;只包含小写英文字母和数字字符。</li>
62+
<li>输入保证所有数字都可以按以上操作被删除。</li>
63+
</ul>
64+
65+
<!-- description:end -->
66+
67+
## 解法
68+
69+
<!-- solution:start -->
70+
71+
### 方法一
72+
73+
<!-- tabs:start -->
74+
75+
#### Python3
76+
77+
```python
78+
79+
```
80+
81+
#### Java
82+
83+
```java
84+
85+
```
86+
87+
#### C++
88+
89+
```cpp
90+
91+
```
92+
93+
#### Go
94+
95+
```go
96+
97+
```
98+
99+
<!-- tabs:end -->
100+
101+
<!-- solution:end -->
102+
103+
<!-- problem:end -->

0 commit comments

Comments
 (0)