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>
pull/965/head
0x6AcE 12 months ago committed by GitHub
parent 5b6fb34a82
commit c5e37c1b41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -28,7 +28,7 @@ func extend(nums: [Int], enlarge: Int) -> [Int] {
/* 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 nums.indices.dropFirst(index).reversed() {
nums[i] = nums[i - 1] nums[i] = nums[i - 1]
} }
// num index // num index
@ -37,9 +37,8 @@ func insert(nums: inout [Int], num: Int, index: Int) {
/* index */ /* index */
func remove(nums: inout [Int], index: Int) { func remove(nums: inout [Int], index: Int) {
let count = nums.count
// index // 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] nums[i] = nums[i + 1]
} }
} }

Loading…
Cancel
Save