剑指 Offer 40. 最小的k个数

题目: 输入整数数组 arr ,找出其中最小的 k 个数。例如,输入4、5、1、6、2、7、3、8这8个数字,则最小的4个数字是1、2、3、4。

示例 1:

输入:arr = [3,2,1], k = 2
输出:[1,2] 或者 [2,1]

示例 2:

输入:arr = [0,1,2,1], k = 1
输出:[0]

思路:

方法一:使用大根堆,首先将前k个元素依次加入到堆中,再将k后边的元素与堆顶比较,
如果小于堆顶元素,直接弹出堆顶元素,并让该元素入堆,最后打印堆中前k个元素。
方法二:直接使用sort排序,但是不建议这样写,太简单了
方法三:使用快排的思路,最后取前k个,但是我写的快排再leetcode上超时了,感觉堆的思路更简单。
下边这个人的快排思路没超时。
https://leetcode.cn/problems/zui-xiao-de-kge-shu-lcof/solution/jian-zhi-offer-40-zui-xiao-de-k-ge-shu-j-9yze/

自己再练习一下最大的k个数,最后贴了代码

方法一:

class Solution {
public:
	vector<int> getLeastNumbers(vector<int>& arr, int k) {
		if (k == 0 || arr.size() == 0)return {};
		priority_queue<int> st;
		vector<int> result;
		for (int i = 0; i < k; i++) {
			st.push(arr[i]);
		}
		for (int i = k; i < arr.size(); i++) {
			if (arr[i] < st.top()) {
				st.pop();
				st.push(arr[i]);
			}
		}
		//cout << st.size() << endl;
		for (int i = 0; i < k;i++) {
			result.push_back(st.top());
			st.pop();
		}
		//cout << result.size() << endl;
		return result;
	}
};

int main() {
	vector<int> arr = { 3, 2, 1 };
	int k = 2;
	Solution ss;
	vector<int> nums = ss.getLeastNumbers(arr, k);
	for (vector<int>::iterator pos = nums.begin(); pos != nums.end();pos++ ) {
		cout << *pos << " ";
	}

	return 0;
}

方法二:

class Solution {
public:
	vector<int> getLeastNumbers(vector<int>& arr,int k) {
		sort(arr.begin(), arr.end());
		vector<int> result;
		for (int i = 0; i < k; i++) {
			result.push_back(arr[i]);
		}
		return result;
	}
};

int main() {
	vector<int> arr = { 3, 2, 1 };
	int k = 2;
	Solution ss;
	vector<int> nums = ss.getLeastNumbers(arr, k);
	for (vector<int>::iterator pos = nums.begin(); pos != nums.end(); pos++) {
		cout << *pos << " ";
	}

	return 0;
}

方法三:

class Solution {
public:
	vector<int> getLeastNumbers(vector<int>& arr,int k) {
		
		QuickSort(arr,0,arr.size()-1);
		vector<int> res(arr.begin(),arr.begin()+k);
		return res;
	}
	void QuickSort(vector<int>&arr,int left,int right) {
		if (left >= right) {
			return;
		}

		int start = left;
		int end = right;
		int slot = left;
		int key = arr[left];
		while (left < right) {
			while (left<right && arr[right]>=key) {
				right--;
			}
			arr[slot] = arr[right];
			slot = right;
			while (left < right && arr[left] <= key) {
				left++;
			}
			arr[slot] = arr[left];
			slot = left;
		}
		arr[slot] = key;
		QuickSort(arr,start,slot-1);
		QuickSort(arr, slot + 1, end);
	}
};

int main() {
	vector<int> arr = { 3, 2, 1 };
	int k = 2;
	Solution ss;
	vector<int> nums = ss.getLeastNumbers(arr, k);
	for (vector<int>::iterator pos = nums.begin(); pos != nums.end(); pos++) {
		cout << *pos << " ";
	}

	return 0;
}

最大的k个数:

class Solution {
public:
	vector<int> getLeastNumbers(vector<int>& arr, int k) {
		if (k == 0 || arr.size() == 0)return {};
		priority_queue<int> st;
		vector<int> result;
		for (int i = 0; i < arr.size(); i++) {
			st.push(arr[i]);
		}

		for (int i = 0; i < k; i++) {
			result.push_back(st.top());
			st.pop();
		}
		//cout << result.size() << endl;
		return result;
	}
};

int main() {
	vector<int> arr = { 3, 2, 1 };
	int k = 2;
	Solution ss;
	vector<int> nums = ss.getLeastNumbers(arr, k);
	for (vector<int>::iterator pos = nums.begin(); pos != nums.end(); pos++) {
		cout << *pos << " ";
	}

	return 0;
}

你可能感兴趣的:(leetcode,算法,数据结构)