Skip to content

Commit 54139a7

Browse files
committed
feat: add solutions to lc problem: No.0075. Sort Colors
1 parent 2ed8f5a commit 54139a7

File tree

6 files changed

+188
-49
lines changed

6 files changed

+188
-49
lines changed

solution/0000-0099/0075.Sort Colors/README.md

+77-1
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,91 @@
7474
<!-- 这里可写当前语言的特殊实现逻辑 -->
7575

7676
```python
77-
77+
class Solution:
78+
def sortColors(self, nums: List[int]) -> None:
79+
"""
80+
Do not return anything, modify nums in-place instead.
81+
"""
82+
i, j = -1, len(nums)
83+
cur = 0
84+
while cur < j:
85+
if nums[cur] == 0:
86+
i += 1
87+
nums[cur], nums[i] = nums[i], nums[cur]
88+
cur += 1
89+
elif nums[cur] == 1:
90+
cur += 1
91+
else:
92+
j -= 1
93+
nums[cur], nums[j] = nums[j], nums[cur]
7894
```
7995

8096
### **Java**
8197

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

84100
```java
101+
class Solution {
102+
public void sortColors(int[] nums) {
103+
int i = -1, j = nums.length;
104+
int cur = 0;
105+
while (cur < j) {
106+
if (nums[cur] == 0) {
107+
swap(nums, cur++, ++i);
108+
} else if (nums[cur] == 1) {
109+
++cur;
110+
} else {
111+
swap(nums, cur, --j);
112+
}
113+
}
114+
}
115+
116+
private void swap(int[] nums, int i, int j) {
117+
int t = nums[i];
118+
nums[i] = nums[j];
119+
nums[j] = t;
120+
}
121+
}
122+
```
123+
124+
### **C++**
125+
126+
```cpp
127+
class Solution {
128+
public:
129+
void sortColors(vector<int>& nums) {
130+
int i = -1, j = nums.size(), cur = 0;
131+
while (cur < j) {
132+
if (nums[cur] == 0) {
133+
swap(nums[++i], nums[cur++]);
134+
} else if (nums[cur] == 1) {
135+
++cur;
136+
} else {
137+
swap(nums[cur], nums[--j]);
138+
}
139+
}
140+
}
141+
};
142+
```
85143
144+
### **Go**
145+
146+
```go
147+
func sortColors(nums []int) {
148+
i, j, cur := -1, len(nums), 0
149+
for cur < j {
150+
if nums[cur] == 0 {
151+
i++
152+
nums[cur], nums[i] = nums[i], nums[cur]
153+
cur++
154+
} else if nums[cur] == 1 {
155+
cur++
156+
} else {
157+
j--
158+
nums[cur], nums[j] = nums[j], nums[cur]
159+
}
160+
}
161+
}
86162
```
87163

88164
### **...**

solution/0000-0099/0075.Sort Colors/README_EN.md

+77-1
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,89 @@
4747
### **Python3**
4848

4949
```python
50-
50+
class Solution:
51+
def sortColors(self, nums: List[int]) -> None:
52+
"""
53+
Do not return anything, modify nums in-place instead.
54+
"""
55+
i, j = -1, len(nums)
56+
cur = 0
57+
while cur < j:
58+
if nums[cur] == 0:
59+
i += 1
60+
nums[cur], nums[i] = nums[i], nums[cur]
61+
cur += 1
62+
elif nums[cur] == 1:
63+
cur += 1
64+
else:
65+
j -= 1
66+
nums[cur], nums[j] = nums[j], nums[cur]
5167
```
5268

5369
### **Java**
5470

5571
```java
72+
class Solution {
73+
public void sortColors(int[] nums) {
74+
int i = -1, j = nums.length;
75+
int cur = 0;
76+
while (cur < j) {
77+
if (nums[cur] == 0) {
78+
swap(nums, cur++, ++i);
79+
} else if (nums[cur] == 1) {
80+
++cur;
81+
} else {
82+
swap(nums, cur, --j);
83+
}
84+
}
85+
}
86+
87+
private void swap(int[] nums, int i, int j) {
88+
int t = nums[i];
89+
nums[i] = nums[j];
90+
nums[j] = t;
91+
}
92+
}
93+
```
94+
95+
### **C++**
96+
97+
```cpp
98+
class Solution {
99+
public:
100+
void sortColors(vector<int>& nums) {
101+
int i = -1, j = nums.size(), cur = 0;
102+
while (cur < j) {
103+
if (nums[cur] == 0) {
104+
swap(nums[++i], nums[cur++]);
105+
} else if (nums[cur] == 1) {
106+
++cur;
107+
} else {
108+
swap(nums[cur], nums[--j]);
109+
}
110+
}
111+
}
112+
};
113+
```
56114
115+
### **Go**
116+
117+
```go
118+
func sortColors(nums []int) {
119+
i, j, cur := -1, len(nums), 0
120+
for cur < j {
121+
if nums[cur] == 0 {
122+
i++
123+
nums[cur], nums[i] = nums[i], nums[cur]
124+
cur++
125+
} else if nums[cur] == 1 {
126+
cur++
127+
} else {
128+
j--
129+
nums[cur], nums[j] = nums[j], nums[cur]
130+
}
131+
}
132+
}
57133
```
58134

59135
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
class Solution {
22
public:
33
void sortColors(vector<int>& nums) {
4-
if(nums.empty())return ;
5-
6-
int count[3] = {0};
7-
size_t len = nums.size();
8-
9-
for(int i = 0;i<len;i++){
10-
count[nums[i]]++;
11-
}
12-
int index = 0;
13-
for(int i = 0;i<3;i++){
14-
while(count[i] != 0){
15-
nums[index++] = i;
16-
count[i]--;
4+
int i = -1, j = nums.size(), cur = 0;
5+
while (cur < j) {
6+
if (nums[cur] == 0) {
7+
swap(nums[++i], nums[cur++]);
8+
} else if (nums[cur] == 1) {
9+
++cur;
10+
} else {
11+
swap(nums[cur], nums[--j]);
1712
}
18-
}
13+
}
1914
}
20-
};
15+
};
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
1-
/*
2-
* @lc app=leetcode.cn id=75 lang=golang
3-
* 87/87 cases passed (4 ms), memory usage 2.4 MB
4-
*/
51
func sortColors(nums []int) {
6-
finish0 := -1 // 元素1结束的位置
7-
start2 := len(nums) // 元素2开始的位置
8-
for i := 0; i < start2; {
9-
if nums[i] == 0 {
10-
finish0++
11-
nums[i], nums[finish0] = nums[finish0], 0
2+
i, j, cur := -1, len(nums), 0
3+
for cur < j {
4+
if nums[cur] == 0 {
125
i++
13-
} else if nums[i] == 2 {
14-
start2--
15-
nums[i], nums[start2] = nums[start2], 2
6+
nums[cur], nums[i] = nums[i], nums[cur]
7+
cur++
8+
} else if nums[cur] == 1 {
9+
cur++
1610
} else {
17-
i++
11+
j--
12+
nums[cur], nums[j] = nums[j], nums[cur]
1813
}
1914
}
20-
}
15+
}

solution/0000-0099/0075.Sort Colors/Solution.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
class Solution {
22
public void sortColors(int[] nums) {
3-
int p = -1;
4-
int q = nums.length;
3+
int i = -1, j = nums.length;
54
int cur = 0;
6-
while (cur < q) {
5+
while (cur < j) {
76
if (nums[cur] == 0) {
8-
swap(nums, cur++, ++p);
7+
swap(nums, cur++, ++i);
98
} else if (nums[cur] == 1) {
109
++cur;
1110
} else {
12-
swap(nums, --q, cur);
11+
swap(nums, cur, --j);
1312
}
1413
}
1514
}
16-
15+
1716
private void swap(int[] nums, int i, int j) {
1817
int t = nums[i];
1918
nums[i] = nums[j];
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
class Solution:
2-
def sortColors(self, nums):
2+
def sortColors(self, nums: List[int]) -> None:
33
"""
4-
:type nums: List[int]
5-
:rtype: void Do not return anything, modify nums in-place instead.
4+
Do not return anything, modify nums in-place instead.
65
"""
7-
p=-1
8-
q=len(nums)
9-
cur=0
10-
while cur < q:
6+
i, j = -1, len(nums)
7+
cur = 0
8+
while cur < j:
119
if nums[cur] == 0:
12-
nums[cur],nums[p+1]=nums[p+1],nums[cur]
10+
i += 1
11+
nums[cur], nums[i] = nums[i], nums[cur]
1312
cur += 1
14-
p += 1
1513
elif nums[cur] == 1:
1614
cur += 1
1715
else:
18-
nums[cur],nums[q-1]=nums[q-1],nums[cur]
19-
q -= 1
16+
j -= 1
17+
nums[cur], nums[j] = nums[j], nums[cur]

0 commit comments

Comments
 (0)