Skip to content

Commit 813b46e

Browse files
committed
Fix code style about for loop and sorted by func
1 parent c0e09ca commit 813b46e

File tree

98 files changed

+193
-193
lines changed

Some content is hidden

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

98 files changed

+193
-193
lines changed

Array/ContainsDuplicateII.swift

+1-1
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 {
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

Array/FourSum.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
*/
77

88
class FourSum {
9-
func fourSum(nums: [Int], _ target: Int) -> [[Int]] {
10-
let nums = nums.sort({$0 < $1})
9+
func fourSum(_ nums: [Int], _ target: Int) -> [[Int]] {
10+
let nums = nums.sorted(by: <)
1111
var threeSum = 0
1212
var twoSum = 0
1313
var left = 0
@@ -18,13 +18,13 @@ class FourSum {
1818
return res
1919
}
2020

21-
for i in 0 ..< nums.count - 3 {
21+
for i in 0..<nums.count - 3 {
2222
guard i == 0 || nums[i] != nums[i - 1] else {
2323
continue
2424
}
2525
threeSum = target - nums[i]
2626

27-
for j in i + 1 ..< nums.count - 2 {
27+
for j in i + 1..<nums.count - 2 {
2828
guard j == i + 1 || nums[j] != nums[j - 1] else {
2929
continue
3030
}

Array/GameLife.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ class GameLife {
1515

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

18-
for i in 0 ..< m {
19-
for j in 0 ..< n {
18+
for i in 0..<m {
19+
for j in 0..<n {
2020
changeStatus(&board, i, j, m, n)
2121
}
2222
}
@@ -27,8 +27,8 @@ class GameLife {
2727
private func changeStatus(_ board: inout [[Int]], _ i: Int, _ j: Int, _ m: Int, _ n: Int) {
2828
var liveNum = 0
2929

30-
for x in i - 1 ... i + 1 {
31-
for y in j - 1 ... j + 1 {
30+
for x in i - 1...i + 1 {
31+
for y in j - 1...j + 1 {
3232
if x < 0 || x >= m || y < 0 || y >= n {
3333
continue
3434
}

Array/IntersectionTwoArraysII.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

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

1515
var i = 0
1616
var j = 0

Array/ProductExceptSelf.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class ProductExceptSelf {
1616
let left = _initLeft(nums)
1717
let right = _initRight(nums)
1818

19-
for i in 0 ..< nums.count {
19+
for i in 0..<nums.count {
2020
res.append(left[i] * right[i])
2121
}
2222

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

30-
for i in 1 ..< nums.count {
30+
for i in 1..<nums.count {
3131
left.append(left[i - 1] * nums[i - 1])
3232
}
3333

Array/RemoveDuplicatesFromSortedArrayII.swift

+1-1
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 {
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/RotateImage.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ class RotateImage {
99
func rotate(_ matrix: inout [[Int]]) {
1010
let n = matrix.count
1111

12-
for layer in 0 ..< n / 2 {
12+
for layer in 0..<n / 2 {
1313
let start = layer, end = n - layer - 1
14-
for i in start ..< end {
14+
for i in start..<end {
1515
let offset = i - start
1616

1717
(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])

Array/SetMatrixZeroes.swift

+8-8
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,41 @@ class SetMatrixZeroes {
1111
var rowHasZero = false, colHasZero = false
1212
let m = matrix.count, n = matrix[0].count
1313

14-
for i in 0 ..< m where matrix[i][0] == 0 {
14+
for i in 0..<m where matrix[i][0] == 0 {
1515
colHasZero = true
1616
break
1717
}
1818

19-
for i in 0 ..< n where matrix[0][i] == 0 {
19+
for i in 0..<n where matrix[0][i] == 0 {
2020
rowHasZero = true
2121
break
2222
}
2323

24-
for i in 1 ..< m {
25-
for j in 1 ..< n {
24+
for i in 1..<m {
25+
for j in 1..<n {
2626
if matrix[i][j] == 0 {
2727
matrix[0][j] = 0
2828
matrix[i][0] = 0
2929
}
3030
}
3131
}
3232

33-
for i in 1 ..< m {
34-
for j in 1 ..< n {
33+
for i in 1..<m {
34+
for j in 1..<n {
3535
if matrix[0][j] == 0 || matrix[i][0] == 0 {
3636
matrix[i][j] = 0
3737
}
3838
}
3939
}
4040

4141
if rowHasZero {
42-
for i in 0 ..< n {
42+
for i in 0..<n {
4343
matrix[0][i] = 0
4444
}
4545
}
4646

4747
if colHasZero {
48-
for i in 0 ..< m {
48+
for i in 0..<m {
4949
matrix[i][0] = 0
5050
}
5151
}

Array/SlidingWindowMaximum.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class SlidingWindowMaximum {
1212
var maxIdx = [Int]()
1313
var res = [Int]()
1414

15-
for i in 0 ..< nums.count {
15+
for i in 0..<nums.count {
1616
while maxIdx.count > 0 && nums[maxIdx.last!] < nums[i] {
1717
maxIdx.removeLast()
1818
}

Array/SpiralMatrix.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class SpiralMatrix {
2020

2121
while true {
2222
// top
23-
for i in startY ... endY {
23+
for i in startY...endY {
2424
res.append(matrix[startX][i])
2525
}
2626
startX += 1
@@ -29,7 +29,7 @@ class SpiralMatrix {
2929
}
3030

3131
// right
32-
for i in startX ... endX {
32+
for i in startX...endX {
3333
res.append(matrix[i][endY])
3434
}
3535
endY -= 1

Array/SpiralMatrixII.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@ class SpiralMatrixII {
1414
var num = 1
1515
var res = Array(repeating: Array(repeating: 0, count: n), count: n)
1616

17-
for layer in 0 ..< n / 2 {
17+
for layer in 0..<n / 2 {
1818
let start = layer
1919
let end = n - layer - 1
2020

2121
// top
22-
for i in start ..< end {
22+
for i in start..<end {
2323
res[start][i] = num
2424
num += 1
2525
}
2626

2727
// right
28-
for i in start ..< end {
28+
for i in start..<end {
2929
res[i][end] = num
3030
num += 1
3131
}

Array/SummaryRanges.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class SummaryRanges {
1616
return res
1717
}
1818

19-
for i in 0 ... nums.count {
19+
for i in 0...nums.count {
2020
if i == nums.count || (i > 0 && nums[i] != nums[i - 1] + 1) {
2121
str = "\(nums[start])"
2222
if i - 1 != start {

Array/ThreeSum.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77

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

1313
if nums.count <= 2 {
1414
return res
1515
}
1616

17-
for i in 0 ... nums.count - 3 {
17+
for i in 0...nums.count - 3 {
1818
if i == 0 || nums[i] != nums[i - 1] {
1919
var remain = -nums[i]
2020
var left = i + 1

Array/ThreeSumClosest.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
let nums = nums.sorted()
1414

15-
for i in 0 ..< nums.count - 2 {
15+
for i in 0..<nums.count - 2 {
1616
if i == 0 || nums[i] != nums[i - 1] {
1717
let twoSum = target - nums[i]
1818
var left = i + 1

Array/ValidSudoku.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ class ValidSudoku {
1515
private func _isRowValid(board: [[Character]]) -> Bool {
1616
var visited = Array(count: size, repeatedValue: false)
1717

18-
for i in 0 ..< size {
18+
for i in 0..<size {
1919
visited = Array(count: size, repeatedValue: false)
20-
for j in 0 ..< size {
20+
for j in 0..<size {
2121
if !_isValidChar(board[i][j], &visited) {
2222
return false
2323
}
@@ -30,9 +30,9 @@ class ValidSudoku {
3030
private func _isColValid(board: [[Character]]) -> Bool {
3131
var visited = Array(count: size, repeatedValue: false)
3232

33-
for i in 0 ..< size {
33+
for i in 0..<size {
3434
visited = Array(count: size, repeatedValue: false)
35-
for j in 0 ..< size {
35+
for j in 0..<size {
3636
if !_isValidChar(board[j][i], &visited) {
3737
return false
3838
}
@@ -48,8 +48,8 @@ class ValidSudoku {
4848
for i in 0.stride(to: size, by: 3) {
4949
for j in 0.stride(to: size, by: 3) {
5050
visited = Array(count: size, repeatedValue: false)
51-
for m in i ..< i + 3 {
52-
for n in j ..< j + 3 {
51+
for m in i..<i + 3 {
52+
for n in j..<j + 3 {
5353
if !_isValidChar(board[m][n], &visited) {
5454
return false
5555
}

DFS/CombinationSum.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class CombinationSum {
1111
var res = [[Int]]()
1212
var path = [Int]()
1313

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

1616
return res
1717
}
@@ -22,7 +22,7 @@ class CombinationSum {
2222
return
2323
}
2424

25-
for i in index ..< candidates.count {
25+
for i in index..<candidates.count {
2626
guard candidates[i] <= target else {
2727
break
2828
}

DFS/CombinationSumII.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class CombinationSumII {
1111
var res = [[Int]]()
1212
var path = [Int]()
1313

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

1616
return res
1717
}
@@ -22,7 +22,7 @@ class CombinationSumII {
2222
return
2323
}
2424

25-
for i in index ..< candidates.count {
25+
for i in index..<candidates.count {
2626
guard candidates[i] <= target else {
2727
break
2828
}

DFS/CombinationSumIII.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class CombinationSumIII {
1010
func combinationSum3(k: Int, _ n: Int) -> [[Int]] {
11-
let candidates = [Int](1 ... 9)
11+
let candidates = [Int](1...9)
1212
var res = [[Int]]()
1313
var path = [Int]()
1414

@@ -23,7 +23,7 @@ class CombinationSumIII {
2323
return
2424
}
2525

26-
for i in index ..< candidates.count {
26+
for i in index..<candidates.count {
2727
guard candidates[i] <= target else {
2828
break
2929
}

DFS/Combinations.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Combinations {
1010
func combine(n: Int, _ k: Int) -> [[Int]] {
1111
var res = [[Int]]()
1212
var path = [Int]()
13-
let nums = [Int](1 ... n)
13+
let nums = [Int](1...n)
1414

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

@@ -23,7 +23,7 @@ class Combinations {
2323
return
2424
}
2525

26-
for i in index ..< nums.count {
26+
for i in index..<nums.count {
2727
path.append(nums[i])
2828
_dfs(nums, &res, &path, i + 1, k)
2929
path.removeLast()

DFS/CourseSchedule.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class CourseSchedule {
2020
var indegree = [Int](repeatElement(0, count: numCourses))
2121

2222
// 1. Create the graph
23-
for i in 0 ..< prerequisites.count {
23+
for i in 0..<prerequisites.count {
2424
let course = prerequisites[i][0]
2525
let pre = prerequisites[i][1]
2626

@@ -34,7 +34,7 @@ class CourseSchedule {
3434

3535
// 3. Create a array of sources
3636
var sources = [Int]()
37-
for i in 0 ..< numCourses {
37+
for i in 0..<numCourses {
3838
if indegree[i] == 0 {
3939
sources.append(i)
4040
}
@@ -49,7 +49,7 @@ class CourseSchedule {
4949
count += 1
5050

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

0 commit comments

Comments
 (0)