Skip to content

Commit d903176

Browse files
committed
Merge pull request soapyigu#7 from soapyigu/Array
Array
2 parents 83c605a + 2a785be commit d903176

File tree

6 files changed

+114
-2
lines changed

6 files changed

+114
-2
lines changed

Array/ContainsDuplicate.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/contains-duplicate/
3+
* Primary idea: traverse the array and use a set to check duplicates
4+
*
5+
* Time Complexity: O(n), Space Complexity: O(n)
6+
*
7+
*/
8+
9+
class ContainsDuplicate {
10+
func containsDuplicate(nums: [Int]) -> Bool {
11+
if nums.count <= 1 {
12+
return false
13+
}
14+
15+
var set = Set<Int>()
16+
17+
for i in 0...nums.count - 1 {
18+
if set.contains(nums[i]) {
19+
return true
20+
} else {
21+
set.insert(nums[i])
22+
}
23+
}
24+
25+
return false
26+
}
27+
}

Array/ContainsDuplicateII.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/contains-duplicate-ii/
3+
* Primary idea: use a dictionary to check duplicates, then judge if their distance is less than k
4+
*
5+
* Time Complexity: O(n), Space Complexity: O(n)
6+
*
7+
*/
8+
9+
class ContainsDuplicateII {
10+
func containsNearbyDuplicate(nums: [Int], _ k: Int) -> Bool {
11+
// edge case
12+
if nums.count <= 1 {
13+
return false
14+
}
15+
16+
// key: nums[index], value: index
17+
var dict = [Int: Int]()
18+
19+
for i in 0...nums.count - 1 {
20+
if let index = dict[nums[i]] {
21+
if i - index <= k {
22+
return true
23+
}
24+
}
25+
26+
dict[nums[i]] = i
27+
}
28+
29+
return false
30+
}
31+
}

Array/MoveZeroes.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/move-zeroes/
3+
* Primary idea: keep index zeroIndex, traverse through the array and swap the value
4+
*
5+
* Time Complexity: O(n), Space Complexity: O(1)
6+
*
7+
*/
8+
9+
class MoveZeroes {
10+
func moveZeroes(inout nums: [Int]) {
11+
var zeroIndex = 0
12+
var left = 0
13+
let right = nums.count
14+
15+
while left < right {
16+
if nums[left] != 0 {
17+
_swap(&nums, zeroIndex, left)
18+
zeroIndex += 1
19+
}
20+
left += 1
21+
}
22+
}
23+
24+
private func _swap(inout nums: [Int], _ p: Int, _ q: Int) {
25+
let temp = nums[p]
26+
nums[p] = nums[q]
27+
nums[q] = temp
28+
}
29+
}

Array/RemoveDuplicatesFromSortedArray.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class RemoveDuplicatesFromSortedArray {
1111
return nums.count
1212
}
1313

14-
var lastIndex: Int = 0
14+
var lastIndex = 0
1515

1616
for i in 1...nums.count - 1 {
1717
if nums[i] != nums[lastIndex] {

Array/RemoveElement.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/remove-element/
3+
* Primary idea: keep a index, compare the element at that index with val while moving forward
4+
* Time Complexity: O(n), Space Complexity: O(1)
5+
*
6+
*/
7+
8+
class RemoveElement {
9+
func removeElement(inout nums: [Int], _ val: Int) -> Int {
10+
var lastIndex = 0
11+
12+
if nums.count == 0 {
13+
return lastIndex
14+
}
15+
16+
for i in 0...nums.count - 1 {
17+
if nums[i] != val {
18+
nums[lastIndex] = nums[i]
19+
lastIndex += 1
20+
}
21+
}
22+
23+
return lastIndex
24+
}
25+
}

Array/RotateArray.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class RotateArray {
3434
}
3535

3636
private func _swap(inout nums: [Int], _ p: Int, _ q: Int) {
37-
var temp: Int = nums[p]
37+
var temp = nums[p]
3838
nums[p] = nums[q]
3939
nums[q] = temp
4040
}

0 commit comments

Comments
 (0)