题解 | #最小的K个数#

最小的K个数

http://www.nowcoder.com/practice/6a296eb82cf844ca8539b57c23e6e9bf

package main

/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 * 
 * @param input int整型一维数组 
 * @param k int整型 
 * @return int整型一维数组
*/
func GetLeastNumbers_Solution( input []int ,  k int ) []int {
    // write code here
    sort(input, 0, len(input) - 1)
    
    return input[0:k]

    
}


func sort(input []int, low, high int) {
	if low >= high {
		return
	}

	left := low
	right := high
	target := input[left]
	for  left < right && right < len(input) {
		for left < right && input[right] >= target {
			right--
		}
		for left < right && input[left] <= target  {
			left++
		}

		if left < right {
			tmp := input[left]
			input[left]  = input[right]
			input[right] = tmp
		}
	}
	tmp := input[left]
	input[left]  = input[low]
	input[low] = tmp
	sort(input, low, left - 1)
	sort(input, left + 1, high)
}
全部评论

相关推荐

牛客83700679...:简历抄别人的,然后再投,有反馈就是简历不行,没反馈就是学历不行,多投多改只要技术不差机会总会有的
点赞 评论 收藏
分享
程序员小白条:找的太晚,别人都是大三实习,然后大四秋招春招的,你大四下了才去实习,晚1年
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务