149:直线上最多的点数
This commit is contained in:
parent
947bac1e95
commit
7dc591288c
87
src/main/java/leetcode/editor/cn/MaxPointsOnALine.java
Normal file
87
src/main/java/leetcode/editor/cn/MaxPointsOnALine.java
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
//给你一个数组 points ,其中 points[i] = [xi, yi] 表示 X-Y 平面上的一个点。求最多有多少个点在同一条直线上。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:points = [[1,1],[2,2],[3,3]]
|
||||||
|
//输出:3
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
|
||||||
|
//输出:4
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 1 <= points.length <= 300
|
||||||
|
// points[i].length == 2
|
||||||
|
// -104 <= xi, yi <= 104
|
||||||
|
// points 中的所有点 互不相同
|
||||||
|
//
|
||||||
|
// Related Topics 几何 哈希表 数学
|
||||||
|
// 👍 257 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
//149:直线上最多的点数
|
||||||
|
public class MaxPointsOnALine {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new MaxPointsOnALine().new Solution();
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public int maxPoints(int[][] points) {
|
||||||
|
Map<Map<Double, Double>, List<Integer>> map = new HashMap<>();
|
||||||
|
int count = 1;
|
||||||
|
for (int i = 0; i < points.length; i++) {
|
||||||
|
for (int j = i + 1; j < points.length; j++) {
|
||||||
|
double sub = points[j][0] - points[i][0];
|
||||||
|
double x, y;
|
||||||
|
if (sub == 0) {
|
||||||
|
x = points[i][0];
|
||||||
|
y = Integer.MAX_VALUE;
|
||||||
|
} else {
|
||||||
|
x = (points[j][1] - points[i][1]) / sub;
|
||||||
|
y = (points[j][0] * points[i][1] - points[i][0] * points[j][1]) / sub;
|
||||||
|
}
|
||||||
|
Map<Double, Double> key = new HashMap<>();
|
||||||
|
key.put(x, y);
|
||||||
|
List<Integer> list = new ArrayList<>();
|
||||||
|
if (map.containsKey(key)) {
|
||||||
|
list = map.get(key);
|
||||||
|
if (!list.contains(i)) {
|
||||||
|
list.add(i);
|
||||||
|
}
|
||||||
|
if (!list.contains(j)) {
|
||||||
|
list.add(j);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
list.add(i);
|
||||||
|
list.add(j);
|
||||||
|
}
|
||||||
|
count = Math.max(count, list.size());
|
||||||
|
map.put(key, list);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
29
src/main/java/leetcode/editor/cn/MaxPointsOnALine.md
Normal file
29
src/main/java/leetcode/editor/cn/MaxPointsOnALine.md
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
<p>给你一个数组 <code>points</code> ,其中 <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> 表示 <strong>X-Y</strong> 平面上的一个点。求最多有多少个点在同一条直线上。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>points = [[1,1],[2,2],[3,3]]
|
||||||
|
<strong>输出:</strong>3
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
|
||||||
|
<strong>输出:</strong>4
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>1 <= points.length <= 300</code></li>
|
||||||
|
<li><code>points[i].length == 2</code></li>
|
||||||
|
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
|
||||||
|
<li><code>points</code> 中的所有点 <strong>互不相同</strong></li>
|
||||||
|
</ul>
|
||||||
|
<div><div>Related Topics</div><div><li>几何</li><li>哈希表</li><li>数学</li></div></div>\n<div><li>👍 257</li><li>👎 0</li></div>
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user