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
@@ -0,0 +1,107 @@
/**
* File: array.swift
* Created Time: 2023-01-05
* Author: nuomi1 (nuomi1@qq.com)
*/
/* */
func randomAccess(nums: [Int]) -> Int {
// [0, nums.count) 1
let randomIndex = nums.indices.randomElement()!
//
let randomNum = nums[randomIndex]
return randomNum
}
/* */
func extend(nums: [Int], enlarge: Int) -> [Int] {
//
var res = Array(repeating: 0, count: nums.count + enlarge)
//
for i in nums.indices {
res[i] = nums[i]
}
//
return res
}
/* index num */
func insert(nums: inout [Int], num: Int, index: Int) {
// index 1
for i in nums.indices.dropFirst(index).reversed() {
nums[i] = nums[i - 1]
}
// index num
nums[index] = num
}
/* index */
func remove(nums: inout [Int], index: Int) {
// index 1
for i in nums.indices.dropFirst(index).dropLast() {
nums[i] = nums[i + 1]
}
}
/* */
func traverse(nums: [Int]) {
var count = 0
//
for i in nums.indices {
count += nums[i]
}
//
for num in nums {
count += num
}
//
for (i, num) in nums.enumerated() {
count += nums[i]
count += num
}
}
/* */
func find(nums: [Int], target: Int) -> Int {
for i in nums.indices {
if nums[i] == target {
return i
}
}
return -1
}
@main
enum _Array {
/* Driver Code */
static func main() {
/* */
let arr = Array(repeating: 0, count: 5)
print("配列 arr = \(arr)")
var nums = [1, 3, 2, 5, 4]
print("配列 nums = \(nums)")
/* */
let randomNum = randomAccess(nums: nums)
print("nums からランダムな要素を取得 \(randomNum)")
/* */
nums = extend(nums: nums, enlarge: 3)
print("配列の長さを 8 に拡張すると、nums = \(nums)")
/* */
insert(nums: &nums, num: 6, index: 3)
print("インデックス 3 に数値 6 を挿入すると、nums = \(nums)")
/* */
remove(nums: &nums, index: 2)
print("インデックス 2 の要素を削除すると、nums = \(nums)")
/* */
traverse(nums: nums)
/* */
let index = find(nums: nums, target: 3)
print("nums 内で要素 3 を検索すると、インデックス = \(index)")
}
}
@@ -0,0 +1,90 @@
/**
* File: linked_list.swift
* Created Time: 2023-01-08
* Author: nuomi1 (nuomi1@qq.com)
*/
import utils
/* n0 P */
func insert(n0: ListNode, P: ListNode) {
let n1 = n0.next
P.next = n1
n0.next = P
}
/* n0 */
func remove(n0: ListNode) {
if n0.next == nil {
return
}
// n0 -> P -> n1
let P = n0.next
let n1 = P?.next
n0.next = n1
}
/* index */
func access(head: ListNode, index: Int) -> ListNode? {
var head: ListNode? = head
for _ in 0 ..< index {
if head == nil {
return nil
}
head = head?.next
}
return head
}
/* target */
func find(head: ListNode, target: Int) -> Int {
var head: ListNode? = head
var index = 0
while head != nil {
if head?.val == target {
return index
}
head = head?.next
index += 1
}
return -1
}
@main
enum LinkedList {
/* Driver Code */
static func main() {
/* */
//
let n0 = ListNode(x: 1)
let n1 = ListNode(x: 3)
let n2 = ListNode(x: 2)
let n3 = ListNode(x: 5)
let n4 = ListNode(x: 4)
//
n0.next = n1
n1.next = n2
n2.next = n3
n3.next = n4
print("初期化した連結リストは")
PrintUtil.printLinkedList(head: n0)
/* */
insert(n0: n0, P: ListNode(x: 0))
print("ノード挿入後の連結リストは")
PrintUtil.printLinkedList(head: n0)
/* */
remove(n0: n0)
print("ノード削除後の連結リストは")
PrintUtil.printLinkedList(head: n0)
/* */
let node = access(head: n0, index: 3)
print("連結リストのインデックス 3 にあるノードの値 = \(node!.val)")
/* */
let index = find(head: n0, target: 2)
print("連結リスト内で値が 2 のノードのインデックス = \(index)")
}
}
@@ -0,0 +1,63 @@
/**
* File: list.swift
* Created Time: 2023-01-08
* Author: nuomi1 (nuomi1@qq.com)
*/
@main
enum List {
/* Driver Code */
static func main() {
/* */
var nums = [1, 3, 2, 5, 4]
print("リスト nums = \(nums)")
/* */
let num = nums[1]
print("インデックス 1 の要素にアクセスすると、num = \(num)")
/* */
nums[1] = 0
print("インデックス 1 の要素を 0 に更新すると、nums = \(nums)")
/* */
nums.removeAll()
print("リストを空にした後、nums = \(nums)")
/* */
nums.append(1)
nums.append(3)
nums.append(2)
nums.append(5)
nums.append(4)
print("要素追加後、nums = \(nums)")
/* */
nums.insert(6, at: 3)
print("インデックス 3 に数値 6 を挿入すると、nums = \(nums)")
/* */
nums.remove(at: 3)
print("インデックス 3 の要素を削除すると、nums = \(nums)")
/* */
var count = 0
for i in nums.indices {
count += nums[i]
}
/* */
count = 0
for x in nums {
count += x
}
/* 2 */
let nums1 = [6, 8, 7, 10, 9]
nums.append(contentsOf: nums1)
print("リスト nums1 を nums の後ろに連結すると、nums = \(nums)")
/* */
nums.sort()
print("リストをソートした後、nums = \(nums)")
}
}
@@ -0,0 +1,146 @@
/**
* File: my_list.swift
* Created Time: 2023-01-08
* Author: nuomi1 (nuomi1@qq.com)
*/
/* */
class MyList {
private var arr: [Int] //
private var _capacity: Int //
private var _size: Int //
private let extendRatio: Int //
/* */
init() {
_capacity = 10
_size = 0
extendRatio = 2
arr = Array(repeating: 0, count: _capacity)
}
/* */
func size() -> Int {
_size
}
/* */
func capacity() -> Int {
_capacity
}
/* */
func get(index: Int) -> Int {
//
if index < 0 || index >= size() {
fatalError("インデックスが範囲外")
}
return arr[index]
}
/* */
func set(index: Int, num: Int) {
if index < 0 || index >= size() {
fatalError("インデックスが範囲外")
}
arr[index] = num
}
/* */
func add(num: Int) {
//
if size() == capacity() {
extendCapacity()
}
arr[size()] = num
//
_size += 1
}
/* */
func insert(index: Int, num: Int) {
if index < 0 || index >= size() {
fatalError("インデックスが範囲外")
}
//
if size() == capacity() {
extendCapacity()
}
// index 1
for j in (index ..< size()).reversed() {
arr[j + 1] = arr[j]
}
arr[index] = num
//
_size += 1
}
/* */
@discardableResult
func remove(index: Int) -> Int {
if index < 0 || index >= size() {
fatalError("インデックスが範囲外")
}
let num = arr[index]
// index 1
for j in index ..< (size() - 1) {
arr[j] = arr[j + 1]
}
//
_size -= 1
//
return num
}
/* */
func extendCapacity() {
// extendRatio
arr = arr + Array(repeating: 0, count: capacity() * (extendRatio - 1))
//
_capacity = arr.count
}
/* */
func toArray() -> [Int] {
Array(arr.prefix(size()))
}
}
@main
enum _MyList {
/* Driver Code */
static func main() {
/* */
let nums = MyList()
/* */
nums.add(num: 1)
nums.add(num: 3)
nums.add(num: 2)
nums.add(num: 5)
nums.add(num: 4)
print("リスト nums = \(nums.toArray()) 、容量 = \(nums.capacity()) 、長さ = \(nums.size())")
/* */
nums.insert(index: 3, num: 6)
print("インデックス 3 に数値 6 を挿入すると、nums = \(nums.toArray())")
/* */
nums.remove(index: 3)
print("インデックス 3 の要素を削除すると、nums = \(nums.toArray())")
/* */
let num = nums.get(index: 1)
print("インデックス 1 の要素にアクセスすると、num = \(num)")
/* */
nums.set(index: 1, num: 0)
print("インデックス 1 の要素を 0 に更新すると、nums = \(nums.toArray())")
/* */
for i in 0 ..< 10 {
// i = 5
nums.add(num: i)
}
print("拡張後のリスト nums = \(nums.toArray()) 、容量 = \(nums.capacity()) 、長さ = \(nums.size())")
}
}