题意: 在一个无序的数组中第k大的数是多少?思路: 按照快排的思路,如果每次分成两段后,设为L和R。如果R>=k ,则答案在右边集合,否则在左边集合。 这里用了3位取中法。注意快排别给写死循环了。 1 class Solution { 2 public: 3 int findKthLa...
分类:
编程语言 时间:
2015-11-17 23:15:19
阅读次数:
165
一道比较经典的数据结构题。可以用多种方式来做。一,分桶法(平方分解)。根据数字x的大小和区间内不大于x的数字数量cnt的单调性,可知第k大数kth对应的cnt应该满足cnt≥k,且kth是满足条件的最小的一个,可以二分下界。关键在于高效找出cnt,对于每个完整的桶,排序以后二分,不完整的桶就直接暴力...
分类:
其他好文 时间:
2015-11-07 16:07:44
阅读次数:
315
题意: 寻找一棵BST中的第k小的数。思路: 递归比较方便。 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * ...
分类:
其他好文 时间:
2015-11-04 21:04:21
阅读次数:
296
题目:
Given a binary search tree, write a function kthSmallest to find the kth
smallest element in it.
Note:
You may assume k is always valid, 1 ≤ k ≤ BST's total elements.
Follow up:
Wh...
分类:
其他好文 时间:
2015-10-30 10:52:12
阅读次数:
170
原题链接在这里:https://leetcode.com/problems/kth-largest-element-in-an-array/采用quickSelect 算法,找第k大等于找第num.length + 1 - k小,这里k是从0开始所以就是 找num.length-k小。findK就是...
分类:
其他好文 时间:
2015-10-29 06:08:35
阅读次数:
155
题意:给你一段数字,每次给你一段区间,求这段区间上第K大的数#include using namespace std;const int MAXN=100010;int tree[20][MAXN];//表示每层每个位置的值int sorted[MAXN];//已经排序好的数int toleft[2...
分类:
其他好文 时间:
2015-10-27 23:55:35
阅读次数:
226
Given a binary search tree, write a functionkthSmallestto find thekth smallest element in it.Note:You may assume k is always valid, 1 ≤ k ≤ BST's tota...
分类:
其他好文 时间:
2015-10-25 13:36:39
阅读次数:
131
Find thekth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.For exampl...
分类:
其他好文 时间:
2015-10-21 22:33:55
阅读次数:
212
原题链接在这里:http://www.lintcode.com/en/problem/kth-largest-element/#采用的quickSelect方法,取出pivot, 比pivot 小的都放在左边,比pivot大的都放在右边,若是pivot左边包括pivot的个数恰巧等于k, 就返回pi...
分类:
其他好文 时间:
2015-10-17 07:01:06
阅读次数:
294
QuestionGiven a binary search tree, write a functionkthSmallestto find thekth smallest element in it.Note:You may assume k is always valid, 1 ≤ k ≤ BS...
分类:
其他好文 时间:
2015-10-10 09:05:45
阅读次数:
274