mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-24 20:52:16 +08:00
Retranslate the complete Japanese online edition from Chinese (#2008)
* Retranslate the complete Japanese online edition from Chinese * Fit Japanese homepage labels and localize heading permalinks * Point Japanese contribution instructions to the Japanese preview * Replace locale conditionals with multilingual UI tables
This commit is contained in:
@@ -6,17 +6,17 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* ヒープに基づいて配列中の最大の k 個の要素を探す */
|
||||
/* ヒープを使って配列内の大きいほうから k 個の要素を求める */
|
||||
priority_queue<int, vector<int>, greater<int>> topKHeap(vector<int> &nums, int k) {
|
||||
// 最小ヒープを初期化
|
||||
// 最小ヒープを初期化する
|
||||
priority_queue<int, vector<int>, greater<int>> heap;
|
||||
// 配列の先頭 k 個の要素をヒープに追加
|
||||
// 配列の先頭 k 個の要素をヒープに挿入する
|
||||
for (int i = 0; i < k; i++) {
|
||||
heap.push(nums[i]);
|
||||
}
|
||||
// k+1 番目の要素から開始し、ヒープ長を k に保つ
|
||||
// k+1 番目の要素から始め、ヒープの長さを k に保つ
|
||||
for (int i = k; i < nums.size(); i++) {
|
||||
// 現在の要素がヒープ先頭より大きければ、ヒープ先頭を取り出して現在の要素を追加する
|
||||
// 現在の要素がヒープの先頭要素より大きい場合、先頭要素を取り出し、現在の要素を挿入する
|
||||
if (nums[i] > heap.top()) {
|
||||
heap.pop();
|
||||
heap.push(nums[i]);
|
||||
@@ -25,13 +25,13 @@ priority_queue<int, vector<int>, greater<int>> topKHeap(vector<int> &nums, int k
|
||||
return heap;
|
||||
}
|
||||
|
||||
// Driver Code
|
||||
// 実行用コード
|
||||
int main() {
|
||||
vector<int> nums = {1, 7, 6, 3, 2};
|
||||
int k = 3;
|
||||
|
||||
priority_queue<int, vector<int>, greater<int>> res = topKHeap(nums, k);
|
||||
cout << "最大の " << k << " 個の要素は: ";
|
||||
cout << "大きいほうから " << k << " 個の要素: ";
|
||||
printHeap(res);
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user