【leetcode刷题笔记】Max Points on a Line

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.



题解:

思路比较简单,每条直线都可以表示为y=kx+b,所以对于任意三点,如果它们共线,那么它们中任意两点的斜率都相等。

所以就遍历points数组,对其中的每一个元素计算它和位于它后面的数组元素的斜率并保存在一个hashmap中.

这个hashmap的键就是两点构成直线的斜率,值就是和当前元素points[i]斜率等于键值k的点有多少个。

比如map中如果有一个KV值为(2.5,3),说明和点points[i]构成的直线斜率为2.5的点有三个。

找到hashmap中最大的value值,加上和点points[i]重合的点的个数以及点points[i]自己,就得到了与点points[i]共线的点的最大值。

最后在所有的点中找到这个最大值中最大的,就是答案了。

代码如下:

 1 /**
 2  * Definition for a point.
 3  * class Point {
 4  *     int x;
 5  *     int y;
 6  *     Point() { x = 0; y = 0; }
 7  *     Point(int a, int b) { x = a; y = b; }
 8  * }
 9  */
10 public class Solution {
11     public int maxPoints(Point[] points) {
12         if(points.length == 0)
13               return 0;
14           int answer = 0;
15           HashMap<Double,Integer> mp = new HashMap<Double,Integer>();
16
17           for(int i = 0;i < points.length;i++){
18             int duplicates = 0;
19             int value;
20             int count = 0;
21             mp.clear();
22             double k = 0.0;
23               for(int j = i+1;j <points.length;j++){
24
25                   if(points[i].x == points[j].x && points[i].y == points[j].y){
26                       duplicates++;
27                       continue;
28                   }
29                   if(points[i].x == points[j].x)
30                       k = (int)Double.POSITIVE_INFINITY;
31                   else
32                       k = (double)(points[j].y-points[i].y)/(points[j].x-points[i].x)+0.0;
33                   if(mp.containsKey(k)){
34                       value = mp.get(k) + 1;
35                   }
36                   else {
37                     value = 1;
38                 }
39                   mp.put(k, value);
40                   if(count < value)
41                       count = value;
42                }
43               answer = Math.max(answer, count+duplicates+1);
44           }
45           return answer;
46
47     }
48 }

虽然逻辑很简单,但是这道题细节非常烦人,对于刚入java门的我来说,还是花了一上午时间=。=,主要有以下几点:

1.map的声明:

HashMap<Double,Integer> mp = new HashMap<Double,Integer>();

2.map中更新value的方法:重新放入一个<key,new_value>覆盖以前的<key,old_value>即可;

3.对于横坐标相等的点,它们构成的直线斜率是无穷,要用Double.POSITIVE_INFINITY单独处理;

4.计算得到的斜率中,有两个非常特殊的值-0.0和0.0,理论上这两个值是相等的,都是零,但是java会判定这两个值不想等,所以要对-0.0做一个特殊处理,加上一个0.0就可以了。

【leetcode刷题笔记】Max Points on a Line,布布扣,bubuko.com

时间: 2024-10-08 16:42:04

【leetcode刷题笔记】Max Points on a Line的相关文章

【leetcode刷题笔记】Insert Interval

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1:Given intervals [1,3],[6,9], insert and merge [

【leetcode刷题笔记】Maximal Rectangle

Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 题解,很巧妙的一道题,对于一个0-1矩阵,它的每一行及以上都可以看作一个直方图(如下图所示),利用Largest Rectangle in Histogram的方法,可以在O(n)的时间搜索出这一行及以上的直方图中面积最大的矩形,对矩阵的每一行依次做这个操作,就可

【leetcode刷题笔记】Best Time to Buy and Sell Stock III

Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most two transactions. Note:You may not engage in multiple transactions at the same time (ie,

【leetcode刷题笔记】Largest Rectangle in Histogram

Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3]. The largest

【leetcode刷题笔记】Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 题解:递归,树的高度 = max(左子树高度,右子树高度)+1: 代码如下: 1 /** 2 * Definition for binary tree 3 * public cla

【leetcode刷题笔记】Merge Intervals

Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18]. 题解:首先对所有的区间按照start大小排序,然后遍历排序后的数组,用last记录前一个区间,如果遍历的当前区间可以和last合并,就把它合并到last里面:否则就把last放到answer list中,并且更新last

【leetcode刷题笔记】Flatten Binary Tree to Linked List

Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 Hints: If you notice carefully in the flattened tree, each node's right child points to the next node of a

【leetcode刷题笔记】Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest subst

【leetcode刷题笔记】Edit Distance

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a word: a) Insert a characterb) Delete a characterc) Replace