Skip to content

Commit

Permalink
Merge branch 'master' into Sort
Browse files Browse the repository at this point in the history
# Conflicts:
#	Sort/MeetingRooms.swift
#	Sort/MergeIntervals.swift
  • Loading branch information
Yi Gu committed Jun 12, 2016
2 parents 87d93f7 + 454ae99 commit acf5bab
Show file tree
Hide file tree
Showing 75 changed files with 2,566 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Array/ContainsDuplicate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ContainsDuplicate {

var set = Set<Int>()

for i in 0...nums.count - 1 {
for i in 0 ..< nums.count {
if set.contains(nums[i]) {
return true
} else {
Expand Down
2 changes: 1 addition & 1 deletion Array/ContainsDuplicateII.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ContainsDuplicateII {
// key: nums[index], value: index
var dict = [Int: Int]()

for i in 0...nums.count - 1 {
for i in 0 ..< nums.count {
guard let index = dict[nums[i]] where i - index <= k else {
dict[nums[i]] = i
continue
Expand Down
29 changes: 29 additions & 0 deletions Array/MaximumSizeSubarraySumEqualsK.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Question Link: https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/
* Primary idea: Use a dictionary to track the sum so far since the first until the current
* Time Complexity: O(n), Space Complexity: O(n)
*/

class MaximumSizeSubarraySumEqualsK {
func maxSubArrayLen(nums: [Int], _ k: Int) -> Int {
var longestLen = 0
var dict = [Int: Int]()
dict[0] = -1
var sum = 0

for i in 0 ..< nums.count {
sum += nums[i]

if let lastIndex = dict[sum - k] {
longestLen = max(longestLen, i - lastIndex)
}

guard let index = dict[sum] else {
dict[sum] = i
continue
}
}

return longestLen
}
}
9 changes: 3 additions & 6 deletions Array/MoveZeroes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@
class MoveZeroes {
func moveZeroes(inout nums: [Int]) {
var zeroIndex = 0
var left = 0
let right = nums.count

while left < right {
if nums[left] != 0 {
_swap(&nums, zeroIndex, left)
for i in 0 ..< nums.count {
if nums[i] != 0 {
_swap(&nums, zeroIndex, i)
zeroIndex += 1
}
left += 1
}
}

Expand Down
46 changes: 46 additions & 0 deletions Array/ProductExceptSelf.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Question Link: https://leetcode.com/problems/product-of-array-except-self/
* Primary idea: Use two arrays to hold multiplication result from left and right sides
* while iterating the original array
* Time Complexity: O(n), Space Complexity: O(n)
*/

class ProductExceptSelf {
func productExceptSelf(nums: [Int]) -> [Int] {
var res = [Int]()

guard nums.count > 0 else {
return res
}

let left = _initLeft(nums)
let right = _initRight(nums)

for i in 0 ..< nums.count {
res.append(left[i] * right[i])
}

return res
}

private func _initLeft(nums: [Int]) -> [Int] {
var left = [Int]()
left.append(1)

for i in 1 ..< nums.count {
left.append(left[i - 1] * nums[i - 1])
}

return left
}

private func _initRight(nums: [Int]) -> [Int] {
var right = Array(count: nums.count, repeatedValue: 1)

for i in (nums.count - 2).stride(through: 0, by: -1) {
right[i] = right[i + 1] * nums[i + 1]
}

return right
}
}
6 changes: 3 additions & 3 deletions Array/RemoveDuplicatesFromSortedArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ class RemoveDuplicatesFromSortedArray {

var lastIndex = 0

for i in 1...nums.count - 1 {
if nums[i] != nums[lastIndex] {
for num in nums {
if num != nums[lastIndex] {
lastIndex += 1
nums[lastIndex] = nums[i]
nums[lastIndex] = num
}
}

Expand Down
2 changes: 1 addition & 1 deletion Array/RemoveDuplicatesFromSortedArrayII.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class RemoveDuplicatesFromSortedArrayII {
}

var lastIndex = 1
for i in 2...nums.count - 1 {
for i in 2 ..< nums.count {
if nums[lastIndex] != nums[i] || nums[lastIndex] != nums[lastIndex - 1] {
lastIndex += 1
nums[lastIndex] = nums[i]
Expand Down
10 changes: 3 additions & 7 deletions Array/RemoveElement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@ class RemoveElement {
func removeElement(inout nums: [Int], _ val: Int) -> Int {
var lastIndex = 0

if nums.count == 0 {
return lastIndex
}

for i in 0...nums.count - 1 {
if nums[i] != val {
nums[lastIndex] = nums[i]
for num in nums {
if num != val {
nums[lastIndex] = num
lastIndex += 1
}
}
Expand Down
61 changes: 61 additions & 0 deletions Array/SpiralMatrix.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Question Link: https://leetcode.com/problems/spiral-matrix/
* Primary idea: Use four index to get the right element during iteration
*
* Time Complexity: O(n^2), Space Complexity: O(1)
*/

class SpiralMatrix {
func spiralOrder(matrix: [[Int]]) -> [Int] {
var res = [Int]()

guard matrix.count != 0 else {
return res
}

var startX = 0
var endX = matrix.count - 1
var startY = 0
var endY = matrix[0].count - 1

while true {
// top
for i in startY ... endY {
res.append(matrix[startX][i])
}
startX += 1
if startX > endX {
break
}

// right
for i in startX ... endX {
res.append(matrix[i][endY])
}
endY -= 1
if startY > endY {
break
}

// bottom
for i in endY.stride(through: startY, by: -1) {
res.append(matrix[endX][i])
}
endX -= 1
if startX > endX {
break
}

// left
for i in endX.stride(through: startX, by: -1) {
res.append(matrix[i][startY])
}
startY += 1
if startY > endY {
break
}
}

return res
}
}
51 changes: 51 additions & 0 deletions Array/ThreeSum.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Question Link: https://leetcode.com/problems/3sum/
* Primary idea: Sort the array, and traverse it, increment left or decrease right
* predicated on their sum is greater or not than the target
* Time Complexity: O(n^2), Space Complexity: O(n)
*/

class ThreeSum {
func threeSum(nums: [Int]) -> [[Int]] {
var nums = nums.sort({$0 < $1})
var res = [[Int]]()

if nums.count <= 2 {
return res
}

for i in 0 ... nums.count - 3 {
if i == 0 || nums[i] != nums[i - 1] {
var remain = -nums[i]
var left = i + 1
var right = nums.count - 1
while left < right {
if nums[left] + nums[right] == remain {
var temp = [Int]()
temp.append(nums[i])
temp.append(nums[left])
temp.append(nums[right])

res.append(temp)
repeat {
left += 1
} while (left < right && nums[left] == nums[left - 1])
repeat {
right -= 1
} while (left < right && nums[right] == nums[right + 1])
} else if nums[left] + nums[right] < remain {
repeat {
left += 1
} while (left < right && nums[left] == nums[left - 1])
} else {
repeat {
right -= 1
} while (left < right && nums[right] == nums[right + 1])
}
}
}
}

return res
}
}
2 changes: 1 addition & 1 deletion Array/TwoSum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class TwoSum {
var res = [Int]()
var dict = [Int: Int]()

for i in 0...nums.count - 1 {
for i in 0 ..< nums.count {
guard let lastIndex = dict[target - nums[i]] else {
dict[nums[i]] = i
continue
Expand Down
41 changes: 41 additions & 0 deletions DFS/CombinationSum.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Question Link: https://leetcode.com/problems/combination-sum/
* Primary idea: Classic Depth-first Search
*
* Time Complexity: O(n^n)
*
*/

class CombinationSum {
func combinationSum(candidates: [Int], _ target: Int) -> [[Int]] {
var res = [[Int]]()

guard candidates.count > 0 else {
return res
}

var path = [Int]()
let candidates = candidates.sort({$0 < $1})

_dfs(candidates, target, &res, &path, 0)

return res
}

private func _dfs(candidates: [Int], _ target: Int, inout _ res: [[Int]], inout _ path: [Int], _ index: Int) {
if target == 0 {
res.append([Int](path))
return
}

for i in index ..< candidates.count {
guard candidates[i] <= target else {
break
}

path.append(candidates[i])
_dfs(candidates, target - candidates[i], &res, &path, i)
path.removeLast()
}
}
}
42 changes: 42 additions & 0 deletions DFS/Combinations.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Question Link: https://leetcode.com/problems/combinations/
* Primary idea: Classic Depth-first Search, another version of Subsets
*
* Time Complexity: O(n!), Space Complexity: O(n)
*
*/

class Combinations {
func combine(n: Int, _ k: Int) -> [[Int]] {
var res = [[Int]]()
var path = [Int]()
let nums = _init(n)

_dfs(nums, &res, &path, 0, k)

return res
}

private func _init(n: Int) -> [Int] {
var res = [Int]()

for i in 1 ... n {
res.append(i)
}

return res
}

private func _dfs(nums: [Int], inout _ res: [[Int]], inout _ path: [Int], _ index: Int, _ k: Int) {
if path.count == k {
res.append([Int](path))
return
}

for i in index ..< nums.count {
path.append(nums[i])
_dfs(nums, &res, &path, i + 1, k)
path.removeLast()
}
}
}
Loading

0 comments on commit acf5bab

Please sign in to comment.