Re-translate the Japanese version (#1871)

* Retranslate Japanese docs with GPT-5.4

* Retranslate Japanese code with GPT-5.4
This commit is contained in:
Yudong Jin
2026-03-30 07:30:15 +08:00
committed by GitHub
parent fe6443235b
commit d7b2277d2b
1444 changed files with 83312 additions and 8363 deletions
+62
View File
@@ -0,0 +1,62 @@
/**
* File: heap.swift
* Created Time: 2024-03-17
* Author: nuomi1 (nuomi1@qq.com)
*/
import HeapModule
import utils
func testPush(heap: inout Heap<Int>, val: Int) {
heap.insert(val)
print("\n要素 \(val) をヒープに追加した後\n")
PrintUtil.printHeap(queue: heap.unordered)
}
func testPop(heap: inout Heap<Int>) {
let val = heap.removeMax()
print("\nヒープ先頭要素 \(val) を取り出した後\n")
PrintUtil.printHeap(queue: heap.unordered)
}
@main
enum _Heap {
/* Driver Code */
static func main() {
/* */
// Swift Heap
var heap = Heap<Int>()
/* */
testPush(heap: &heap, val: 1)
testPush(heap: &heap, val: 3)
testPush(heap: &heap, val: 2)
testPush(heap: &heap, val: 5)
testPush(heap: &heap, val: 4)
/* */
let peek = heap.max()
print("\nヒープ先頭要素は \(peek!)\n")
/* */
testPop(heap: &heap)
testPop(heap: &heap)
testPop(heap: &heap)
testPop(heap: &heap)
testPop(heap: &heap)
/* */
let size = heap.count
print("\nヒープ内の要素数は \(size)\n")
/* */
let isEmpty = heap.isEmpty
print("\nヒープが空かどうか \(isEmpty)\n")
/* */
// O(n) O(nlogn)
let heap2 = Heap([1, 3, 2, 5, 4])
print("\nリストを入力してヒープを構築した後")
PrintUtil.printHeap(queue: heap2.unordered)
}
}
+163
View File
@@ -0,0 +1,163 @@
/**
* File: my_heap.swift
* Created Time: 2023-01-28
* Author: nuomi1 (nuomi1@qq.com)
*/
import utils
/* */
class MaxHeap {
private var maxHeap: [Int]
/* */
init(nums: [Int]) {
//
maxHeap = nums
//
for i in (0 ... parent(i: size() - 1)).reversed() {
siftDown(i: i)
}
}
/* */
private func left(i: Int) -> Int {
2 * i + 1
}
/* */
private func right(i: Int) -> Int {
2 * i + 2
}
/* */
private func parent(i: Int) -> Int {
(i - 1) / 2 //
}
/* */
private func swap(i: Int, j: Int) {
maxHeap.swapAt(i, j)
}
/* */
func size() -> Int {
maxHeap.count
}
/* */
func isEmpty() -> Bool {
size() == 0
}
/* */
func peek() -> Int {
maxHeap[0]
}
/* */
func push(val: Int) {
//
maxHeap.append(val)
//
siftUp(i: size() - 1)
}
/* i */
private func siftUp(i: Int) {
var i = i
while true {
// i
let p = parent(i: i)
//
if p < 0 || maxHeap[i] <= maxHeap[p] {
break
}
// 2
swap(i: i, j: p)
//
i = p
}
}
/* */
func pop() -> Int {
//
if isEmpty() {
fatalError("ヒープが空です")
}
//
swap(i: 0, j: size() - 1)
//
let val = maxHeap.remove(at: size() - 1)
//
siftDown(i: 0)
//
return val
}
/* i */
private func siftDown(i: Int) {
var i = i
while true {
// i, l, r ma
let l = left(i: i)
let r = right(i: i)
var ma = i
if l < size(), maxHeap[l] > maxHeap[ma] {
ma = l
}
if r < size(), maxHeap[r] > maxHeap[ma] {
ma = r
}
// i l, r
if ma == i {
break
}
// 2
swap(i: i, j: ma)
//
i = ma
}
}
/* */
func print() {
let queue = maxHeap
PrintUtil.printHeap(queue: queue)
}
}
@main
enum MyHeap {
/* Driver Code */
static func main() {
/* */
let maxHeap = MaxHeap(nums: [9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2])
print("\nリストを入力してヒープを構築した後")
maxHeap.print()
/* */
var peek = maxHeap.peek()
print("\nヒープ先頭要素は \(peek)")
/* */
let val = 7
maxHeap.push(val: val)
print("\n要素 \(val) をヒープに追加した後")
maxHeap.print()
/* */
peek = maxHeap.pop()
print("\nヒープ先頭要素 \(peek) を取り出した後")
maxHeap.print()
/* */
let size = maxHeap.size()
print("\nヒープ内の要素数は \(size)")
/* */
let isEmpty = maxHeap.isEmpty()
print("\nヒープが空かどうか \(isEmpty)")
}
}
+36
View File
@@ -0,0 +1,36 @@
/**
* File: top_k.swift
* Created Time: 2023-07-02
* Author: nuomi1 (nuomi1@qq.com)
*/
import HeapModule
import utils
/* k */
func topKHeap(nums: [Int], k: Int) -> [Int] {
// k
var heap = Heap(nums.prefix(k))
// k+1 k
for i in nums.indices.dropFirst(k) {
//
if nums[i] > heap.min()! {
_ = heap.removeMin()
heap.insert(nums[i])
}
}
return heap.unordered
}
@main
enum TopK {
/* Driver Code */
static func main() {
let nums = [1, 7, 6, 3, 2]
let k = 3
let res = topKHeap(nums: nums, k: k)
print("最大の \(k) 個の要素は")
PrintUtil.printHeap(queue: res)
}
}