title
The smallest number of K
Enter n integers and find the smallest K number. For example, if you enter the 8 numbers 4,5,1,6,2,7,3,8, the smallest 4 numbers are 1,2,3,4.
title link : smallest number of K
Code
import java.util.ArrayList;
import java.util.Arrays;
/**
* 标题:最小的 K 个数
* 题目描述
* 输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4。
* 题目链接:
* https://www.nowcoder.com/practice/6a296eb82cf844ca8539b57c23e6e9bf?tpId=13&&tqId=11182&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
*/
public class Jz29 {
public ArrayList<Integer> getLeastNumbers_Solution(int[] input, int k) {
ArrayList<Integer> result = new ArrayList<Integer>();
if (input == null) {
return result;
}
if (k > input.length) {
return result;
}
Arrays.sort(input);
for (int i = 0; i < k; i++) {
if (i < input.length) {
result.add(input[i]);
}
}
return result;
}
public static void main(String[] args) {
Jz29 jz29 = new Jz29();
int[] input = new int[]{4, 5, 1, 6, 2, 7, 3, 8};
ArrayList<Integer> result = jz29.getLeastNumbers_Solution(input, 10);
for (Integer data : result) {
System.out.println(data);
}
}
}
[Daily Message] Life is always like this, it can't satisfy people everywhere, but we still have to live enthusiastically. A person lives a lifetime, and there are many things worthy of love. Don't be discouraged because one is not satisfied.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。