mirror of
https://github.com/krahets/hello-algo.git
synced 2026-10-20 21:15:19 +08:00
fix: correct Swift fixed-size array insertion bounds (#1968)
Co-authored-by: Cuong Nguyen <[email protected]> Co-authored-by: krahets <[email protected]>
This commit is contained in:
co-authored by
Cuong Nguyen
krahets
parent
620abe42f3
commit
46a09cf31a
@@ -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 nums.indices.dropFirst(index).reversed() {
|
||||
for i in stride(from: nums.count - 1, to: index, by: -1) {
|
||||
nums[i] = nums[i - 1]
|
||||
}
|
||||
// 将 num 赋给 index 处的元素
|
||||
|
||||
@@ -28,7 +28,7 @@ func extend(nums: [Int], enlarge: Int) -> [Int] {
|
||||
/* Insert element num at index index in the array */
|
||||
func insert(nums: inout [Int], num: Int, index: Int) {
|
||||
// Move all elements at and after index index backward by one position
|
||||
for i in nums.indices.dropFirst(index).reversed() {
|
||||
for i in stride(from: nums.count - 1, to: index, by: -1) {
|
||||
nums[i] = nums[i - 1]
|
||||
}
|
||||
// Assign num to the element at index index
|
||||
|
||||
@@ -28,7 +28,7 @@ func extend(nums: [Int], enlarge: Int) -> [Int] {
|
||||
/* 配列の index 番目に要素 num を挿入 */
|
||||
func insert(nums: inout [Int], num: Int, index: Int) {
|
||||
// インデックス index 以降の全要素を 1 つ後ろへ移動する
|
||||
for i in nums.indices.dropFirst(index).reversed() {
|
||||
for i in stride(from: nums.count - 1, to: index, by: -1) {
|
||||
nums[i] = nums[i - 1]
|
||||
}
|
||||
// index の要素に num を代入する
|
||||
|
||||
@@ -28,7 +28,7 @@ func extend(nums: [Int], enlarge: Int) -> [Int] {
|
||||
/* Вставить элемент num по индексу index в массив */
|
||||
func insert(nums: inout [Int], num: Int, index: Int) {
|
||||
// Сдвинуть элемент с индексом index и все последующие элементы на одну позицию назад
|
||||
for i in nums.indices.dropFirst(index).reversed() {
|
||||
for i in stride(from: nums.count - 1, to: index, by: -1) {
|
||||
nums[i] = nums[i - 1]
|
||||
}
|
||||
// Присвоить num элементу по индексу index
|
||||
|
||||
@@ -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 nums.indices.dropFirst(index).reversed() {
|
||||
for i in stride(from: nums.count - 1, to: index, by: -1) {
|
||||
nums[i] = nums[i - 1]
|
||||
}
|
||||
// 將 num 賦給 index 處的元素
|
||||
|
||||
Reference in New Issue
Block a user