剑指Offer-30
来源:互联网 发布:naza飞控调参软件下载 编辑:程序博客网 时间:2024/06/10 23:31
题目:
输入 n 个整数,找出其中最小的的 K 个数。
实现
// coding = javapublic class Solution30 { public static void findNums(int[] nums ,int k){ if(nums==null || nums.length<1 || k<1 || k>nums.length){ return; } if(k==nums.length){ for(int i=0;i<k;i++){ System.out.print(nums[i]); } return; } int low = 0; int high = nums.length-1; int index = partition(nums,low,high); while(index!=k){ if(index>k){ high=index-1; index = partition(nums,low,high); } else{ low = index+1; index = partition(nums,low,high); } } for(int i =0;i<k;i++){ System.out.print(nums[i]); } } public static int partition(int nums[],int low,int high){ int pivot = nums[low]; while (low<high){ while (low<high && nums[high]>=pivot){ high--; } nums[low]=nums[high]; while (low<high && nums[low]<=pivot){ low++; } nums[high]=nums[low]; } nums[low]=pivot; return low; } public static void main(String[] args){ int[] nums = {1,2,3,4,5,6,7,8,9}; findNums(nums,8); }}
阅读全文
1 0
- 【26-30】剑指offer
- 剑指Offer-30
- 剑指offer面试题30
- 剑指offer(21-30)
- 【剑指offer】21-30题
- 剑指offer 试题21~30
- 剑指OFFER
- 剑指offer
- 剑指Offer
- 剑指offer
- 剑指Offer
- 剑指offer
- 剑指offer
- 剑指offer
- 剑指Offer
- 剑指offer
- 剑指offer
- 剑指Offer
- Eclipse开发SpringMVC简单小程序
- 业界良心? 谷歌宣布Chrome将自带广告拦截功能
- [P1052]过河
- 实战hadoop海量数据处理系列 01:数据导入篇
- Spark2.8.0源代码导入,XMPP即时通讯Openfire和Spark源代码导入
- 剑指Offer-30
- FloatingActionButton+RecycleView联动,并解决只隐藏不出现的bug
- 编辑距离(dp)
- [LeetCode] Is Subsequence
- 插入排序
- 最详细的openfire3.7.1 插件开发环境搭建
- Oracle日常性能查看
- Android 开发之 App 启动时间统计
- 关于框架和模式的学习,重点为MyBatis