剑指OFFER之最小的K个数(九度OJ1371)
题目描述:
-
输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。
- 输入:
-
每个测试案例包括2行:
第一行为2个整数n,k(1<=n,k<=200000),表示数组的长度。
第二行包含n个整数,表示这n个数,数组中的数的范围是[0,1000 000 000]。
- 输出:
-
对应每个测试案例,输出最小的k个数,并按从小到大顺序打印。
- 样例输入:
- 样例输出:
解题思路:
我们通过快排找到第k个数,然后比他的小的都在左边,比他大的都在右边。
void getNumber(int n,int m){
int i;
int begin = ;
int end = n-;
int index = patition(begin,end);
while(index != m-){
if(index > m-){
end = index -;
index = patition(begin,end);
}else{
begin = index+;
index = patition(begin,end);
}
}
}
在进行一次快排,将数组有序的输出。
void Qsort(int begin,int end){
if(begin >= end)
return ;
else{
int middle = patition(begin,end);
Qsort(begin,middle-);
Qsort(middle+,end);
}
}
快排的代码如下:
int patition(int begin,int end){
int index,small;
small = begin-;
for(index = begin;index < end;index++){
if(gArr[index] < gArr[end]){
small++;
if(small != index)
swap(index,small);
}
}
small++;
swap(small,end);
return small;
}
void swap(int i,int j){
int tmp = gArr[j];
gArr[j] = gArr[i];
gArr[i] = tmp;
}
全部代码:
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 200000
int gArr[MAXSIZE]={};
void getNumber(int n,int m);
void Qsort(int begin,int end);
int patition(int begin,int end);
void swap(int i,int j);
int main(){
int n,m,i;
while(scanf("%d %d",&n,&m)!=EOF && n>= && n<=){
for(i=;i<n;i++)
scanf("%d",&gArr[i]);
getNumber(n,m);
Qsort(,m-);
for(i=;i<m-;i++){
printf("%d ",gArr[i]);
}
printf("%d\n",gArr[m-]);
}
return ;
}
void Qsort(int begin,int end){
if(begin >= end)
return ;
else{
int middle = patition(begin,end);
Qsort(begin,middle-);
Qsort(middle+,end);
}
}
void getNumber(int n,int m){
int i;
int begin = ;
int end = n-;
int index = patition(begin,end);
while(index != m-){
if(index > m-){
end = index -;
index = patition(begin,end);
}else{
begin = index+;
index = patition(begin,end);
}
}
}
int patition(int begin,int end){
int index,small;
small = begin-;
for(index = begin;index < end;index++){
if(gArr[index] < gArr[end]){
small++;
if(small != index)
swap(index,small);
}
}
small++;
swap(small,end);
return small;
}
void swap(int i,int j){
int tmp = gArr[j];
gArr[j] = gArr[i];
gArr[i] = tmp;
}
/**************************************************************
Problem: 1371
User: xhalo
Language: C
Result: Accepted
Time:950 ms
Memory:1696 kb
****************************************************************/
剑指OFFER之最小的K个数(九度OJ1371)的更多相关文章
- 剑指 Offer 40. 最小的k个数 + 优先队列 + 堆 + 快速排序
剑指 Offer 40. 最小的k个数 Offer_40 题目描述 解法一:排序后取前k个数 /** * 题目描述:输入整数数组 arr ,找出其中最小的 k 个数.例如,输入4.5.1.6.2.7. ...
- 剑指 Offer 40. 最小的k个数
剑指 Offer 40. 最小的k个数 输入整数数组 arr ,找出其中最小的 k 个数.例如,输入4.5.1.6.2.7.3.8这8个数字,则最小的4个数字是1.2.3.4. 示例 1: 输入:ar ...
- 【剑指Offer】最小的K个数 解题报告(Python)
[剑指Offer]最小的K个数 解题报告(Python) 标签(空格分隔): 剑指Offer 题目地址:https://www.nowcoder.com/ta/coding-interviews 题目 ...
- 【Java】 剑指offer(40) 最小的k个数
本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 输入n个整数,找出其中最小的k个数.例如输入4.5.1.6.2.7 ...
- Go语言实现:【剑指offer】最小的K个数
该题目来源于牛客网<剑指offer>专题. 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. Go语言实现: fu ...
- 每日一题 - 剑指 Offer 40. 最小的k个数
题目信息 时间: 2019-06-30 题目链接:Leetcode tag: 快排 难易程度:中等 题目描述: 输入整数数组 arr ,找出其中最小的 k 个数.例如,输入4.5.1.6.2.7.3. ...
- 《剑指offer》最小的k个数
本题来自<剑指offer> 反转链表 题目: 思路: C++ Code: Python Code: 总结:
- 剑指Offer 29. 最小的K个数 (其他)
题目描述 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4. 题目地址 https://www.nowcoder.com/prac ...
- 【剑指offer】最小的K个数
一.题目: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. 二.思路: 一群大牛在讨论用噼里啪啦各种排序,复杂度一般也都是O ...
随机推荐
- LCMS
LCMS(LearningContent Management System) 即学习内容管理系统
- IIS会话过期的问题/WCF日志管理
http://technet.microsoft.com/zh-cn/library/cc725624(v=ws.10).aspx http://msdn.microsoft.com/zh-cn/li ...
- Error Code: 1175
用mysql workbench 更新一个表的时候报如下异常: Error Code: 1175. To disable safe mode, toggle the option in Prefere ...
- many-to-one和one-to-many的配置比较
many-to-one配置: <many-to-one name="dailyCatalog" column="daily_catalog_id" cla ...
- Dubbo使用解析及远程服务框架
this is a thub here Spring的Remoting框架 阿里巴巴的dubbo框架 RPC,RMI,JMS,Webservice的区别
- bzoj4028
一眼分块题…… 分块,维护每个块的总的gcd和xor和 先思考我们应该怎么查询,考虑到gcd是一个神奇的东西,因为它最多变化logX次 于是我们从前往后扫描每个块,如果一个块内总的gcd是当前扫描的前 ...
- oracle 字段上下两条记录的相减
SELECT T.ID ,BALANCE,nvl(lag (BALANCE,1) over (order by T.ID ) ,0) FROM AN T ORDER BY T.ID [转]orac ...
- JDK版本1.6和6.0到底指什么
Both version numbers (1.6.0 and 6) are used to identify this release of the Java Platform. Version 6 ...
- 【转】google play上传应用
原文网址:https://support.google.com/googleplay/android-developer/answer/113469?hl=zh-Hans 注册 Google Play ...
- 【转】Tomcat配置文件入门
Tomcat 基本配置 tomcat读取配置文件 首先简单说一下tomcat是如何读取配置文件的.tomcat在启动时,首先找系统变量CATALINA_BASE,如果没有,则找CATALINA_HOM ...