Skip to content

Commit 13f5964

Browse files
committedDec 26, 2022
feat: add solutions to lc problem: No.1150
No.1150.Check If a Number Is Majority Element in a Sorted Array
1 parent 0eda68c commit 13f5964

File tree

6 files changed

+92
-143
lines changed

6 files changed

+92
-143
lines changed
 

Diff for: ‎solution/1100-1199/1150.Check If a Number Is Majority Element in a Sorted Array/README.md

+36-60
Original file line numberDiff line numberDiff line change
@@ -46,54 +46,24 @@
4646

4747
<!-- 这里可写通用的实现逻辑 -->
4848

49+
**方法一:二分查找**
50+
4951
“二分查找”求 `target` 在数组 `nums` 中的左右边界。
5052

53+
时间复杂度 $O(\log n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 `nums` 的长度。
54+
5155
<!-- tabs:start -->
5256

5357
### **Python3**
5458

5559
<!-- 这里可写当前语言的特殊实现逻辑 -->
5660

57-
自己实现二分查找:
58-
5961
```python
6062
class Solution:
6163
def isMajorityElement(self, nums: List[int], target: int) -> bool:
62-
def bsearch_left(nums, target, left, right):
63-
while left < right:
64-
mid = (left + right) >> 1
65-
if nums[mid] >= target:
66-
right = mid
67-
else:
68-
left = mid + 1
69-
return left if nums[left] == target else -1
70-
71-
def bsearch_right(nums, target, left, right):
72-
while left < right:
73-
mid = (left + right + 1) >> 1
74-
if nums[mid] <= target:
75-
left = mid
76-
else:
77-
right = mid - 1
78-
return left if nums[left] == target else -1
79-
80-
n = len(nums)
81-
left = bsearch_left(nums, target, 0, n - 1)
82-
if left == -1:
83-
return False
84-
right = bsearch_right(nums, target, left, n - 1)
85-
if right == -1:
86-
return False
87-
return right - left + 1 > n >> 1
88-
```
89-
90-
使用 `bisect` 实现二分查找:
91-
92-
```python
93-
class Solution:
94-
def isMajorityElement(self, nums: List[int], target: int) -> bool:
95-
left, right = bisect_left(nums, target), bisect_right(nums, target)
96-
return right - left > (len(nums) >> 1)
64+
left = bisect_left(nums, target)
65+
right = bisect_right(nums, target)
66+
return right - left > len(nums) // 2
9767
```
9868

9969
### **Java**
@@ -103,41 +73,47 @@ class Solution:
10373
```java
10474
class Solution {
10575
public boolean isMajorityElement(int[] nums, int target) {
106-
int n = nums.length;
107-
int left = bsearchLeft(nums, target, 0, n - 1);
108-
if (left == -1) {
109-
return false;
110-
}
111-
int right = bsearchRight(nums, target, left, n - 1);
112-
if (right == -1) {
113-
return false;
114-
}
115-
return right - left + 1 > (n >> 1);
76+
int left = search(nums, target);
77+
int right = search(nums, target + 1);
78+
return right - left > nums.length / 2;
11679
}
11780

118-
private int bsearchLeft(int[] nums, int target, int left, int right) {
81+
private int search(int[] nums, int x) {
82+
int left = 0, right = nums.length;
11983
while (left < right) {
12084
int mid = (left + right) >> 1;
121-
if (nums[mid] >= target) {
85+
if (nums[mid] >= x) {
12286
right = mid;
12387
} else {
12488
left = mid + 1;
12589
}
12690
}
127-
return nums[left] == target ? left : -1;
91+
return left;
12892
}
93+
}
94+
```
12995

130-
private int bsearchRight(int[] nums, int target, int left, int right) {
131-
while (left < right) {
132-
int mid = (left + right + 1) >> 1;
133-
if (nums[mid] <= target) {
134-
left = mid;
135-
} else {
136-
right = mid - 1;
137-
}
138-
}
139-
return nums[left] == target ? left : -1;
96+
### **C++**
97+
98+
```cpp
99+
class Solution {
100+
public:
101+
bool isMajorityElement(vector<int>& nums, int target) {
102+
auto left = lower_bound(nums.begin(), nums.end(), target);
103+
auto right = upper_bound(nums.begin(), nums.end(), target);
104+
return right - left > nums.size() / 2;
140105
}
106+
};
107+
```
108+
109+
### **Go**
110+
111+
```go
112+
func isMajorityElement(nums []int, target int) bool {
113+
n := len(nums)
114+
left := sort.Search(n, func(i int) bool { return nums[i] >= target })
115+
right := sort.Search(n, func(i int) bool { return nums[i] > target })
116+
return right-left > n/2
141117
}
142118
```
143119

Diff for: ‎solution/1100-1199/1150.Check If a Number Is Majority Element in a Sorted Array/README_EN.md

+32-56
Original file line numberDiff line numberDiff line change
@@ -45,81 +45,57 @@ Thus, 101 is not a majority element because 2 &gt; 4/2 is false.
4545
```python
4646
class Solution:
4747
def isMajorityElement(self, nums: List[int], target: int) -> bool:
48-
def bsearch_left(nums, target, left, right):
49-
while left < right:
50-
mid = (left + right) >> 1
51-
if nums[mid] >= target:
52-
right = mid
53-
else:
54-
left = mid + 1
55-
return left if nums[left] == target else -1
56-
57-
def bsearch_right(nums, target, left, right):
58-
while left < right:
59-
mid = (left + right + 1) >> 1
60-
if nums[mid] <= target:
61-
left = mid
62-
else:
63-
right = mid - 1
64-
return left if nums[left] == target else -1
65-
66-
n = len(nums)
67-
left = bsearch_left(nums, target, 0, n - 1)
68-
if left == -1:
69-
return False
70-
right = bsearch_right(nums, target, left, n - 1)
71-
if right == -1:
72-
return False
73-
return right - left + 1 > n >> 1
74-
```
75-
76-
```python
77-
class Solution:
78-
def isMajorityElement(self, nums: List[int], target: int) -> bool:
79-
left, right = bisect_left(nums, target), bisect_right(nums, target)
80-
return right - left > (len(nums) >> 1)
48+
left = bisect_left(nums, target)
49+
right = bisect_right(nums, target)
50+
return right - left > len(nums) // 2
8151
```
8252

8353
### **Java**
8454

8555
```java
8656
class Solution {
8757
public boolean isMajorityElement(int[] nums, int target) {
88-
int n = nums.length;
89-
int left = bsearchLeft(nums, target, 0, n - 1);
90-
if (left == -1) {
91-
return false;
92-
}
93-
int right = bsearchRight(nums, target, left, n - 1);
94-
if (right == -1) {
95-
return false;
96-
}
97-
return right - left + 1 > (n >> 1);
58+
int left = search(nums, target);
59+
int right = search(nums, target + 1);
60+
return right - left > nums.length / 2;
9861
}
9962

100-
private int bsearchLeft(int[] nums, int target, int left, int right) {
63+
private int search(int[] nums, int x) {
64+
int left = 0, right = nums.length;
10165
while (left < right) {
10266
int mid = (left + right) >> 1;
103-
if (nums[mid] >= target) {
67+
if (nums[mid] >= x) {
10468
right = mid;
10569
} else {
10670
left = mid + 1;
10771
}
10872
}
109-
return nums[left] == target ? left : -1;
73+
return left;
11074
}
75+
}
76+
```
11177

112-
private int bsearchRight(int[] nums, int target, int left, int right) {
113-
while (left < right) {
114-
int mid = (left + right + 1) >> 1;
115-
if (nums[mid] <= target) {
116-
left = mid;
117-
} else {
118-
right = mid - 1;
119-
}
120-
}
121-
return nums[left] == target ? left : -1;
78+
### **C++**
79+
80+
```cpp
81+
class Solution {
82+
public:
83+
bool isMajorityElement(vector<int>& nums, int target) {
84+
auto left = lower_bound(nums.begin(), nums.end(), target);
85+
auto right = upper_bound(nums.begin(), nums.end(), target);
86+
return right - left > nums.size() / 2;
12287
}
88+
};
89+
```
90+
91+
### **Go**
92+
93+
```go
94+
func isMajorityElement(nums []int, target int) bool {
95+
n := len(nums)
96+
left := sort.Search(n, func(i int) bool { return nums[i] >= target })
97+
right := sort.Search(n, func(i int) bool { return nums[i] > target })
98+
return right-left > n/2
12399
}
124100
```
125101

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution {
2+
public:
3+
bool isMajorityElement(vector<int>& nums, int target) {
4+
auto left = lower_bound(nums.begin(), nums.end(), target);
5+
auto right = upper_bound(nums.begin(), nums.end(), target);
6+
return right - left > nums.size() / 2;
7+
}
8+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
func isMajorityElement(nums []int, target int) bool {
2+
n := len(nums)
3+
left := sort.Search(n, func(i int) bool { return nums[i] >= target })
4+
right := sort.Search(n, func(i int) bool { return nums[i] > target })
5+
return right-left > n/2
6+
}
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,20 @@
11
class Solution {
22
public boolean isMajorityElement(int[] nums, int target) {
3-
int n = nums.length;
4-
int left = bsearchLeft(nums, target, 0, n - 1);
5-
if (left == -1) {
6-
return false;
7-
}
8-
int right = bsearchRight(nums, target, left, n - 1);
9-
if (right == -1) {
10-
return false;
11-
}
12-
return right - left + 1 > (n >> 1);
3+
int left = search(nums, target);
4+
int right = search(nums, target + 1);
5+
return right - left > nums.length / 2;
136
}
147

15-
private int bsearchLeft(int[] nums, int target, int left, int right) {
8+
private int search(int[] nums, int x) {
9+
int left = 0, right = nums.length;
1610
while (left < right) {
1711
int mid = (left + right) >> 1;
18-
if (nums[mid] >= target) {
12+
if (nums[mid] >= x) {
1913
right = mid;
2014
} else {
2115
left = mid + 1;
2216
}
2317
}
24-
return nums[left] == target ? left : -1;
25-
}
26-
27-
private int bsearchRight(int[] nums, int target, int left, int right) {
28-
while (left < right) {
29-
int mid = (left + right + 1) >> 1;
30-
if (nums[mid] <= target) {
31-
left = mid;
32-
} else {
33-
right = mid - 1;
34-
}
35-
}
36-
return nums[left] == target ? left : -1;
18+
return left;
3719
}
3820
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
class Solution:
22
def isMajorityElement(self, nums: List[int], target: int) -> bool:
3-
left, right = bisect_left(nums, target), bisect_right(nums, target)
4-
return right - left > (len(nums) >> 1)
3+
left = bisect_left(nums, target)
4+
right = bisect_right(nums, target)
5+
return right - left > len(nums) // 2

0 commit comments

Comments
 (0)