mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-11 23:16:07 +00:00
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:
@@ -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())")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user