BM46. 最小的K个数
描述
示例1
输入:
[4,5,1,6,2,7,3,8],4
输出:
[1,2,3,4]
说明:
返回最小的4个数即可,返回[1,3,2,4]也可以示例2
输入:
[1],0
输出:
[]
示例3
输入:
[0,1,2,1,2],3
输出:
[0,1,1]
Rust 解法, 执行用时: 2ms, 内存消耗: 220KB, 提交时间: 2021-03-20
struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param input int整型一维数组 * @param k int整型 * @return int整型一维数组 */ pub fn GetLeastNumbers_Solution(&self, input: Vec<i32>, k: i32) -> Vec<i32> { // write code here let k = k as usize; if input.len() < k || k == 0 { return Vec::new(); } let mut heap = input; Self::heap_init(&mut heap, k as usize); for i in k .. heap.len() { let v = heap[i]; Self::heap_insert(&mut heap, v, k); } heap.truncate(k); let mut k = k - 1; while k > 0 { heap.swap(0, k); Self::heap_rewind(&mut heap, k); k -= 1; } heap } fn heap_init(heap: &mut Vec<i32>, k: usize) { for i in 0 .. k { let mut j = i; while j > 0 { let k = (j - 1) / 2; if heap[j] > heap[k] { heap.swap(j, k); j = k; } else { break; } } } } fn heap_rewind(heap: &mut Vec<i32>, k: usize) { let mut i = 0; while i < k { let mut j = i * 2 + 1; if j >= k { break; } if j + 1 < k && heap[j] < heap[j + 1] { j = j + 1; } if heap[i] < heap[j] { heap.swap(i, j); i = j; continue; } break; } } fn heap_insert(heap: &mut Vec<i32>, v: i32, k: usize) { if v >= heap[0] { return; } heap[0] = v; Self::heap_rewind(heap, k); } }
Rust 解法, 执行用时: 2ms, 内存消耗: 220KB, 提交时间: 2021-03-15
struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param input int整型一维数组 * @param k int整型 * @return int整型一维数组 */ pub fn GetLeastNumbers_Solution(&self, input: Vec<i32>, k: i32) -> Vec<i32> { if k as usize > input.len() { return vec![]; } use std::collections::BinaryHeap; let mut heap = BinaryHeap::new(); for v in input { if heap.len() < k as usize { heap.push(v); } else { if v < heap.peek().cloned().unwrap_or_default() { heap.pop(); heap.push(v); } } } // write code here heap.into_sorted_vec() } }
Rust 解法, 执行用时: 2ms, 内存消耗: 260KB, 提交时间: 2021-05-02
use std::collections::BinaryHeap; use std::cmp::Reverse; struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param input int整型一维数组 * @param k int整型 * @return int整型一维数组 */ fn heapfiy(&self, input: Vec<i32>) { } pub fn GetLeastNumbers_Solution(&self, input: Vec<i32>, k: i32) -> Vec<i32> { // write code here let mut heap = BinaryHeap::new(); let mut result = vec![]; if k > input.len() as i32 { return result; } // build a k elements min heap for i in 0..input.len() { heap.push(Reverse(input[i])); } // insert the others for i in 0..k { if let Some(Reverse(v)) = heap.pop() { result.push(v); } } // return the heap return result; } }
Rust 解法, 执行用时: 2ms, 内存消耗: 340KB, 提交时间: 2021-05-11
struct Solution{} impl Solution { fn new() -> Self { Solution{} } pub fn GetLeastNumbers_Solution(&self, mut input: Vec<i32>, k: i32) -> Vec<i32> { input.sort(); if k as usize>input.len(){ return vec![]; } input[..k as usize].to_vec() } }
C++ 解法, 执行用时: 2ms, 内存消耗: 352KB, 提交时间: 2021-05-20
class Solution { public: vector<int> GetLeastNumbers_Solution(vector<int> input, int k) { vector<int> res; if(k>input.size()) return res; multiset<int> s; for(auto i:input) { s.insert(i); } int i=0; for(set<int>::iterator it=s.begin();it!=s.end()&&i<k;it++,i++) { res.push_back(*it); } return res; } };