Skip to content

Commit 4a1a33b

Browse files
committed
feat: add solutions to lc problems
* No.2164.Sort Even and Odd Indices Independently * No.2165.Smallest Value of the Rearranged Number * No.2167.Minimum Time to Remove All Cars Containing Illegal Goods
1 parent 670cc25 commit 4a1a33b

File tree

18 files changed

+900
-6
lines changed

18 files changed

+900
-6
lines changed

solution/2100-2199/2164.Sort Even and Odd Indices Independently/README.md

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,97 @@
6868
<!-- 这里可写当前语言的特殊实现逻辑 -->
6969

7070
```python
71-
71+
class Solution:
72+
def sortEvenOdd(self, nums: List[int]) -> List[int]:
73+
a = sorted(nums[::2])
74+
b = sorted(nums[1::2], reverse=True)
75+
nums[::2] = a
76+
nums[1::2] = b
77+
return nums
7278
```
7379

7480
### **Java**
7581

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

7884
```java
85+
class Solution {
86+
public int[] sortEvenOdd(int[] nums) {
87+
int n = nums.length;
88+
int[] a = new int[(n + 1) >> 1];
89+
int[] b = new int[n >> 1];
90+
for (int i = 0, j = 0; j < n >> 1; i += 2, ++j) {
91+
a[j] = nums[i];
92+
b[j] = nums[i + 1];
93+
}
94+
if (n % 2 == 1) {
95+
a[a.length - 1] = nums[n - 1];
96+
}
97+
Arrays.sort(a);
98+
Arrays.sort(b);
99+
int[] ans = new int[n];
100+
for (int i = 0, j = 0; j < a.length; i += 2, ++j) {
101+
ans[i] = a[j];
102+
}
103+
for (int i = 1, j = b.length - 1; j >= 0; i += 2, --j) {
104+
ans[i] = b[j];
105+
}
106+
return ans;
107+
}
108+
}
109+
```
110+
111+
### **C++**
112+
113+
```cpp
114+
class Solution {
115+
public:
116+
vector<int> sortEvenOdd(vector<int>& nums) {
117+
int n = nums.size();
118+
vector<int> a;
119+
vector<int> b;
120+
for (int i = 0; i < n; ++i)
121+
{
122+
if (i % 2 == 0) a.push_back(nums[i]);
123+
else b.push_back(nums[i]);
124+
}
125+
sort(a.begin(), a.end());
126+
sort(b.begin(), b.end(), greater<int>());
127+
vector<int> ans(n);
128+
for (int i = 0, j = 0; j < a.size(); i += 2, ++j) ans[i] = a[j];
129+
for (int i = 1, j = 0; j < b.size(); i += 2, ++j) ans[i] = b[j];
130+
return ans;
131+
}
132+
};
133+
```
79134
135+
### **Go**
136+
137+
```go
138+
func sortEvenOdd(nums []int) []int {
139+
n := len(nums)
140+
var a []int
141+
var b []int
142+
for i, v := range nums {
143+
if i%2 == 0 {
144+
a = append(a, v)
145+
} else {
146+
b = append(b, v)
147+
}
148+
}
149+
ans := make([]int, n)
150+
sort.Ints(a)
151+
sort.Slice(b, func(i, j int) bool {
152+
return b[i] > b[j]
153+
})
154+
for i, j := 0, 0; j < len(a); i, j = i+2, j+1 {
155+
ans[i] = a[j]
156+
}
157+
for i, j := 1, 0; j < len(b); i, j = i+2, j+1 {
158+
ans[i] = b[j]
159+
}
160+
return ans
161+
}
80162
```
81163

82164
### **TypeScript**

solution/2100-2199/2164.Sort Even and Odd Indices Independently/README_EN.md

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,95 @@ The resultant array formed is [2,1], which is the same as the initial array.
6060
### **Python3**
6161

6262
```python
63-
63+
class Solution:
64+
def sortEvenOdd(self, nums: List[int]) -> List[int]:
65+
a = sorted(nums[::2])
66+
b = sorted(nums[1::2], reverse=True)
67+
nums[::2] = a
68+
nums[1::2] = b
69+
return nums
6470
```
6571

6672
### **Java**
6773

6874
```java
75+
class Solution {
76+
public int[] sortEvenOdd(int[] nums) {
77+
int n = nums.length;
78+
int[] a = new int[(n + 1) >> 1];
79+
int[] b = new int[n >> 1];
80+
for (int i = 0, j = 0; j < n >> 1; i += 2, ++j) {
81+
a[j] = nums[i];
82+
b[j] = nums[i + 1];
83+
}
84+
if (n % 2 == 1) {
85+
a[a.length - 1] = nums[n - 1];
86+
}
87+
Arrays.sort(a);
88+
Arrays.sort(b);
89+
int[] ans = new int[n];
90+
for (int i = 0, j = 0; j < a.length; i += 2, ++j) {
91+
ans[i] = a[j];
92+
}
93+
for (int i = 1, j = b.length - 1; j >= 0; i += 2, --j) {
94+
ans[i] = b[j];
95+
}
96+
return ans;
97+
}
98+
}
99+
```
100+
101+
### **C++**
102+
103+
```cpp
104+
class Solution {
105+
public:
106+
vector<int> sortEvenOdd(vector<int>& nums) {
107+
int n = nums.size();
108+
vector<int> a;
109+
vector<int> b;
110+
for (int i = 0; i < n; ++i)
111+
{
112+
if (i % 2 == 0) a.push_back(nums[i]);
113+
else b.push_back(nums[i]);
114+
}
115+
sort(a.begin(), a.end());
116+
sort(b.begin(), b.end(), greater<int>());
117+
vector<int> ans(n);
118+
for (int i = 0, j = 0; j < a.size(); i += 2, ++j) ans[i] = a[j];
119+
for (int i = 1, j = 0; j < b.size(); i += 2, ++j) ans[i] = b[j];
120+
return ans;
121+
}
122+
};
123+
```
69124
125+
### **Go**
126+
127+
```go
128+
func sortEvenOdd(nums []int) []int {
129+
n := len(nums)
130+
var a []int
131+
var b []int
132+
for i, v := range nums {
133+
if i%2 == 0 {
134+
a = append(a, v)
135+
} else {
136+
b = append(b, v)
137+
}
138+
}
139+
ans := make([]int, n)
140+
sort.Ints(a)
141+
sort.Slice(b, func(i, j int) bool {
142+
return b[i] > b[j]
143+
})
144+
for i, j := 0, 0; j < len(a); i, j = i+2, j+1 {
145+
ans[i] = a[j]
146+
}
147+
for i, j := 1, 0; j < len(b); i, j = i+2, j+1 {
148+
ans[i] = b[j]
149+
}
150+
return ans
151+
}
70152
```
71153

72154
### **TypeScript**
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
vector<int> sortEvenOdd(vector<int>& nums) {
4+
int n = nums.size();
5+
vector<int> a;
6+
vector<int> b;
7+
for (int i = 0; i < n; ++i)
8+
{
9+
if (i % 2 == 0) a.push_back(nums[i]);
10+
else b.push_back(nums[i]);
11+
}
12+
sort(a.begin(), a.end());
13+
sort(b.begin(), b.end(), greater<int>());
14+
vector<int> ans(n);
15+
for (int i = 0, j = 0; j < a.size(); i += 2, ++j) ans[i] = a[j];
16+
for (int i = 1, j = 0; j < b.size(); i += 2, ++j) ans[i] = b[j];
17+
return ans;
18+
}
19+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
func sortEvenOdd(nums []int) []int {
2+
n := len(nums)
3+
var a []int
4+
var b []int
5+
for i, v := range nums {
6+
if i%2 == 0 {
7+
a = append(a, v)
8+
} else {
9+
b = append(b, v)
10+
}
11+
}
12+
ans := make([]int, n)
13+
sort.Ints(a)
14+
sort.Slice(b, func(i, j int) bool {
15+
return b[i] > b[j]
16+
})
17+
for i, j := 0, 0; j < len(a); i, j = i+2, j+1 {
18+
ans[i] = a[j]
19+
}
20+
for i, j := 1, 0; j < len(b); i, j = i+2, j+1 {
21+
ans[i] = b[j]
22+
}
23+
return ans
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public int[] sortEvenOdd(int[] nums) {
3+
int n = nums.length;
4+
int[] a = new int[(n + 1) >> 1];
5+
int[] b = new int[n >> 1];
6+
for (int i = 0, j = 0; j < n >> 1; i += 2, ++j) {
7+
a[j] = nums[i];
8+
b[j] = nums[i + 1];
9+
}
10+
if (n % 2 == 1) {
11+
a[a.length - 1] = nums[n - 1];
12+
}
13+
Arrays.sort(a);
14+
Arrays.sort(b);
15+
int[] ans = new int[n];
16+
for (int i = 0, j = 0; j < a.length; i += 2, ++j) {
17+
ans[i] = a[j];
18+
}
19+
for (int i = 1, j = b.length - 1; j >= 0; i += 2, --j) {
20+
ans[i] = b[j];
21+
}
22+
return ans;
23+
}
24+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def sortEvenOdd(self, nums: List[int]) -> List[int]:
3+
a = sorted(nums[::2])
4+
b = sorted(nums[1::2], reverse=True)
5+
nums[::2] = a
6+
nums[1::2] = b
7+
return nums

0 commit comments

Comments
 (0)