Skip to content

Commit 3ac8592

Browse files
authoredMar 23, 2025
Merge pull request #1153 from 0xff-dev/2401
Add solution and test-cases for problem 2401
2 parents 5caf6de + a4c4941 commit 3ac8592

File tree

3 files changed

+53
-23
lines changed

3 files changed

+53
-23
lines changed
 

‎leetcode/2401-2500/2401.Longest-Nice-Subarray/README.md

+21-14
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11
# [2401.Longest Nice Subarray][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given an array `nums` consisting of **positive** integers.
5+
6+
We call a subarray of `nums` **nice** if the bitwise **AND** of every pair of elements that are in **different** positions in the subarray is equal to `0`.
7+
8+
Return the length of the **longest** nice subarray.
9+
10+
A **subarray** is a **contiguous** part of an array.
11+
12+
**Note** that subarrays of length `1` are always considered nice.
713

814
**Example 1:**
915

1016
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
17+
Input: nums = [1,3,8,48,10]
18+
Output: 3
19+
Explanation: The longest nice subarray is [3,8,48]. This subarray satisfies the conditions:
20+
- 3 AND 8 = 0.
21+
- 3 AND 48 = 0.
22+
- 8 AND 48 = 0.
23+
It can be proven that no longer nice subarray can be obtained, so we return 3.
1324
```
1425

15-
## 题意
16-
> ...
17-
18-
## 题解
26+
**Example 2:**
1927

20-
### 思路1
21-
> ...
22-
Longest Nice Subarray
23-
```go
2428
```
25-
29+
Input: nums = [3,1,5,11,13]
30+
Output: 1
31+
Explanation: The length of the longest nice subarray is 1. Any subarray of length 1 can be chosen.
32+
```
2633

2734
## 结语
2835

Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int) int {
4+
if len(nums) == 1 {
5+
return 1
6+
}
7+
8+
cur := 0
9+
start, end := 0, 0
10+
ans := 0
11+
for ; end < len(nums); end++ {
12+
if cur&nums[end] == 0 {
13+
cur |= nums[end]
14+
continue
15+
}
16+
ans = max(ans, end-start)
17+
18+
for ; start <= end; start++ {
19+
cur ^= nums[start]
20+
if cur&nums[end] == 0 {
21+
start++
22+
cur |= nums[end]
23+
break
24+
}
25+
}
26+
}
27+
ans = max(ans, end-start)
28+
return ans
529
}

‎leetcode/2401-2500/2401.Longest-Nice-Subarray/Solution_test.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{1, 3, 8, 48, 10}, 3},
17+
{"TestCase2", []int{3, 1, 5, 11, 13}, 1},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)