LeetCode:Max Points on a Line

意识到下边这点是关键:

斜率和与X轴的交点可以确定一条直线。

依据此推论,按照斜率和与X轴的交点对点进行分组,最后看哪个组中的点最多。

对于与X轴平行的直线需单独另分组。


import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;


public class Solution {
	
	public int maxPoints(Point[] points) {
		if (points.length == 1 || points.length == 2) {
			return points.length;
		}
		
        Map<Double, Set<Integer>> yMap = new HashMap<Double, Set<Integer>>();//存放与X轴平行的直线,KEY:与Y轴交点坐标,VALUE:Point
        //存放与x轴有交点的直线,KEY-斜率,VLUE-(KEY-与X轴交点的X坐标,VALUE-Point)
        Map<Double, Map<Double, Set<Integer>>> xMap = new HashMap<Double, Map<Double, Set<Integer>>>();
        for (int i = 0; i < points.length - 1; i++) {
        	for (int j = i + 1; j < points.length; j++) {
        	
        		if (points[i].y == points[j].y) { //与X轴平行
        			double ykey= points[i].y;
        			
        			Set<Integer> list = yMap.get(ykey);
            		if (list == null) {
            			list = new HashSet<Integer>();
            			yMap.put(ykey, list);
            		}
            		list.add(i);
            		list.add(j);
        		} else { //与X轴有交点
        			double tan;//斜率
        			//与X轴交点的X坐标
        			double xkey = points[j].x - (double) ((points[j].x - points[i].x) * points[j].y) / (double) (points[j].y - points[i].y);
        			if (points[j].x - points[i].x == 0) {
        				tan = Double.NaN;
        			} else {
        				tan = (double) (points[j].y - points[i].y) / (double) (points[j].x - points[i].x);
        			}
        			Map<Double, Set<Integer>> m = xMap.get(tan);
            		if (m == null) {
            			m = new HashMap<Double, Set<Integer>>();
            			xMap.put(tan, m);
            		}
            		Set<Integer> set = m.get(xkey);
            		if (set == null) {
            			set = new HashSet<Integer>();
            			m.put(xkey, set);
            		}
            		set.add(i);
            		set.add(j);
        		}
        		
            }
        }
        
        int max = 0;
        for (Map<Double, Set<Integer>> tan :  xMap.values()) {
        	for (Set<Integer> set : tan.values()) {
        		max = Math.max(max, set.size());
        	}
        }
        
        for (Set<Integer> set:  yMap.values()) {
        	max = Math.max(max, set.size());
        }
        
        return max;
    }
	
	public static void main(String[] args) {
		Point[] ps = new Point[] {
				new Point(0,0),
				new Point(1,1),
				new Point(1,-1)
				};
		Solution s = new Solution();
		int m = s.maxPoints(ps);
		System.out.print(m);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值