|
|
@ -84,7 +84,7 @@ comments: true
|
|
|
|
=== "Swift"
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
|
|
```swift title="array.swift"
|
|
|
|
```swift title="array.swift"
|
|
|
|
// 初始化数组
|
|
|
|
/* 初始化数组 */
|
|
|
|
let arr = Array(repeating: 0, count: 5) // [0, 0, 0, 0, 0]
|
|
|
|
let arr = Array(repeating: 0, count: 5) // [0, 0, 0, 0, 0]
|
|
|
|
let nums = [1, 3, 2, 5, 4]
|
|
|
|
let nums = [1, 3, 2, 5, 4]
|
|
|
|
```
|
|
|
|
```
|
|
|
@ -204,7 +204,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
|
|
|
=== "Swift"
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
|
|
```swift title="array.swift"
|
|
|
|
```swift title="array.swift"
|
|
|
|
// 随机返回一个数组元素
|
|
|
|
/* 随机返回一个数组元素 */
|
|
|
|
func randomAccess(nums: [Int]) -> Int {
|
|
|
|
func randomAccess(nums: [Int]) -> Int {
|
|
|
|
// 在区间 [0, nums.count) 中随机抽取一个数字
|
|
|
|
// 在区间 [0, nums.count) 中随机抽取一个数字
|
|
|
|
let randomIndex = nums.indices.randomElement()!
|
|
|
|
let randomIndex = nums.indices.randomElement()!
|
|
|
@ -341,7 +341,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
|
|
|
=== "Swift"
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
|
|
```swift title="array.swift"
|
|
|
|
```swift title="array.swift"
|
|
|
|
// 扩展数组长度
|
|
|
|
/* 扩展数组长度 */
|
|
|
|
func extend(nums: [Int], enlarge: Int) -> [Int] {
|
|
|
|
func extend(nums: [Int], enlarge: Int) -> [Int] {
|
|
|
|
// 初始化一个扩展长度后的数组
|
|
|
|
// 初始化一个扩展长度后的数组
|
|
|
|
var res = Array(repeating: 0, count: nums.count + enlarge)
|
|
|
|
var res = Array(repeating: 0, count: nums.count + enlarge)
|
|
|
@ -526,7 +526,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
|
|
|
=== "Swift"
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
|
|
```swift title="array.swift"
|
|
|
|
```swift title="array.swift"
|
|
|
|
// 在数组的索引 index 处插入元素 num
|
|
|
|
/* 在数组的索引 index 处插入元素 num */
|
|
|
|
func insert(nums: inout [Int], num: Int, index: Int) {
|
|
|
|
func insert(nums: inout [Int], num: Int, index: Int) {
|
|
|
|
// 把索引 index 以及之后的所有元素向后移动一位
|
|
|
|
// 把索引 index 以及之后的所有元素向后移动一位
|
|
|
|
for i in sequence(first: nums.count - 1, next: { $0 > index + 1 ? $0 - 1 : nil }) {
|
|
|
|
for i in sequence(first: nums.count - 1, next: { $0 > index + 1 ? $0 - 1 : nil }) {
|
|
|
@ -536,7 +536,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
|
|
|
nums[index] = num
|
|
|
|
nums[index] = num
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 删除索引 index 处元素
|
|
|
|
/* 删除索引 index 处元素 */
|
|
|
|
func remove(nums: inout [Int], index: Int) {
|
|
|
|
func remove(nums: inout [Int], index: Int) {
|
|
|
|
let count = nums.count
|
|
|
|
let count = nums.count
|
|
|
|
// 把索引 index 之后的所有元素向前移动一位
|
|
|
|
// 把索引 index 之后的所有元素向前移动一位
|
|
|
@ -674,7 +674,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
|
|
|
=== "Swift"
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
|
|
```swift title="array.swift"
|
|
|
|
```swift title="array.swift"
|
|
|
|
// 遍历数组
|
|
|
|
/* 遍历数组 */
|
|
|
|
func traverse(nums: [Int]) {
|
|
|
|
func traverse(nums: [Int]) {
|
|
|
|
var count = 0
|
|
|
|
var count = 0
|
|
|
|
// 通过索引遍历数组
|
|
|
|
// 通过索引遍历数组
|
|
|
@ -793,7 +793,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
|
|
|
=== "Swift"
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
|
|
```swift title="array.swift"
|
|
|
|
```swift title="array.swift"
|
|
|
|
// 在数组中查找指定元素
|
|
|
|
/* 在数组中查找指定元素 */
|
|
|
|
func find(nums: [Int], target: Int) -> Int {
|
|
|
|
func find(nums: [Int], target: Int) -> Int {
|
|
|
|
for i in nums.indices {
|
|
|
|
for i in nums.indices {
|
|
|
|
if nums[i] == target {
|
|
|
|
if nums[i] == target {
|
|
|
|