美文网首页leetcode
75. Sort Colors.go

75. Sort Colors.go

作者: AnakinSun | 来源:发表于2019-03-23 13:31 被阅读0次

简单暴力

func swap(nums []int, i, j int) {
   tmp := nums[i]
   nums[i] = nums[j]
   nums[j] = tmp

func sortColors(nums []int) {
   l, i, r := 0, 0, len(nums)-1
   for i <= r {
       if nums[i] == 0 {
           swap(nums, i, l)
           i++
           l++
       } else {
           if nums[i] == 1 {
               i++
           } else {
               swap(nums, i, r)
               r--
           }
       }
   }
}

相关文章

网友评论

    本文标题:75. Sort Colors.go

    本文链接:https://www.haomeiwen.com/subject/ehcsvqtx.html