Skip to content

Instantly share code, notes, and snippets.

@terracotta-ko
Last active October 13, 2021 04:09
Show Gist options
  • Save terracotta-ko/117468567b85011eee772069b8598927 to your computer and use it in GitHub Desktop.
Save terracotta-ko/117468567b85011eee772069b8598927 to your computer and use it in GitHub Desktop.
leetcode 75
class Solution {
fun sortColors(nums: IntArray): Unit {
var index = 0
var redIdx = 0
var blueIdx = nums.lastIndex
while(index <= blueIdx) {
when {
nums[index] == 0 -> {
nums[redIdx] = nums[index].also {nums[index] = nums[redIdx]}
++redIdx
++index
}
nums[index] == 2 -> {
nums[blueIdx] = nums[index].also {nums[index] = nums[blueIdx]}
--blueIdx
}
else -> ++index
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment