File tree 4 files changed +111
-1
lines changed
solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND
4 files changed +111
-1
lines changed Original file line number Diff line number Diff line change 52
52
<strong >输入:</strong >nums = [1,2,3,4]
53
53
<strong >输出:</strong >1
54
54
<strong >解释:</strong >
55
- 子数组按位与运算的最大值是 4 。
55
+ 子数组按位与运算的最大值是 4 。
56
56
能得到此结果的最长子数组是 [4],所以返回 1 。
57
57
</pre >
58
58
@@ -161,6 +161,46 @@ func longestSubarray(nums []int) int {
161
161
}
162
162
```
163
163
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
+
164
204
<!-- tabs: end -->
165
205
166
206
<!-- solution: end -->
Original file line number Diff line number Diff line change @@ -159,6 +159,46 @@ func longestSubarray(nums []int) int {
159
159
}
160
160
```
161
161
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
+
162
202
<!-- tabs: end -->
163
203
164
204
<!-- solution: end -->
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments