Skip to content

Commit

Permalink
Added 75-Sort-Colors.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
a93a committed Nov 22, 2022
1 parent c17e82c commit e52d6e7
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions kotlin/75-Sort-Colors.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Solution {
fun sortColors(nums: IntArray): Unit {

var low = 0
var high = nums.size-1
var pointer = 0

while(pointer <= high){
when(nums[pointer]){
0 -> {
swap(low,pointer,nums)
low++
pointer++
}
1 -> {
pointer++
}
2 -> {
swap(high,pointer,nums)
high--
}
}
}
}

private fun swap(i: Int, j: Int, nums: IntArray){
val temp = nums[i]
nums[i] = nums[j]
nums[j] = temp
}
}

0 comments on commit e52d6e7

Please sign in to comment.