二维坐标中在一条直线上最大点数

这里写图片描述

import java.util.HashMap;
class Point {
      int x;
      int y;
      Point() { x = 0; y = 0; }
      Point(int a, int b) { x = a; y = b; }
  }

public class Solution {
    //解法一:存在问题
    public int maxPoints(Point[] points) {
        if(points==null||points.length==0)
            return 0;
        if(points.length==1||points.length==2)
            return points.length;
        HashMap<Integer,Integer>map=new HashMap<>();  
        for(int i=0;i!=points.length;i++){

            for(int j=i+1;j!=points.length;j++){

               int k=getXie(points[i],points[j]);
               if(!map.containsKey(k)){
                   map.put(k,1);
               }
               else{
                   int temp=map.get(k);
                   map.put(k,++temp);
               }
            }
        }
        int max=0;
        for(int k:map.keySet()){
             max=Math.max(max,map.get(k));
        }
        return max;
    }
    //获得两个点的斜率
    public int getXie(Point point1,Point point2){
           int a= Math.abs(point2.y-point1.y);
           int b=Math.abs(point2.x-point1.x);
           if(a==0)
             return 0;
           if(b==0)
             return Integer.MAX_VALUE;
          return  a/b;
    }
    //方法二:斜率求解法
    public int maxPoints2(Point[] points) {
        if(points==null||points.length==0)
            return 0;
        if(points.length==1||points.length==2)
            return points.length;
        int max=0;
        for(int i=0;i!=points.length;i++)
        {
            int vcnt=0;  //垂直的点
            int dup=0;   //重复的点
            int curmax=1;
            HashMap<Double,Integer>map=new HashMap<>();
            for(int j=0;j!=points.length;j++){

               if(j!=i){
                  double x1=points[i].x-points[j].x;
                  double y1=points[i].y-points[j].y;
                  if(x1==0&&y1==0)
                  {
                    dup++;   //重复点加1
                  }else if(x1==0)
                  { 
                    if(vcnt==0)
                        vcnt=2;
                    else
                       vcnt++;  //垂直点加1
                    curmax=Math.max(vcnt,curmax);
                  }else{
                   double k=y1/x1;  //斜率
                    if(!map.containsKey(k))
                    {
                         map.put(k,2); 
                    }else{
                         int temp=map.get(k);
                         map.put(k,++temp);    
                    }
                    curmax=Math.max(map.get(k),curmax);
                  }

               }
            }
         max=Math.max(max,curmax+dup);
        }
        return max;

    }
    public static void main(String[]args){
        //System.out.println("Hello World!");
        Point point1=new Point(1,1);
        Point point2=new Point(2,2);
        Point point3=new Point(3,3);
        Point point4=new Point(3,4);
        Point[] points={point1,point2,point3,point4};
        Solution s=new Solution();
        System.out.println(s.maxPoints2(points));

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值