Skip to content

Commit acf5bab

Browse files
author
Yi Gu
committed
Merge branch 'master' into Sort
# Conflicts: # Sort/MeetingRooms.swift # Sort/MergeIntervals.swift
2 parents 87d93f7 + 454ae99 commit acf5bab

File tree

75 files changed

+2566
-28
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+2566
-28
lines changed

Array/ContainsDuplicate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ContainsDuplicate {
1414

1515
var set = Set<Int>()
1616

17-
for i in 0...nums.count - 1 {
17+
for i in 0 ..< nums.count {
1818
if set.contains(nums[i]) {
1919
return true
2020
} else {

Array/ContainsDuplicateII.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class ContainsDuplicateII {
1616
// key: nums[index], value: index
1717
var dict = [Int: Int]()
1818

19-
for i in 0...nums.count - 1 {
19+
for i in 0 ..< nums.count {
2020
guard let index = dict[nums[i]] where i - index <= k else {
2121
dict[nums[i]] = i
2222
continue
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/maximum-size-subarray-sum-equals-k/
3+
* Primary idea: Use a dictionary to track the sum so far since the first until the current
4+
* Time Complexity: O(n), Space Complexity: O(n)
5+
*/
6+
7+
class MaximumSizeSubarraySumEqualsK {
8+
func maxSubArrayLen(nums: [Int], _ k: Int) -> Int {
9+
var longestLen = 0
10+
var dict = [Int: Int]()
11+
dict[0] = -1
12+
var sum = 0
13+
14+
for i in 0 ..< nums.count {
15+
sum += nums[i]
16+
17+
if let lastIndex = dict[sum - k] {
18+
longestLen = max(longestLen, i - lastIndex)
19+
}
20+
21+
guard let index = dict[sum] else {
22+
dict[sum] = i
23+
continue
24+
}
25+
}
26+
27+
return longestLen
28+
}
29+
}

Array/MoveZeroes.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,12 @@
99
class MoveZeroes {
1010
func moveZeroes(inout nums: [Int]) {
1111
var zeroIndex = 0
12-
var left = 0
13-
let right = nums.count
1412

15-
while left < right {
16-
if nums[left] != 0 {
17-
_swap(&nums, zeroIndex, left)
13+
for i in 0 ..< nums.count {
14+
if nums[i] != 0 {
15+
_swap(&nums, zeroIndex, i)
1816
zeroIndex += 1
1917
}
20-
left += 1
2118
}
2219
}
2320

Array/ProductExceptSelf.swift

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/product-of-array-except-self/
3+
* Primary idea: Use two arrays to hold multiplication result from left and right sides
4+
* while iterating the original array
5+
* Time Complexity: O(n), Space Complexity: O(n)
6+
*/
7+
8+
class ProductExceptSelf {
9+
func productExceptSelf(nums: [Int]) -> [Int] {
10+
var res = [Int]()
11+
12+
guard nums.count > 0 else {
13+
return res
14+
}
15+
16+
let left = _initLeft(nums)
17+
let right = _initRight(nums)
18+
19+
for i in 0 ..< nums.count {
20+
res.append(left[i] * right[i])
21+
}
22+
23+
return res
24+
}
25+
26+
private func _initLeft(nums: [Int]) -> [Int] {
27+
var left = [Int]()
28+
left.append(1)
29+
30+
for i in 1 ..< nums.count {
31+
left.append(left[i - 1] * nums[i - 1])
32+
}
33+
34+
return left
35+
}
36+
37+
private func _initRight(nums: [Int]) -> [Int] {
38+
var right = Array(count: nums.count, repeatedValue: 1)
39+
40+
for i in (nums.count - 2).stride(through: 0, by: -1) {
41+
right[i] = right[i + 1] * nums[i + 1]
42+
}
43+
44+
return right
45+
}
46+
}

Array/RemoveDuplicatesFromSortedArray.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ class RemoveDuplicatesFromSortedArray {
1313

1414
var lastIndex = 0
1515

16-
for i in 1...nums.count - 1 {
17-
if nums[i] != nums[lastIndex] {
16+
for num in nums {
17+
if num != nums[lastIndex] {
1818
lastIndex += 1
19-
nums[lastIndex] = nums[i]
19+
nums[lastIndex] = num
2020
}
2121
}
2222

Array/RemoveDuplicatesFromSortedArrayII.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class RemoveDuplicatesFromSortedArrayII {
1414
}
1515

1616
var lastIndex = 1
17-
for i in 2...nums.count - 1 {
17+
for i in 2 ..< nums.count {
1818
if nums[lastIndex] != nums[i] || nums[lastIndex] != nums[lastIndex - 1] {
1919
lastIndex += 1
2020
nums[lastIndex] = nums[i]

Array/RemoveElement.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,9 @@ class RemoveElement {
99
func removeElement(inout nums: [Int], _ val: Int) -> Int {
1010
var lastIndex = 0
1111

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]
12+
for num in nums {
13+
if num != val {
14+
nums[lastIndex] = num
1915
lastIndex += 1
2016
}
2117
}

Array/SpiralMatrix.swift

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/spiral-matrix/
3+
* Primary idea: Use four index to get the right element during iteration
4+
*
5+
* Time Complexity: O(n^2), Space Complexity: O(1)
6+
*/
7+
8+
class SpiralMatrix {
9+
func spiralOrder(matrix: [[Int]]) -> [Int] {
10+
var res = [Int]()
11+
12+
guard matrix.count != 0 else {
13+
return res
14+
}
15+
16+
var startX = 0
17+
var endX = matrix.count - 1
18+
var startY = 0
19+
var endY = matrix[0].count - 1
20+
21+
while true {
22+
// top
23+
for i in startY ... endY {
24+
res.append(matrix[startX][i])
25+
}
26+
startX += 1
27+
if startX > endX {
28+
break
29+
}
30+
31+
// right
32+
for i in startX ... endX {
33+
res.append(matrix[i][endY])
34+
}
35+
endY -= 1
36+
if startY > endY {
37+
break
38+
}
39+
40+
// bottom
41+
for i in endY.stride(through: startY, by: -1) {
42+
res.append(matrix[endX][i])
43+
}
44+
endX -= 1
45+
if startX > endX {
46+
break
47+
}
48+
49+
// left
50+
for i in endX.stride(through: startX, by: -1) {
51+
res.append(matrix[i][startY])
52+
}
53+
startY += 1
54+
if startY > endY {
55+
break
56+
}
57+
}
58+
59+
return res
60+
}
61+
}

Array/ThreeSum.swift

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/3sum/
3+
* Primary idea: Sort the array, and traverse it, increment left or decrease right
4+
* predicated on their sum is greater or not than the target
5+
* Time Complexity: O(n^2), Space Complexity: O(n)
6+
*/
7+
8+
class ThreeSum {
9+
func threeSum(nums: [Int]) -> [[Int]] {
10+
var nums = nums.sort({$0 < $1})
11+
var res = [[Int]]()
12+
13+
if nums.count <= 2 {
14+
return res
15+
}
16+
17+
for i in 0 ... nums.count - 3 {
18+
if i == 0 || nums[i] != nums[i - 1] {
19+
var remain = -nums[i]
20+
var left = i + 1
21+
var right = nums.count - 1
22+
while left < right {
23+
if nums[left] + nums[right] == remain {
24+
var temp = [Int]()
25+
temp.append(nums[i])
26+
temp.append(nums[left])
27+
temp.append(nums[right])
28+
29+
res.append(temp)
30+
repeat {
31+
left += 1
32+
} while (left < right && nums[left] == nums[left - 1])
33+
repeat {
34+
right -= 1
35+
} while (left < right && nums[right] == nums[right + 1])
36+
} else if nums[left] + nums[right] < remain {
37+
repeat {
38+
left += 1
39+
} while (left < right && nums[left] == nums[left - 1])
40+
} else {
41+
repeat {
42+
right -= 1
43+
} while (left < right && nums[right] == nums[right + 1])
44+
}
45+
}
46+
}
47+
}
48+
49+
return res
50+
}
51+
}

0 commit comments

Comments
 (0)