Skip to content

Commit

Permalink
Fix code style about for loop and sorted by func
Browse files Browse the repository at this point in the history
  • Loading branch information
soapyigu committed Aug 13, 2017
1 parent c0e09ca commit 813b46e
Show file tree
Hide file tree
Showing 98 changed files with 193 additions and 193 deletions.
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 {
for i in 0..<nums.count {
guard let index = dict[nums[i]] where i - index <= k else {
dict[nums[i]] = i
continue
Expand Down
8 changes: 4 additions & 4 deletions Array/FourSum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*/

class FourSum {
func fourSum(nums: [Int], _ target: Int) -> [[Int]] {
let nums = nums.sort({$0 < $1})
func fourSum(_ nums: [Int], _ target: Int) -> [[Int]] {
let nums = nums.sorted(by: <)
var threeSum = 0
var twoSum = 0
var left = 0
Expand All @@ -18,13 +18,13 @@ class FourSum {
return res
}

for i in 0 ..< nums.count - 3 {
for i in 0..<nums.count - 3 {
guard i == 0 || nums[i] != nums[i - 1] else {
continue
}
threeSum = target - nums[i]

for j in i + 1 ..< nums.count - 2 {
for j in i + 1..<nums.count - 2 {
guard j == i + 1 || nums[j] != nums[j - 1] else {
continue
}
Expand Down
8 changes: 4 additions & 4 deletions Array/GameLife.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class GameLife {

let m = board.count, n = board[0].count

for i in 0 ..< m {
for j in 0 ..< n {
for i in 0..<m {
for j in 0..<n {
changeStatus(&board, i, j, m, n)
}
}
Expand All @@ -27,8 +27,8 @@ class GameLife {
private func changeStatus(_ board: inout [[Int]], _ i: Int, _ j: Int, _ m: Int, _ n: Int) {
var liveNum = 0

for x in i - 1 ... i + 1 {
for y in j - 1 ... j + 1 {
for x in i - 1...i + 1 {
for y in j - 1...j + 1 {
if x < 0 || x >= m || y < 0 || y >= n {
continue
}
Expand Down
4 changes: 2 additions & 2 deletions Array/IntersectionTwoArraysII.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

class IntersectionTwoArraysII {
func intersect(nums1: [Int], _ nums2: [Int]) -> [Int] {
var nums1 = nums1.sort({$0 < $1})
var nums2 = nums2.sort({$0 < $1})
var nums1 = nums1.sorted(by: <)
var nums2 = nums2.sorted(by: <)

var i = 0
var j = 0
Expand Down
4 changes: 2 additions & 2 deletions Array/ProductExceptSelf.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ProductExceptSelf {
let left = _initLeft(nums)
let right = _initRight(nums)

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

Expand All @@ -27,7 +27,7 @@ class ProductExceptSelf {
var left = [Int]()
left.append(1)

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

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 {
for i in 2..<nums.count {
if nums[lastIndex] != nums[i] || nums[lastIndex] != nums[lastIndex - 1] {
lastIndex += 1
nums[lastIndex] = nums[i]
Expand Down
4 changes: 2 additions & 2 deletions Array/RotateImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ class RotateImage {
func rotate(_ matrix: inout [[Int]]) {
let n = matrix.count

for layer in 0 ..< n / 2 {
for layer in 0..<n / 2 {
let start = layer, end = n - layer - 1
for i in start ..< end {
for i in start..<end {
let offset = i - start

(matrix[start][i], matrix[i][end], matrix[end][end - offset], matrix[end - offset][start]) = (matrix[end - offset][start], matrix[start][i], matrix[i][end], matrix[end][end - offset])
Expand Down
16 changes: 8 additions & 8 deletions Array/SetMatrixZeroes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,41 @@ class SetMatrixZeroes {
var rowHasZero = false, colHasZero = false
let m = matrix.count, n = matrix[0].count

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

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

for i in 1 ..< m {
for j in 1 ..< n {
for i in 1..<m {
for j in 1..<n {
if matrix[i][j] == 0 {
matrix[0][j] = 0
matrix[i][0] = 0
}
}
}

for i in 1 ..< m {
for j in 1 ..< n {
for i in 1..<m {
for j in 1..<n {
if matrix[0][j] == 0 || matrix[i][0] == 0 {
matrix[i][j] = 0
}
}
}

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

if colHasZero {
for i in 0 ..< m {
for i in 0..<m {
matrix[i][0] = 0
}
}
Expand Down
2 changes: 1 addition & 1 deletion Array/SlidingWindowMaximum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class SlidingWindowMaximum {
var maxIdx = [Int]()
var res = [Int]()

for i in 0 ..< nums.count {
for i in 0..<nums.count {
while maxIdx.count > 0 && nums[maxIdx.last!] < nums[i] {
maxIdx.removeLast()
}
Expand Down
4 changes: 2 additions & 2 deletions Array/SpiralMatrix.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class SpiralMatrix {

while true {
// top
for i in startY ... endY {
for i in startY...endY {
res.append(matrix[startX][i])
}
startX += 1
Expand All @@ -29,7 +29,7 @@ class SpiralMatrix {
}

// right
for i in startX ... endX {
for i in startX...endX {
res.append(matrix[i][endY])
}
endY -= 1
Expand Down
6 changes: 3 additions & 3 deletions Array/SpiralMatrixII.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ class SpiralMatrixII {
var num = 1
var res = Array(repeating: Array(repeating: 0, count: n), count: n)

for layer in 0 ..< n / 2 {
for layer in 0..<n / 2 {
let start = layer
let end = n - layer - 1

// top
for i in start ..< end {
for i in start..<end {
res[start][i] = num
num += 1
}

// right
for i in start ..< end {
for i in start..<end {
res[i][end] = num
num += 1
}
Expand Down
2 changes: 1 addition & 1 deletion Array/SummaryRanges.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class SummaryRanges {
return res
}

for i in 0 ... nums.count {
for i in 0...nums.count {
if i == nums.count || (i > 0 && nums[i] != nums[i - 1] + 1) {
str = "\(nums[start])"
if i - 1 != start {
Expand Down
4 changes: 2 additions & 2 deletions Array/ThreeSum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

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

if nums.count <= 2 {
return res
}

for i in 0 ... nums.count - 3 {
for i in 0...nums.count - 3 {
if i == 0 || nums[i] != nums[i - 1] {
var remain = -nums[i]
var left = i + 1
Expand Down
2 changes: 1 addition & 1 deletion Array/ThreeSumClosest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

let nums = nums.sorted()

for i in 0 ..< nums.count - 2 {
for i in 0..<nums.count - 2 {
if i == 0 || nums[i] != nums[i - 1] {
let twoSum = target - nums[i]
var left = i + 1
Expand Down
12 changes: 6 additions & 6 deletions Array/ValidSudoku.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ class ValidSudoku {
private func _isRowValid(board: [[Character]]) -> Bool {
var visited = Array(count: size, repeatedValue: false)

for i in 0 ..< size {
for i in 0..<size {
visited = Array(count: size, repeatedValue: false)
for j in 0 ..< size {
for j in 0..<size {
if !_isValidChar(board[i][j], &visited) {
return false
}
Expand All @@ -30,9 +30,9 @@ class ValidSudoku {
private func _isColValid(board: [[Character]]) -> Bool {
var visited = Array(count: size, repeatedValue: false)

for i in 0 ..< size {
for i in 0..<size {
visited = Array(count: size, repeatedValue: false)
for j in 0 ..< size {
for j in 0..<size {
if !_isValidChar(board[j][i], &visited) {
return false
}
Expand All @@ -48,8 +48,8 @@ class ValidSudoku {
for i in 0.stride(to: size, by: 3) {
for j in 0.stride(to: size, by: 3) {
visited = Array(count: size, repeatedValue: false)
for m in i ..< i + 3 {
for n in j ..< j + 3 {
for m in i..<i + 3 {
for n in j..<j + 3 {
if !_isValidChar(board[m][n], &visited) {
return false
}
Expand Down
4 changes: 2 additions & 2 deletions DFS/CombinationSum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class CombinationSum {
var res = [[Int]]()
var path = [Int]()

_dfs(candidates.sort({$0 < $1}), target, &res, &path, 0)
_dfs(candidates.sorted(by: <), target, &res, &path, 0)

return res
}
Expand All @@ -22,7 +22,7 @@ class CombinationSum {
return
}

for i in index ..< candidates.count {
for i in index..<candidates.count {
guard candidates[i] <= target else {
break
}
Expand Down
4 changes: 2 additions & 2 deletions DFS/CombinationSumII.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class CombinationSumII {
var res = [[Int]]()
var path = [Int]()

_dfs(&res, &path, target, candidates.sort({$0 < $1}), 0)
_dfs(&res, &path, target, candidates.sorted(by: <), 0)

return res
}
Expand All @@ -22,7 +22,7 @@ class CombinationSumII {
return
}

for i in index ..< candidates.count {
for i in index..<candidates.count {
guard candidates[i] <= target else {
break
}
Expand Down
4 changes: 2 additions & 2 deletions DFS/CombinationSumIII.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class CombinationSumIII {
func combinationSum3(k: Int, _ n: Int) -> [[Int]] {
let candidates = [Int](1 ... 9)
let candidates = [Int](1...9)
var res = [[Int]]()
var path = [Int]()

Expand All @@ -23,7 +23,7 @@ class CombinationSumIII {
return
}

for i in index ..< candidates.count {
for i in index..<candidates.count {
guard candidates[i] <= target else {
break
}
Expand Down
4 changes: 2 additions & 2 deletions DFS/Combinations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Combinations {
func combine(n: Int, _ k: Int) -> [[Int]] {
var res = [[Int]]()
var path = [Int]()
let nums = [Int](1 ... n)
let nums = [Int](1...n)

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

Expand All @@ -23,7 +23,7 @@ class Combinations {
return
}

for i in index ..< nums.count {
for i in index..<nums.count {
path.append(nums[i])
_dfs(nums, &res, &path, i + 1, k)
path.removeLast()
Expand Down
6 changes: 3 additions & 3 deletions DFS/CourseSchedule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CourseSchedule {
var indegree = [Int](repeatElement(0, count: numCourses))

// 1. Create the graph
for i in 0 ..< prerequisites.count {
for i in 0..<prerequisites.count {
let course = prerequisites[i][0]
let pre = prerequisites[i][1]

Expand All @@ -34,7 +34,7 @@ class CourseSchedule {

// 3. Create a array of sources
var sources = [Int]()
for i in 0 ..< numCourses {
for i in 0..<numCourses {
if indegree[i] == 0 {
sources.append(i)
}
Expand All @@ -49,7 +49,7 @@ class CourseSchedule {
count += 1

// 4.ii. Decrement the in-degree of the destination node
for i in 0 ..< numCourses {
for i in 0..<numCourses {
if graph[source!][i] == 1 {
indegree[i] -= 1
// Check all of its destination vertices and add them to the set if they have no incoming edges
Expand Down
Loading

0 comments on commit 813b46e

Please sign in to comment.