60
60
61
61
### 方法一:遍历
62
62
63
- 遍历数组 ,统计正整数和负整数的个数 $a$ 和 $b$,返回 $a$ 和 $b$ 中的较大值即可。
63
+ 我们可以直接遍历数组 ,统计正整数和负整数的个数 $a$ 和 $b$,返回 $a$ 和 $b$ 中的较大值即可。
64
64
65
- 时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组长度 。
65
+ 时间复杂度 $O(n)$,其中 $n$ 为数组长度。 空间复杂度 $O(1)$。
66
66
67
67
<!-- tabs:start -->
68
68
69
69
``` python
70
70
class Solution :
71
71
def maximumCount (self , nums : List[int ]) -> int :
72
- a = sum (v > 0 for v in nums)
73
- b = sum (v < 0 for v in nums)
72
+ a = sum (x > 0 for x in nums)
73
+ b = sum (x < 0 for x in nums)
74
74
return max (a, b)
75
75
```
76
76
77
77
``` java
78
78
class Solution {
79
79
public int maximumCount (int [] nums ) {
80
80
int a = 0 , b = 0 ;
81
- for (int v : nums) {
82
- if (v > 0 ) {
81
+ for (int x : nums) {
82
+ if (x > 0 ) {
83
83
++ a;
84
- }
85
- if (v < 0 ) {
84
+ } else if (x < 0 ) {
86
85
++ b;
87
86
}
88
87
}
@@ -96,11 +95,10 @@ class Solution {
96
95
public:
97
96
int maximumCount(vector<int >& nums) {
98
97
int a = 0, b = 0;
99
- for (int& v : nums) {
100
- if (v > 0) {
98
+ for (int x : nums) {
99
+ if (x > 0) {
101
100
++a;
102
- }
103
- if (v < 0) {
101
+ } else if (x < 0) {
104
102
++b;
105
103
}
106
104
}
@@ -111,12 +109,11 @@ public:
111
109
112
110
```go
113
111
func maximumCount(nums []int) int {
114
- a, b := 0, 0
115
- for _, v := range nums {
116
- if v > 0 {
112
+ var a, b int
113
+ for _, x := range nums {
114
+ if x > 0 {
117
115
a++
118
- }
119
- if v < 0 {
116
+ } else if x < 0 {
120
117
b++
121
118
}
122
119
}
@@ -126,47 +123,50 @@ func maximumCount(nums []int) int {
126
123
127
124
``` ts
128
125
function maximumCount(nums : number []): number {
129
- const count = [0 , 0 ];
130
- for (const num of nums ) {
131
- if (num < 0 ) {
132
- count [ 0 ] ++ ;
133
- } else if (num > 0 ) {
134
- count [ 1 ] ++ ;
126
+ let [a, b] = [0 , 0 ];
127
+ for (const x of nums ) {
128
+ if (x > 0 ) {
129
+ ++ a ;
130
+ } else if (x < 0 ) {
131
+ ++ b ;
135
132
}
136
133
}
137
- return Math .max (... count );
134
+ return Math .max (a , b );
138
135
}
139
136
```
140
137
141
138
``` rust
142
139
impl Solution {
143
140
pub fn maximum_count (nums : Vec <i32 >) -> i32 {
144
- let mut count = [0 , 0 ];
145
- for & num in nums . iter () {
146
- if num < 0 {
147
- count [0 ] += 1 ;
148
- } else if num > 0 {
149
- count [1 ] += 1 ;
141
+ let mut a = 0 ;
142
+ let mut b = 0 ;
143
+
144
+ for x in nums {
145
+ if x > 0 {
146
+ a += 1 ;
147
+ } else if x < 0 {
148
+ b += 1 ;
150
149
}
151
150
}
152
- * count . iter (). max (). unwrap ()
151
+
152
+ std :: cmp :: max (a , b )
153
153
}
154
154
}
155
155
```
156
156
157
157
``` c
158
- #define max (a, b ) (((a) > (b)) ? (a) : (b) )
158
+ #define max (a, b ) (a > b ? a : b )
159
159
160
160
int maximumCount (int* nums, int numsSize) {
161
- int count [ 2 ] = {0} ;
162
- for (int i = 0; i < numsSize; i++ ) {
163
- if (nums[ i] < 0) {
164
- count [ 0 ] ++ ;
165
- } else if (nums[ i] > 0) {
166
- count [ 1 ] ++ ;
161
+ int a = 0, b = 0 ;
162
+ for (int i = 0; i < numsSize; ++i ) {
163
+ if (nums[ i] > 0) {
164
+ ++a ;
165
+ } else if (nums[ i] < 0) {
166
+ ++b ;
167
167
}
168
168
}
169
- return max(count [ 0 ] , count [ 1 ] );
169
+ return max(a, b );
170
170
}
171
171
```
172
172
@@ -176,7 +176,7 @@ int maximumCount(int* nums, int numsSize) {
176
176
177
177
由于数组是按非递减顺序排列的,因此可以使用二分查找找到第一个大于等于 $1$ 的元素的下标 $i$ 以及第一个大于等于 $0$ 的元素的下标 $j$,那么正整数的个数 $a = n - i$,负整数的个数 $b = j$,返回 $a$ 和 $b$ 中的较大值即可。
178
178
179
- 时间复杂度 $O(\log n)$,空间复杂度 $O(1)$。其中 $n$ 为数组长度 。
179
+ 时间复杂度 $O(\log n)$,其中 $n$ 为数组长度。 空间复杂度 $O(1)$。
180
180
181
181
<!-- tabs:start -->
182
182
@@ -232,97 +232,71 @@ func maximumCount(nums []int) int {
232
232
233
233
``` ts
234
234
function maximumCount(nums : number []): number {
235
- const search = (target : number ) => {
236
- let left = 0 ;
237
- let right = n ;
235
+ const search = (x : number ): number => {
236
+ let [left, right] = [0 , nums .length ];
238
237
while (left < right ) {
239
- const mid = (left + right ) >>> 1 ;
240
- if (nums [mid ] < target ) {
241
- left = mid + 1 ;
242
- } else {
238
+ const mid = (left + right ) >> 1 ;
239
+ if (nums [mid ] >= x ) {
243
240
right = mid ;
241
+ } else {
242
+ left = mid + 1 ;
244
243
}
245
244
}
246
245
return left ;
247
246
};
248
- const n = nums . length ;
249
- const i = search (0 );
250
- const j = search ( 1 ) ;
251
- return Math .max (i , n - j );
247
+ const i = search ( 1 ) ;
248
+ const j = search (0 );
249
+ const [a, b] = [ nums . length - i , j ] ;
250
+ return Math .max (a , b );
252
251
}
253
252
```
254
253
255
254
``` rust
256
255
impl Solution {
257
- fn search (nums : & Vec <i32 >, target : i32 ) -> usize {
256
+ fn search (nums : & Vec <i32 >, x : i32 ) -> usize {
258
257
let mut left = 0 ;
259
258
let mut right = nums . len ();
260
259
while left < right {
261
260
let mid = (left + right ) >> 1 ;
262
- if nums [mid ] < target {
263
- left = mid + 1 ;
264
- } else {
261
+ if nums [mid ] >= x {
265
262
right = mid ;
263
+ } else {
264
+ left = mid + 1 ;
266
265
}
267
266
}
268
267
left
269
268
}
270
269
271
270
pub fn maximum_count (nums : Vec <i32 >) -> i32 {
272
271
let n = nums . len ();
273
- let i = Self :: search (& nums , 0 );
274
- let j = Self :: search (& nums , 1 );
275
- i . max (n - j ) as i32
272
+ let i = Self :: search (& nums , 1 );
273
+ let j = Self :: search (& nums , 0 );
274
+ (n - i ) . max ( j ) as i32
276
275
}
277
276
}
278
277
```
279
278
280
279
``` c
281
- #define max (a, b ) (((a) > (b)) ? (a) : (b) )
280
+ #define max (a, b ) (a > b ? a : b )
282
281
283
- int search (int* nums, int numsSize, int target ) {
282
+ int search (int* nums, int numsSize, int x ) {
284
283
int left = 0;
285
284
int right = numsSize;
286
285
while (left < right) {
287
286
int mid = (left + right) >> 1;
288
- if (nums[ mid] < target) {
289
- left = mid + 1;
290
- } else {
287
+ if (nums[ mid] >= x) {
291
288
right = mid;
289
+ } else {
290
+ left = mid + 1;
292
291
}
293
292
}
294
293
return left;
295
294
}
296
295
297
296
int maximumCount(int* nums, int numsSize) {
298
- int i = search(nums, numsSize, 0);
299
- int j = search(nums, numsSize, 1);
300
- return max(i, numsSize - j);
301
- }
302
- ```
303
-
304
- <!-- tabs:end -->
305
-
306
- ### 方法三
307
-
308
- <!-- tabs:start -->
309
-
310
- ```rust
311
- impl Solution {
312
- pub fn maximum_count(nums: Vec<i32>) -> i32 {
313
- let mut a = 0;
314
- let mut b = 0;
315
-
316
- for n in nums {
317
- if n > 0 {
318
- a += 1;
319
- } else if n < 0 {
320
- b += 1;
321
- }
322
- }
323
-
324
- std::cmp::max(a, b)
325
- }
297
+ int i = search(nums, numsSize, 1);
298
+ int j = search(nums, numsSize, 0);
299
+ return max(numsSize - i, j);
326
300
}
327
301
```
328
302
0 commit comments