Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
master
Branches (1)
master
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
The license selected for the repository is subject to the license used by the main branch of the repository.
master
Branches (1)
master
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (1)
master
algorithm
/
src
/
arrays
/
array_23.java
algorithm
/
src
/
arrays
/
array_23.java
array_23.java 1.99 KB
Copy Edit Raw Blame History
Byte authored 2026年07月03日 06:34 +08:00 . 11
package arrays;
import java.util.ArrayList;
import java.util.PriorityQueue;
/**
* 题目: 数组中最小的 K 个数
*
* @Author Gavin
* @date 2022年08月06日 15:52
*/
public class array_23 {
/**
* 第一种方法:使用一个大小为k的大顶堆(默认是小顶堆[从小到大输出])
*/
//O(NlogK) + O(K)
public ArrayList<Integer> solution(int[] nums, int k) {
if (k > nums.length || k <= 0)
return new ArrayList<>();
PriorityQueue<Integer> maxHeap = new PriorityQueue<>((o1, o2) -> o2 - o1);
for (int num : nums) {
maxHeap.add(num);
if (maxHeap.size() > k)
maxHeap.poll();
}
return new ArrayList<>(maxHeap);
}
/**
* 第二种方法:选择排序
*/
//利用选择排序
public ArrayList<Integer> solution_2(int[] nums, int k) {
ArrayList<Integer> result = new ArrayList<>();
getByFastAsc(nums);//传递的是引用,所以nums里面已经排序了
for (int i = 0; i < k; i++) {
result.add(nums[i]);
}
return result;
}
public void getByFastAsc(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {//控制循环次数
int min = i;//最小索引
for (int j = i + 1; j < arr.length; j++) {//从无序区选取最小的记录
if (arr[min] > arr[j]) {//如果最小数索引的数比后面的数大就和该数交换索引
min = j;//交换索引,循环重复后得出最小的数的索引
}
}
if (min != i) {//如果最小数的索引和一开始引用的数的索引不想等,那么就做一个替换。
int temp = arr[i];//把一开始引用的数传给中间介质,也就是较大值
arr[i] = arr[min];//把最小数传到一开始定位的位置
arr[min] = temp;//把中间介质传给最小数,把大值赋值给min位置
}
}
}
}
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

Releases

No release

Contributors

All

Language(Optional)

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/Gavery/algorithm.git
git@gitee.com:Gavery/algorithm.git
Gavery
algorithm
algorithm
master
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /