Skip to content

Commit b1510f0

Browse files
committed
feat: add solutions to lc problems: No.2148~2151
* No.2148.Count Elements With Strictly Smaller and Greater Elements * No.2149.Rearrange Array Elements by Sign * No.2150.Find All Lonely Numbers in the Array * No.2151.Maximum Good People Based on Statements
1 parent a07b44a commit b1510f0

File tree

24 files changed

+906
-8
lines changed

24 files changed

+906
-8
lines changed

solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/README.md

+65-1
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,79 @@
4747
<!-- 这里可写当前语言的特殊实现逻辑 -->
4848

4949
```python
50-
50+
class Solution:
51+
def countElements(self, nums: List[int]) -> int:
52+
mi, mx = min(nums), max(nums)
53+
return sum(mi < num < mx for num in nums)
5154
```
5255

5356
### **Java**
5457

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

5760
```java
61+
class Solution {
62+
63+
public int countElements(int[] nums) {
64+
int mi = 1000000, mx = -1000000;
65+
for (int num : nums) {
66+
mi = Math.min(mi, num);
67+
mx = Math.max(mx, num);
68+
}
69+
int ans = 0;
70+
for (int num : nums) {
71+
if (mi < num && num < mx) {
72+
++ans;
73+
}
74+
}
75+
return ans;
76+
}
77+
}
78+
79+
```
80+
81+
### **C++**
82+
83+
```cpp
84+
class Solution {
85+
public:
86+
int countElements(vector<int>& nums) {
87+
int mi = 1e6, mx = -1e6;
88+
for (int num : nums)
89+
{
90+
mi = min(mi, num);
91+
mx = max(mx, num);
92+
}
93+
int ans = 0;
94+
for (int num : nums)
95+
if (mi < num && num < mx)
96+
++ans;
97+
return ans;
98+
}
99+
};
100+
```
58101
102+
### **Go**
103+
104+
```go
105+
func countElements(nums []int) int {
106+
mi, mx := int(1e6), -int(1e6)
107+
for _, num := range nums {
108+
if num < mi {
109+
mi = num
110+
}
111+
if num > mx {
112+
mx = num
113+
}
114+
}
115+
ans := 0
116+
for _, num := range nums {
117+
if mi < num && num < mx {
118+
ans++
119+
}
120+
}
121+
return ans
122+
}
59123
```
60124

61125
### **TypeScript**

solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/README_EN.md

+65-1
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,77 @@ Since there are two elements with the value 3, in total there are 2 elements hav
4141
### **Python3**
4242

4343
```python
44-
44+
class Solution:
45+
def countElements(self, nums: List[int]) -> int:
46+
mi, mx = min(nums), max(nums)
47+
return sum(mi < num < mx for num in nums)
4548
```
4649

4750
### **Java**
4851

4952
```java
53+
class Solution {
54+
55+
public int countElements(int[] nums) {
56+
int mi = 1000000, mx = -1000000;
57+
for (int num : nums) {
58+
mi = Math.min(mi, num);
59+
mx = Math.max(mx, num);
60+
}
61+
int ans = 0;
62+
for (int num : nums) {
63+
if (mi < num && num < mx) {
64+
++ans;
65+
}
66+
}
67+
return ans;
68+
}
69+
}
70+
71+
```
72+
73+
### **C++**
74+
75+
```cpp
76+
class Solution {
77+
public:
78+
int countElements(vector<int>& nums) {
79+
int mi = 1e6, mx = -1e6;
80+
for (int num : nums)
81+
{
82+
mi = min(mi, num);
83+
mx = max(mx, num);
84+
}
85+
int ans = 0;
86+
for (int num : nums)
87+
if (mi < num && num < mx)
88+
++ans;
89+
return ans;
90+
}
91+
};
92+
```
5093
94+
### **Go**
95+
96+
```go
97+
func countElements(nums []int) int {
98+
mi, mx := int(1e6), -int(1e6)
99+
for _, num := range nums {
100+
if num < mi {
101+
mi = num
102+
}
103+
if num > mx {
104+
mx = num
105+
}
106+
}
107+
ans := 0
108+
for _, num := range nums {
109+
if mi < num && num < mx {
110+
ans++
111+
}
112+
}
113+
return ans
114+
}
51115
```
52116

53117
### **TypeScript**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int countElements(vector<int>& nums) {
4+
int mi = 1e6, mx = -1e6;
5+
for (int num : nums)
6+
{
7+
mi = min(mi, num);
8+
mx = max(mx, num);
9+
}
10+
int ans = 0;
11+
for (int num : nums)
12+
if (mi < num && num < mx)
13+
++ans;
14+
return ans;
15+
}
16+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
func countElements(nums []int) int {
2+
mi, mx := int(1e6), -int(1e6)
3+
for _, num := range nums {
4+
if num < mi {
5+
mi = num
6+
}
7+
if num > mx {
8+
mx = num
9+
}
10+
}
11+
ans := 0
12+
for _, num := range nums {
13+
if mi < num && num < mx {
14+
ans++
15+
}
16+
}
17+
return ans
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int countElements(int[] nums) {
3+
int mi = 1000000, mx = -1000000;
4+
for (int num : nums) {
5+
mi = Math.min(mi, num);
6+
mx = Math.max(mx, num);
7+
}
8+
int ans = 0;
9+
for (int num : nums) {
10+
if (mi < num && num < mx) {
11+
++ans;
12+
}
13+
}
14+
return ans;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
def countElements(self, nums: List[int]) -> int:
3+
mi, mx = min(nums), max(nums)
4+
return sum(mi < num < mx for num in nums)

solution/2100-2199/2149.Rearrange Array Elements by Sign/README.md

+74-1
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,88 @@ nums 中的正整数是 [3,1,2] ,负整数是 [-2,-5,-4] 。
6363
<!-- 这里可写当前语言的特殊实现逻辑 -->
6464

6565
```python
66-
66+
class Solution:
67+
def rearrangeArray(self, nums: List[int]) -> List[int]:
68+
ans = [0] * len(nums)
69+
i, j = 0, 1
70+
for num in nums:
71+
if num > 0:
72+
ans[i] = num
73+
i += 2
74+
else:
75+
ans[j] = num
76+
j += 2
77+
return ans
6778
```
6879

6980
### **Java**
7081

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

7384
```java
85+
class Solution {
86+
87+
public int[] rearrangeArray(int[] nums) {
88+
int[] ans = new int[nums.length];
89+
int i = 0, j = 1;
90+
for (int num : nums) {
91+
if (num > 0) {
92+
ans[i] = num;
93+
i += 2;
94+
} else {
95+
ans[j] = num;
96+
j += 2;
97+
}
98+
}
99+
return ans;
100+
}
101+
}
102+
103+
```
104+
105+
### **C++**
106+
107+
```cpp
108+
class Solution {
109+
public:
110+
vector<int> rearrangeArray(vector<int>& nums) {
111+
vector<int> ans(nums.size());
112+
int i = 0, j = 1;
113+
for (int num : nums)
114+
{
115+
if (num > 0)
116+
{
117+
ans[i] = num;
118+
i += 2;
119+
}
120+
else
121+
{
122+
ans[j] = num;
123+
j += 2;
124+
}
125+
}
126+
return ans;
127+
}
128+
};
129+
```
74130
131+
### **Go**
132+
133+
```go
134+
func rearrangeArray(nums []int) []int {
135+
ans := make([]int, len(nums))
136+
i, j := 0, 1
137+
for _, num := range nums {
138+
if num > 0 {
139+
ans[i] = num
140+
i += 2
141+
} else {
142+
ans[j] = num
143+
j += 2
144+
}
145+
}
146+
return ans
147+
}
75148
```
76149

77150
### **TypeScript**

solution/2100-2199/2149.Rearrange Array Elements by Sign/README_EN.md

+74-1
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,86 @@ So nums is rearranged to [1,-1].
5555
### **Python3**
5656

5757
```python
58-
58+
class Solution:
59+
def rearrangeArray(self, nums: List[int]) -> List[int]:
60+
ans = [0] * len(nums)
61+
i, j = 0, 1
62+
for num in nums:
63+
if num > 0:
64+
ans[i] = num
65+
i += 2
66+
else:
67+
ans[j] = num
68+
j += 2
69+
return ans
5970
```
6071

6172
### **Java**
6273

6374
```java
75+
class Solution {
76+
77+
public int[] rearrangeArray(int[] nums) {
78+
int[] ans = new int[nums.length];
79+
int i = 0, j = 1;
80+
for (int num : nums) {
81+
if (num > 0) {
82+
ans[i] = num;
83+
i += 2;
84+
} else {
85+
ans[j] = num;
86+
j += 2;
87+
}
88+
}
89+
return ans;
90+
}
91+
}
92+
93+
```
94+
95+
### **C++**
96+
97+
```cpp
98+
class Solution {
99+
public:
100+
vector<int> rearrangeArray(vector<int>& nums) {
101+
vector<int> ans(nums.size());
102+
int i = 0, j = 1;
103+
for (int num : nums)
104+
{
105+
if (num > 0)
106+
{
107+
ans[i] = num;
108+
i += 2;
109+
}
110+
else
111+
{
112+
ans[j] = num;
113+
j += 2;
114+
}
115+
}
116+
return ans;
117+
}
118+
};
119+
```
64120
121+
### **Go**
122+
123+
```go
124+
func rearrangeArray(nums []int) []int {
125+
ans := make([]int, len(nums))
126+
i, j := 0, 1
127+
for _, num := range nums {
128+
if num > 0 {
129+
ans[i] = num
130+
i += 2
131+
} else {
132+
ans[j] = num
133+
j += 2
134+
}
135+
}
136+
return ans
137+
}
65138
```
66139

67140
### **TypeScript**

0 commit comments

Comments
 (0)