Leetcode Golang 75. Sort Colors.go

2019-04-12 10:59:17 浏览数 (1)

版权声明:原创勿转 https://cloud.tencent.com/developer/article/1412869

思路

暴力循环

code

代码语言:javascript复制
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--
			}

		}
	}
}

0 人点赞