Skip to content

Commit 6ee014b

Browse files
authored
feat: add solutions to lc problem: No.2419 (#3527)
1 parent 8bae47f commit 6ee014b

File tree

4 files changed

+111
-1
lines changed

4 files changed

+111
-1
lines changed

solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/README.md

+41-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ tags:
5252
<strong>输入:</strong>nums = [1,2,3,4]
5353
<strong>输出:</strong>1
5454
<strong>解释:</strong>
55-
子数组按位与运算的最大值是 4 。
55+
子数组按位与运算的最大值是 4 。
5656
能得到此结果的最长子数组是 [4],所以返回 1 。
5757
</pre>
5858

@@ -161,6 +161,46 @@ func longestSubarray(nums []int) int {
161161
}
162162
```
163163

164+
#### TypeScript
165+
166+
```ts
167+
function longestSubarray(nums: number[]): number {
168+
const mx = Math.max(...nums);
169+
let [ans, cnt] = [0, 0];
170+
171+
for (const x of nums) {
172+
if (x === mx) {
173+
cnt++;
174+
ans = Math.max(ans, cnt);
175+
} else {
176+
cnt = 0;
177+
}
178+
}
179+
180+
return ans;
181+
}
182+
```
183+
184+
#### JavaScript
185+
186+
```js
187+
function longestSubarray(nums) {
188+
const mx = Math.max(...nums);
189+
let [ans, cnt] = [0, 0];
190+
191+
for (const x of nums) {
192+
if (x === mx) {
193+
cnt++;
194+
ans = Math.max(ans, cnt);
195+
} else {
196+
cnt = 0;
197+
}
198+
}
199+
200+
return ans;
201+
}
202+
```
203+
164204
<!-- tabs:end -->
165205

166206
<!-- solution:end -->

solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/README_EN.md

+40
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,46 @@ func longestSubarray(nums []int) int {
159159
}
160160
```
161161

162+
#### TypeScript
163+
164+
```ts
165+
function longestSubarray(nums: number[]): number {
166+
const mx = Math.max(...nums);
167+
let [ans, cnt] = [0, 0];
168+
169+
for (const x of nums) {
170+
if (x === mx) {
171+
cnt++;
172+
ans = Math.max(ans, cnt);
173+
} else {
174+
cnt = 0;
175+
}
176+
}
177+
178+
return ans;
179+
}
180+
```
181+
182+
#### JavaScript
183+
184+
```js
185+
function longestSubarray(nums) {
186+
const mx = Math.max(...nums);
187+
let [ans, cnt] = [0, 0];
188+
189+
for (const x of nums) {
190+
if (x === mx) {
191+
cnt++;
192+
ans = Math.max(ans, cnt);
193+
} else {
194+
cnt = 0;
195+
}
196+
}
197+
198+
return ans;
199+
}
200+
```
201+
162202
<!-- tabs:end -->
163203

164204
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function longestSubarray(nums) {
2+
const mx = Math.max(...nums);
3+
let [ans, cnt] = [0, 0];
4+
5+
for (const x of nums) {
6+
if (x === mx) {
7+
cnt++;
8+
ans = Math.max(ans, cnt);
9+
} else {
10+
cnt = 0;
11+
}
12+
}
13+
14+
return ans;
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function longestSubarray(nums: number[]): number {
2+
const mx = Math.max(...nums);
3+
let [ans, cnt] = [0, 0];
4+
5+
for (const x of nums) {
6+
if (x === mx) {
7+
cnt++;
8+
ans = Math.max(ans, cnt);
9+
} else {
10+
cnt = 0;
11+
}
12+
}
13+
14+
return ans;
15+
}

0 commit comments

Comments
 (0)