Created
June 14, 2020 23:32
-
-
Save aladine/a96e9ac3c4f06f99da140aad954166e6 to your computer and use it in GitHub Desktop.
Dutch National Flag Problem
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 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