mirror of
https://github.com/krahets/hello-algo.git
synced 2025-01-23 14:20:29 +08:00
Simplify the code in array.swift (#960)
* 简单点 * Apply suggestions from code review Co-authored-by: nuomi1 <nuomi1@qq.com> --------- Co-authored-by: Yudong Jin <krahets@163.com> Co-authored-by: nuomi1 <nuomi1@qq.com>
This commit is contained in:
parent
5b6fb34a82
commit
c5e37c1b41
@ -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]
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user