LeetCode_215数组中第K个最大元素

在未排序的数组中找到第 k 个最大的元素。请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。

示例 1:

输入: [3,2,1,5,6,4] 和 k = 2
输出: 5

示例 2:

输入: [3,2,3,1,2,4,5,5,6] 和 k = 4
输出: 4

说明:
你可以假设 k 总是有效的,且 1 ≤ k ≤ 数组的长度。


解法1:选择排序

class Solution {
    public int findKthLargest(int[] nums, int k) {
        int length = nums.length;
		int endIndex = k > length / 2 ? (length - k + 1) : k;
		int temp;
		for (int i = 0; i < endIndex; i++) {
			int minIndex = i;
			int maxIndex = length - i - 1;
			for (int j = i; j < length - i; j++) {
				if (nums[j] < nums[minIndex]) {
					minIndex = j;
				}
				if (nums[j] > nums[maxIndex]) {
					maxIndex = j;
				}
			}
			temp = nums[i];
			nums[i] = nums[minIndex];
			nums[minIndex] = temp;
			if (maxIndex == i) {
				maxIndex = minIndex;
			}
			temp = nums[length - i - 1];
			nums[length - i - 1] = nums[maxIndex];
			nums[maxIndex] = temp;
		}
		return nums[length - k];
    }
}

结果:

32 / 32 个通过测试用例
状态:通过
执行用时: 14 ms     19%
内存消耗: 39.2 MB   49%

解法2:冒泡排序

class Solution {
    public int findKthLargest(int[] nums, int k) {
        boolean asc = (nums.length - k + 1) > (nums.length / 2 + nums.length % 2);
		int temp;
		int endIndex = asc ? nums.length - k : k - 1;
		if (asc) {
			for (int i = 0; i < endIndex; i++) {
				boolean flag = false;
				for (int j = 0; j < nums.length - i - 1; j++) {
					if (nums[j] > nums[j + 1]) {
						temp = nums[j + 1];
						nums[j + 1] = nums[j];
						nums[j] = temp;
						flag = true;
					}
				}
				if (!flag) {
					break;
				}
			}
		} else {
			for (int i = 0; i < endIndex; i++) {
				boolean flag = false;
				for (int j = 0; j < nums.length - i - 1; j++) {
					if (nums[j] < nums[j + 1]) {
						temp = nums[j + 1];
						nums[j + 1] = nums[j];
						nums[j] = temp;
						flag = true;
					}
				}
				if (!flag) {
					break;
				}
			}
		}
		return nums[endIndex];
    }
}

结果:

32 / 32 个通过测试用例
状态:通过
执行用时: 211 ms
内存消耗: 39.3 MB

解法3:桶排序

class Solution {
    public int findKthLargest(int[] nums, int k) {
    int maxValue = nums[0];
        int minValue = nums[0];
        for (int num : nums) {
            if (maxValue < num) {
                maxValue = num;
            }
            if (minValue > num) {
                minValue = num;
            }
        }
        int[] result = new int[maxValue - minValue + 1];
        for (int num : nums) {
            result[num - minValue] = result[num - minValue] + 1;
        }
        int total = 0;
        k = nums.length - k + 1;
        for (int i = 0; i < result.length; i++) {
            if (result[i] > 0) {
                total += result[i];
                if (total >= k) {
                    return i + minValue;
                }
            }
        }
        return 0;
    }
}

结果:

32 / 32 个通过测试用例
状态:通过
执行用时: 1 ms       100%
内存消耗: 39.2 MB    57%

解法4:使用HashMap(失败)

class Solution {
    public int findKthLargest(int[] nums, int k) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int num : nums) {
            int temp = 0;
            Integer value = map.get(num);
            if (value != null) {
                temp = value;
            }
            map.put(num, temp + 1);
        }
        int total = 0;
        k = nums.length - k + 1;
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            total += entry.getValue();
            if (total >= k) {
                return entry.getKey();
            }
        }
        return 0;
    }
}

结果:失败

31 / 32 个通过测试用例

解法5:堆排序(同事完成)
在这里插入图片描述
结果:

32 / 32 个通过测试用例
状态:通过
执行用时: 4 ms        
内存消耗: 39.5 MB   

资源:

  1. 常用数据结构+算法
  2. 十大经典排序算法(动图演示)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值