-
-
Save terracotta-ko/117468567b85011eee772069b8598927 to your computer and use it in GitHub Desktop.
leetcode 75
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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