LeetCode Kth Largest Element in an Array

原创 2015年07月09日 15:50:53

Description:

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.

Ensure that numbers within the set are sorted in ascending order.

Solution:

似乎一个排序就出来了,可能是数据卡的不够严。

import java.util.*;

public class Solution {

	public int findKthLargest(int[] nums, int k) {
		Arrays.sort(nums);
		return nums[nums.length - k];
	}
}

网上找到了一个更加靠谱的解决方案,是快速选择法(Quick Select)Quick Select

其实很类似快排。

import java.util.*;

public class Solution {

	public int findKthLargest(int[] nums, int k) {
		Arrays.sort(nums);
		return nums[nums.length - k];
	}

	int findKth(int nums[], int start, int end, int k) {
		int mark = partition(nums, start, end);
		if (mark == k)
			return nums[mark];
		else if (mark < k)
			return findKth(nums, mark + 1, end, k - mark);
		else
			return findKth(nums, start, mark, k);

	}

	int partition(int[] nums, int start, int end) {
		int mark = nums[start];
		int tot = start + 1;

		for (int i = start + 1; i <= end; i++) {
			if (nums[i] <= mark) {
				swap(nums, tot++, i);
			}
		}

		swap(nums, start, tot);
		return tot;
	}

	void swap(int[] nums, int i, int j) {
		int c = nums[i];
		nums[i] = nums[j];
		nums[j] = c;
	}
}


【LeetCode-面试算法经典-Java实现】【215-Kth Largest Element in an Array(数组中第K大的数)】

【215-Kth Largest Element in an Array(数组中第K大的数)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】代码下载【https://github...

《leetCode》:Kth Largest Element in an Array

题目Find the kth largest element in an unsorted array. Note that it is the kth largest element in the ...

LeetCode(215) Kth Largest Element in an Array

题目Find the kth largest element in an unsorted array. Note that it is the kth largest element in the ...
  • fly_yr
  • fly_yr
  • 2015年12月08日 13:54
  • 852

LeetCode-215.Kth Largest Element in an Array

https://leetcode.com/problems/kth-largest-element-in-an-array/ Find the kth largest element in an ...

【leetcode】215. Kth Largest Element in an Array

【leetcode】215. Kth Largest Element in an ArrayFind the kth largest element in an unsorted array. Not...

Leetcode 215. Kth Largest Element in an Array[medium]

题目: Find the kth largest element in an unsorted array. Note that it is the kth largest element in t...

LeetCode 215. Kth Largest Element in an Array

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so...

LeetCode6 Kth Largest Element in an Array

1、题目描述 Find the kth largest element in an unsorted array. Note that it is the kth largest element i...

Leetcode 215 Kth Largest Element in an Array 数组中第k大的元素

在一个无须数组中找出第k大的元素。

[LeetCode 215]Kth Largest Element in an Array (分治法/快速排序)

215. Kth Largest Element in an ArrayFind the kth largest element in an unsorted array. Note that it ...
  • zlasd
  • zlasd
  • 2016年09月14日 21:07
  • 270
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode Kth Largest Element in an Array
举报原因:
原因补充:

(最多只允许输入30个字)