Intuition
- Each two points can lie on one line, use a map to save all the lines, key is the line(no matter the form you save, here is using the string(a_b) to identify the line), value is all the index of the points.
solution
|
|
problem
WA.
reason
The points may be duplicate.
modification
- When two points are the same, continue.
- When meet a new point on a line, add the count of the point to the point-number of the line at once. Mark this point is added of this line.
|
|
problem
WA.
reason
Miss the situation that all the points are the same.
modification
add the case of all points are the same.
|
|
problem
WA.
Input:
[[0,0],[94911151,94911150],[94911152,94911151]]
Output:
3
Expected:
2
reason
Big number, the precision is not enough.
modification
Here’s the answer found on the net.I’m still confused by the solution.
- To get rid of the precision problems, we treat slope as pair ((y2 – y1), (x2 – x1)) instead of ratio and reduce pair by their gcd before inserting into map. In below code points which are vertical or repeated are treated separately.
|
|