File tree 3 files changed +96
-0
lines changed
3 files changed +96
-0
lines changed Original file line number Diff line number Diff line change @@ -138,6 +138,39 @@ func bestLine(points [][]int) []int {
138
138
}
139
139
```
140
140
141
+ ``` swift
142
+ class Solution {
143
+ func bestLine (_ points : [[Int ]]) -> [Int ] {
144
+ let n = points.count
145
+ var maxCount = 0
146
+ var answer = [Int ](repeating : 0 , count : 2 )
147
+
148
+ for i in 0 ..< n {
149
+ let x1 = points[i][0 ], y1 = points[i][1 ]
150
+ for j in i + 1 ..< n {
151
+ let x2 = points[j][0 ], y2 = points[j][1 ]
152
+ var count = 2
153
+
154
+ for k in j + 1 ..< n {
155
+ let x3 = points[k][0 ], y3 = points[k][1 ]
156
+ let a = (y2 - y1) * (x3 - x1)
157
+ let b = (y3 - y1) * (x2 - x1)
158
+ if a == b {
159
+ count += 1
160
+ }
161
+ }
162
+
163
+ if maxCount < count {
164
+ maxCount = count
165
+ answer = [i, j]
166
+ }
167
+ }
168
+ }
169
+ return answer
170
+ }
171
+ }
172
+ ```
173
+
141
174
<!-- tabs: end -->
142
175
143
176
### 方法二:枚举 + 哈希表
Original file line number Diff line number Diff line change @@ -142,6 +142,39 @@ func bestLine(points [][]int) []int {
142
142
}
143
143
```
144
144
145
+ ``` swift
146
+ class Solution {
147
+ func bestLine (_ points : [[Int ]]) -> [Int ] {
148
+ let n = points.count
149
+ var maxCount = 0
150
+ var answer = [Int ](repeating : 0 , count : 2 )
151
+
152
+ for i in 0 ..< n {
153
+ let x1 = points[i][0 ], y1 = points[i][1 ]
154
+ for j in i + 1 ..< n {
155
+ let x2 = points[j][0 ], y2 = points[j][1 ]
156
+ var count = 2
157
+
158
+ for k in j + 1 ..< n {
159
+ let x3 = points[k][0 ], y3 = points[k][1 ]
160
+ let a = (y2 - y1) * (x3 - x1)
161
+ let b = (y3 - y1) * (x2 - x1)
162
+ if a == b {
163
+ count += 1
164
+ }
165
+ }
166
+
167
+ if maxCount < count {
168
+ maxCount = count
169
+ answer = [i, j]
170
+ }
171
+ }
172
+ }
173
+ return answer
174
+ }
175
+ }
176
+ ```
177
+
145
178
<!-- tabs: end -->
146
179
147
180
### Solution 2: Enumeration + Hash Table
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ func bestLine( _ points: [ [ Int ] ] ) -> [ Int ] {
3
+ let n = points. count
4
+ var maxCount = 0
5
+ var answer = [ Int] ( repeating: 0 , count: 2 )
6
+
7
+ for i in 0 ..< n {
8
+ let x1 = points [ i] [ 0 ] , y1 = points [ i] [ 1 ]
9
+ for j in i + 1 ..< n {
10
+ let x2 = points [ j] [ 0 ] , y2 = points [ j] [ 1 ]
11
+ var count = 2
12
+
13
+ for k in j + 1 ..< n {
14
+ let x3 = points [ k] [ 0 ] , y3 = points [ k] [ 1 ]
15
+ let a = ( y2 - y1) * ( x3 - x1)
16
+ let b = ( y3 - y1) * ( x2 - x1)
17
+ if a == b {
18
+ count += 1
19
+ }
20
+ }
21
+
22
+ if maxCount < count {
23
+ maxCount = count
24
+ answer = [ i, j]
25
+ }
26
+ }
27
+ }
28
+ return answer
29
+ }
30
+ }
You can’t perform that action at this time.
0 commit comments