-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathMergeTwoSortedArray.swift
99 lines (92 loc) · 2.73 KB
/
MergeTwoSortedArray.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//
// MergeTwoSortedArray.swift
// Array
//
// Created by ggl on 2019/3/21.
// Copyright © 2019年 ggl. All rights reserved.
//
import Foundation
/// 合并两个有序数组为一个有序数组
///
/// - Parameters:
/// - arrOne: 第一个有序数组
/// - arrOneCount: 第一个有序数组的大小
/// - arrTwo: 第二个有序数组
/// - arrTwoCount: 第二个有序数组的大小
/// - Returns: 新的有序数组(需要释放)
func mergeTwoSortedArray(arrOne: UnsafePointer<Int>, arrOneCount: Int, arrTwo: UnsafePointer<Int>, arrTwoCount: Int) -> UnsafePointer<Int> {
let newArray = UnsafeMutablePointer<Int>.allocate(capacity: arrOneCount + arrTwoCount)
var i = 0, j = 0
for index in 0..<(arrOneCount + arrTwoCount) {
// 判断i和j有没有到头
if i > arrOneCount-1 {
newArray[index] = arrTwo[j]
j += 1
} else if j > arrTwoCount-1 {
newArray[index] = arrOne[i]
i += 1
} else {
let num1 = arrOne[i]
let num2 = arrTwo[j]
if num1 > num2 {
newArray[index] = num2
j += 1
} else {
newArray[index] = num1
i += 1
}
}
}
return UnsafePointer(newArray)
}
/// 合并两个有序数组
/// - Parameters:
/// - arr: 有序的第一个数组
/// - otherArr: 有序的第二个数组
/// - Returns: 有序的合并数组
func mergeTwoSortedArray(_ arr: [Int], _ otherArr: [Int]) -> [Int] {
var i = 0, j = 0
var resultArr = [Int]()
while i < arr.count || j < otherArr.count {
if i == arr.count {
// arr 数组到头了,只需要添加 otherArr 中的元素
print(otherArr[j])
resultArr.append(otherArr[j])
j += 1
} else if j == otherArr.count {
// otherArr 数组到头了,只需要添加 arr 中的元素
print(arr[i])
resultArr.append(arr[i])
i += 1
} else {
// 比较两者大小
if arr[i] > otherArr[j] {
print(otherArr[j])
resultArr.append(otherArr[j])
j += 1
} else {
print(arr[i])
resultArr.append(arr[i])
i += 1
}
}
}
return resultArr
}
/// 打印数组内容
///
/// - Parameter arr: 数组
func printArray(arr: UnsafePointer<Int>, count: Int) {
if count == 0 {
print("[]")
return
}
print("[", terminator: "")
for i in 0..<count {
if i == count - 1 {
print(arr[count-1], terminator: "]\n")
} else {
print(arr[i], terminator: " ")
}
}
}