剑指offer
最小的K个数
相似的企业真题
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 64M,其他语言128M
热度指数:962209
本题知识点:
堆
排序
分治
算法知识视频讲解
校招时部分企业笔试将禁止编程题跳出页面,为提前适应,练习时请使用在线自测,而非本地IDE。
题目描述
给定一个数组,找出其中最小的K个数。例如数组元素是4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4。如果K>数组的长度,那么返回一个空的数组
示例1
输入
复制
[4,5,1,6,2,7,3,8],4
[4,5,1,6,2,7,3,8],4
返回值
复制
[1,2,3,4]
[1,2,3,4]
上一题
下一题
登录
/
注册
我的提交
编辑器加载中...
import java.util.ArrayList; public class Solution { public ArrayList
GetLeastNumbers_Solution(int [] input, int k) { } }
class Solution { public: vector
GetLeastNumbers_Solution(vector
input, int k) { } };
# -*- coding:utf-8 -*- class Solution: def GetLeastNumbers_Solution(self, tinput, k): # write code here
using System; using System.Collections.Generic; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param input int整型一维数组 * @param k int整型 * @return int整型一维数组 */ public List
GetLeastNumbers_Solution (List
input, int k) { // write code here } }
function GetLeastNumbers_Solution(input, k) { // write code here } module.exports = { GetLeastNumbers_Solution : GetLeastNumbers_Solution };
function GetLeastNumbers_Solution(input, k) { // write code here }
package main /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param input int整型一维数组 * @param k int整型 * @return int整型一维数组 */ func GetLeastNumbers_Solution( input []int , k int ) []int { // write code here }
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # @param input int整型一维数组 # @param k int整型 # @return int整型一维数组 # class Solution def GetLeastNumbers_Solution(input, k) # write code here end end
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param input int整型一维数组 * @param k int整型 * @return int整型一维数组 */ def GetLeastNumbers_Solution(input: Array[Int],k: Int): Array[Int] = { // write code here } }
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param input int整型一维数组 * @param k int整型 * @return int整型一维数组 */ fun GetLeastNumbers_Solution(input: IntArray,k: Int): IntArray { // write code here } }
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param input int整型一维数组 * @param k int整型 * @return int整型一维数组 */ public int[] GetLeastNumbers_Solution (int[] input, int k) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param input int整型一维数组 * @param k int整型 * @return int整型一维数组 */ export function GetLeastNumbers_Solution(input: number[], k: number): number[] { // write code here }
public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param input int整型一维数组 * @param k int整型 * @return int整型一维数组 */ func GetLeastNumbers_Solution ( _ input: [Int], _ k: Int) -> [Int] { // write code here } }
struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param input int整型一维数组 * @param k int整型 * @return int整型一维数组 */ pub fn GetLeastNumbers_Solution(&self, input: Vec
, k: i32) -> Vec
{ // write code here } }
[4,5,1,6,2,7,3,8],4
[1,2,3,4]