Skip to content

Instantly share code, notes, and snippets.

@aladine
Created June 14, 2020 23:32
Show Gist options
  • Save aladine/a96e9ac3c4f06f99da140aad954166e6 to your computer and use it in GitHub Desktop.
Save aladine/a96e9ac3c4f06f99da140aad954166e6 to your computer and use it in GitHub Desktop.
Dutch National Flag Problem
class Solution {
fun swapColor(nums: IntArray, k: Int, l: Int) {
val tmp = nums[k]
nums[k] = nums[l]
nums[l] = tmp
}
fun sortColors(nums: IntArray) {
var l = 0
var m = 0
var h = nums.size - 1
while (m <= h) {
when (nums[m]) {
0 -> swapColor(nums, m++, l++)
1 -> m++
2 -> swapColor(nums, m, h--)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment