Skip to content

Commit

Permalink
Optimize code style by Swifter way
Browse files Browse the repository at this point in the history
  • Loading branch information
Yi Gu committed Jan 24, 2017
1 parent 79fa129 commit c245e0f
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 43 deletions.
16 changes: 6 additions & 10 deletions Array/SetMatrixZeroes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,14 @@ class SetMatrixZeroes {
var rowHasZero = false, colHasZero = false
let m = matrix.count, n = matrix[0].count

for i in 0 ..< m {
if matrix[i][0] == 0 {
colHasZero = true
break
}
for i in 0 ..< m where matrix[i][0] == 0 {
colHasZero = true
break
}

for i in 0 ..< n {
if matrix[0][i] == 0 {
rowHasZero = true
break
}
for i in 0 ..< n where matrix[0][i] == 0 {
rowHasZero = true
break
}

for i in 1 ..< m {
Expand Down
10 changes: 4 additions & 6 deletions DFS/FactorCombinations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@ class FactorCombinations {
return
}

for i in start ... n {
if n % i == 0 {
path.append(i)
dfs(&res, &path, n / i, i)
path.removeLast()
}
for i in start ... n where n % i == 0 {
path.append(i)
dfs(&res, &path, n / i, i)
path.removeLast()
}
}
}
29 changes: 11 additions & 18 deletions DFS/Permutations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,27 @@
*/

class Permutations {
func permute(nums: [Int]) -> [[Int]] {
func permute(_ nums: [Int]) -> [[Int]] {
var res = [[Int]]()
var path = [Int]()
var visited = [Bool](count: nums.count, repeatedValue: false)
var isVisited = [Bool](repeating: false, count: nums.count)

let nums = nums.sort({$0 < $1})

_dfs(&res, &path, nums, &visited)
dfs(&res, &path, &isVisited, nums)

return res
}

private func _dfs(inout res: [[Int]], inout _ path: [Int], _ nums: [Int], inout _ visited: [Bool]) {
// termination case
if path.count == nums.count {
res.append(Array(path))
private func dfs(_ res: inout [[Int]], _ path: inout [Int], _ isVisited: inout [Bool], _ nums: [Int]) {
guard path.count != nums.count else {
res.append(path)
return
}

for i in 0 ..< nums.count {
guard !visited[i] else {
continue
}

path.append(nums[i])
visited[i] = true
_dfs(&res, &path, nums, &visited)
visited[i] = false
for (i, num) in nums.enumerated() where !isVisited[i] {
path.append(num)
isVisited[i] = true
dfs(&res, &path, &isVisited, nums)
isVisited[i] = false
path.removeLast()
}
}
Expand Down
6 changes: 2 additions & 4 deletions DP/BestTimeBuySellStockII.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
return max
}

for i in 1 ..< prices.count {
if prices[i] > prices[i - 1] {
max += prices[i] - prices[i - 1]
}
for i in 1 ..< prices.count where prices[i] > prices[i - 1] {
max += prices[i] - prices[i - 1]
}

return max
Expand Down
8 changes: 3 additions & 5 deletions Tree/ConstructBinaryTreeInorderPostorder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,9 @@ class ConstructBinaryTreeInorderPostorder {
let root = TreeNode(postorder[postEnd])

var mid = 0
for i in inStart ... inEnd {
if inorder[i] == root.val {
mid = i
break
}
for i in inStart ... inEnd where inorder[i] == root.val {
mid = i
break
}

root.left = _buildHelper(inorder, inStart, mid - 1, postorder, postStart, mid - 1 - inStart + postStart)
Expand Down

0 comments on commit c245e0f

Please sign in to comment.