From c5e37c1b41f9aea6923f8a685e8dcddd7d9b65a9 Mon Sep 17 00:00:00 2001 From: 0x6AcE <0x6ace@gmail.com> Date: Thu, 23 Nov 2023 15:20:17 +0800 Subject: [PATCH] Simplify the code in array.swift (#960) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 简单点 * Apply suggestions from code review Co-authored-by: nuomi1 --------- Co-authored-by: Yudong Jin Co-authored-by: nuomi1 --- codes/swift/chapter_array_and_linkedlist/array.swift | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/codes/swift/chapter_array_and_linkedlist/array.swift b/codes/swift/chapter_array_and_linkedlist/array.swift index 0bbdeab5a..929a0e96f 100644 --- a/codes/swift/chapter_array_and_linkedlist/array.swift +++ b/codes/swift/chapter_array_and_linkedlist/array.swift @@ -28,7 +28,7 @@ func extend(nums: [Int], enlarge: Int) -> [Int] { /* 在数组的索引 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 }) { + for i in nums.indices.dropFirst(index).reversed() { nums[i] = nums[i - 1] } // 将 num 赋给 index 处元素 @@ -37,9 +37,8 @@ func insert(nums: inout [Int], num: Int, index: Int) { /* 删除索引 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 }) { + for i in nums.indices.dropFirst(index).dropLast() { nums[i] = nums[i + 1] } }