mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-07 09:54:57 +08:00
clean up redundant code
This commit is contained in:
@ -1,28 +1,16 @@
|
|||||||
package leetcode
|
package leetcode
|
||||||
|
|
||||||
func sortColors(nums []int) {
|
func sortColors(nums []int) {
|
||||||
if len(nums) == 0 {
|
zero, one := 0, 0
|
||||||
return
|
for i, n := range nums {
|
||||||
}
|
nums[i] = 2
|
||||||
|
if n <= 1 {
|
||||||
r := 0
|
nums[one] = 1
|
||||||
w := 0
|
one++
|
||||||
b := 0 // label the end of different colors;
|
}
|
||||||
for _, num := range nums {
|
if n == 0 {
|
||||||
if num == 0 {
|
nums[zero] = 0
|
||||||
nums[b] = 2
|
zero++
|
||||||
b++
|
|
||||||
nums[w] = 1
|
|
||||||
w++
|
|
||||||
nums[r] = 0
|
|
||||||
r++
|
|
||||||
} else if num == 1 {
|
|
||||||
nums[b] = 2
|
|
||||||
b++
|
|
||||||
nums[w] = 1
|
|
||||||
w++
|
|
||||||
} else if num == 2 {
|
|
||||||
b++
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -43,28 +43,16 @@ First, iterate the array counting number of 0's, 1's, and 2's, then overwrite ar
|
|||||||
package leetcode
|
package leetcode
|
||||||
|
|
||||||
func sortColors(nums []int) {
|
func sortColors(nums []int) {
|
||||||
if len(nums) == 0 {
|
zero, one := 0, 0
|
||||||
return
|
for i, n := range nums {
|
||||||
}
|
nums[i] = 2
|
||||||
|
if n <= 1 {
|
||||||
r := 0
|
nums[one] = 1
|
||||||
w := 0
|
one++
|
||||||
b := 0 // label the end of different colors;
|
}
|
||||||
for _, num := range nums {
|
if n == 0 {
|
||||||
if num == 0 {
|
nums[zero] = 0
|
||||||
nums[b] = 2
|
zero++
|
||||||
b++
|
|
||||||
nums[w] = 1
|
|
||||||
w++
|
|
||||||
nums[r] = 0
|
|
||||||
r++
|
|
||||||
} else if num == 1 {
|
|
||||||
nums[b] = 2
|
|
||||||
b++
|
|
||||||
nums[w] = 1
|
|
||||||
w++
|
|
||||||
} else if num == 2 {
|
|
||||||
b++
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user