Skip to content

Commit cfa9977

Browse files
author
wuyinjun
committed
新增两题swift
1 parent f3e2924 commit cfa9977

File tree

8 files changed

+90
-0
lines changed

8 files changed

+90
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import UIKit
2+
3+
class Solution {
4+
func removeDuplicates(_ nums: inout [Int]) -> Int {
5+
guard nums.count > 1 else {
6+
return 1
7+
}
8+
guard nums.count > 2 else {
9+
if nums[0] == nums[1] {
10+
return 1
11+
} else {
12+
return 2
13+
}
14+
}
15+
var slow = 0
16+
var fast = 1
17+
while fast < nums.count {
18+
if nums[slow] != nums[fast] {
19+
nums[slow + 1] = nums[fast]
20+
fast += 1
21+
slow += 1
22+
} else {
23+
fast += 1
24+
}
25+
}
26+
return slow + 1
27+
}
28+
}
29+
30+
let s = Solution()
31+
var list = [1, 1, 2]
32+
let result = s.removeDuplicates(&list)
33+
print(list)
34+
print(result)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

26. 删除有序数组中的重复项/MyPlayground.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import UIKit
2+
3+
4+
class Solution {
5+
func merge(_ nums1: inout [Int], _ m: Int, _ nums2: [Int], _ n: Int) {
6+
var list1Index = m - 1
7+
var list2Index = n - 1
8+
var index = m + n - 1
9+
10+
while index >= 0 {
11+
if list1Index < 0 {
12+
nums1[index] = nums2[list2Index]
13+
index -= 1
14+
list2Index -= 1
15+
continue
16+
}
17+
if list2Index < 0 {
18+
nums1[index] = nums1[list1Index]
19+
index -= 1
20+
list1Index -= 1
21+
continue
22+
}
23+
if nums1[list1Index] < nums2[list2Index] {
24+
nums1[index] = nums2[list2Index]
25+
index -= 1
26+
list2Index -= 1
27+
} else {
28+
nums1[index] = nums1[list1Index]
29+
index -= 1
30+
list1Index -= 1
31+
}
32+
}
33+
}
34+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

88. 合并两个有序数组/MyPlayground.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)