mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-26 23:06:31 +08:00
fcbaf101a4
* Update Dockerfile for code debugging. * Format Python code using Black. * Improve dark theme by defining html classes for the figures, animations and cover images. * Fix several glossary translation. * Update a code comment. * Fix climbing_stairs_backtrack: the pruning should not require the sorted choices list. * Update the code of array and list traversal. * Fix a rendering issue of README.md * Update code of list traversal. * Fix array_definition.png * Update README.md * Fix max_capacity_moving_short_board.png * Fix array.dart * Fix array.dart * Fix array.dart * Fix array.dart
103 lines
2.8 KiB
Swift
103 lines
2.8 KiB
Swift
/**
|
|
* File: array.swift
|
|
* Created Time: 2023-01-05
|
|
* Author: nuomi1 (nuomi1@qq.com)
|
|
*/
|
|
|
|
/* 随机访问元素 */
|
|
func randomAccess(nums: [Int]) -> Int {
|
|
// 在区间 [0, nums.count) 中随机抽取一个数字
|
|
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 以及之后的所有元素向后移动一位
|
|
for i in sequence(first: nums.count - 1, next: { $0 > index + 1 ? $0 - 1 : nil }) {
|
|
nums[i] = nums[i - 1]
|
|
}
|
|
// 将 num 赋给 index 处元素
|
|
nums[index] = num
|
|
}
|
|
|
|
/* 删除索引 index 处元素 */
|
|
func remove(nums: inout [Int], index: Int) {
|
|
let count = nums.count
|
|
// 把索引 index 之后的所有元素向前移动一位
|
|
for i in sequence(first: index, next: { $0 < count - 1 - 1 ? $0 + 1 : nil }) {
|
|
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
|
|
}
|
|
}
|
|
|
|
/* 在数组中查找指定元素 */
|
|
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)")
|
|
}
|
|
}
|