29.最小的K个数
题目描述:
输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。
思路分析:
利用快速排序的partition函数,partition函数会在数组中找到一个Key值,然后将小于Key的放到它前面,大于Key的放到它后面,我们只需要判断Key的下标t是否等于K,如果等于K那么返回数组的前K个数,如果小于Key那么我们缩小范围,将数组的low更新为t+1,再进行查找,如果大于K,那么将high更新为t-1。直达K等于t。时间复杂度为O(n).
代码:
import java.util.*;
public class Solution {
public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) {
ArrayList<Integer>res=new ArrayList<>();
if(input==null||k>input.length)
return res;
int low=0;
int high=input.length-1;
while(low<high){
int t=partition(low,high,input);
if(t>k){
high=t-1;
}else if(t<k){
low=t+1;
}else{
break;
}
}
for(int i=0;i<k;i++){
res.add(input[i]);
}
return res;
}
public int partition(int low ,int high,int []input){
int key=input[low];
while(low<high){
while(low<high&&input[high]>=key){
high--;
}
input[low]=input[high];
while(low<high&&input[low]<=key){
low++;
}
input[high]=input[low];
}
input[low]=key;
return low;
}
}
28.连续子数组的最大和
29.最小的K个数的更多相关文章
- 剑指Offer 29. 最小的K个数 (其他)
题目描述 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4. 题目地址 https://www.nowcoder.com/prac ...
- 29最小的K个数
题目描述 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. 思路: 利用快速排序的partion 来解决 如果基于数字的第 ...
- [剑指Offer] 29.最小的K个数
[思路1]全排序(快排)之后取出前K个数.O(K+nlogn) class Solution { public: vector<int> GetLeastNumbers_Solution( ...
- 剑指offer——python【第29题】最小的K个数
题目描述 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. 思路 先排序后取数,排序可以用冒泡,插入,选择,快排,二分法等等, ...
- 剑指offer(29)最小的K个数
题目描述 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. 题目分析 这题有两种方法来做. 第一种就是基于partition的 ...
- 《剑指offer》— JavaScript(29)最小的K个数
最小的K个数 题目描述 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4. 思路一 使用JavaScript的Array对象的so ...
- 29、最小的K个数
一.题目 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. 二.解法 import java.util.ArrayList; ...
- 【剑指Offer】29、最小的K个数
题目描述: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4. 解题思路: 本题最直观的解法就是将输入的n个整数排 ...
- 剑指29:最小的k个数
题目描述 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4. class Solution {public: vector& ...
随机推荐
- java基础之JDBC一:概述及步骤详解
1. JDBC的简介 概述: 就是Java用来操作不同数据库(DBMS)的类库(技术), 本质就是一些类和接口. /* 类: DriverManager 接口: Driver, Connection, ...
- mongo 修改器 $inc/$set/$unset/$pop/$push/$pull/$addToSet
mongo $inc 可以对集合里面的某些值是数字的增减.看代码 $set 可以进行修改,并且不存在的时候默认添加. 同时还能该变数据的类型. 还可以该变内嵌元素的值 用.调用 $unset 删除 ...
- spring框架 使用 JdbcTemplate 对数据进行增删改查
user=LF password=LF jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl driverClass=oracle.jdbc.driver.Ora ...
- tensorflow 中 feed的用法
Feed 上述示例在计算图中引入了 tensor, 以常量或变量的形式存储. TensorFlow 还提供了 feed 机制, 该机制 可以临时替代图中的任意操作中的 tensor 可以对图中任何操作 ...
- Shiro——概述
Apache Shiro 是 Java 的一个安全(权限)框架. Shiro 可以非常容易的开发出足够好的应用,其不仅可以用在JavaSE 环境,也可以用在 JavaEE 环境. Shiro 可以完成 ...
- C++中的Trivial 、POD、non-POD和Standard Layout概念
POD types non-POD types Standard Layout types A Formal Definition Informally, a standard layout clas ...
- Jackson Streaming API to read and write JSON
Jackson supports read and write JSON via high-performance Jackson Streaming APIs, or incremental mod ...
- POJ - 2965 The Pilots Brothers' refrigerator(压位+bfs)
The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to op ...
- linux 查看 PHP 的默认版本。
命令 env env:显示当前用户的环境变量: which php
- SourceTree使用
SourceTree的基本使用 1. SourceTree是什么 拥有可视化界面的项目版本控制软件,适用于git项目管理 window.mac可用 2. 获取项目代码 1. 点击克隆/新建 2. ...