Leetcode-max Points on a line

Source: Internet
Author: User

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

Solution:

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  */Ten  One classLine { A     intSlopex; -     intSlopey; -     intb; the     BooleanNegslope; -     BooleanSamepointline; -Set<point>PointSet; - String uid; +      PublicLine (intSxintSyintBbBooleanSPL) { -         if(sx<0) Slopex =-SX; +         ElseSlopex =SX; A  at         if(sy<0) Slopey =-Sy; -         ElseSlopey =Sy; -  -         if(sy==0) Slopex=1; -         if(sx==0) Slopey=1; -b =BB; in  -         if(sx<0 | | sy<0) Negslope =true; to         ElseNegslope =false; +          -Samepointline =SPL; the  *PointSet =NewHashset<point>(); $ Panax NotoginsengUID = integer.tostring (Slopex) + "/" +integer.tostring (Slopey) + "/" +double.tostring (b) + "/" +boolean.tostring (NegSlope )+"/"+boolean.tostring (samepointline); -   } the  +    Public Booleanequals (line L2) { A       if(uid==l2.uid) the           return true; +       Else return false; -   } $  $ }        -  -  the  Public classSolution { -      Public intmaxpoints (point[] points) {Wuyi         if(points.length==1)return1; the          -Set<point> PSet =NewHashset<point>(); Wu          for(inti=0;i<points.length;i++) Pset.add (Points[i]); -  AboutMap<string,line> linelist =NewHashmap<string,line>(); $List<string> uidlist =NewArraylist<string>(); -          for(inti=1;i<points.length;i++){ -Set<point> Curpset =NewHashset<point>(); - Curpset.addall (pSet); A              for(intj=0;j<i;j++) +                 if(Curpset.contains (Points[j])) { the                 //This is a point with not been checked. -                 //Calculate The line formed by I and J $                     intIX =points[i].x; the                     intiy =points[i].y; the                     intJX =points[j].x; the                     intJY =points[j].y; the                     intSX = points[i].x-points[j].x; -                     intSY = points[i].y-points[j].y; inLine L1 =NULL; the                     //infnite slope line. the                     if(Sx==0 && sy==0) AboutL1 =NewLine (points[i].x,points[i].y,0,true); the                     Else if(sx==0) theL1 =NewLine (0,1,points[i].x,false); the                     Else if(sy==0) +L1 =NewLine (1,0,POINTS[I].Y,false); -                     Else { the                         intBB = jy*ix-jx*iy;Bayi                         intGCD =GETGCD (sx,sy); the                         intGCD2 =GETGCD (BB,SX); theSX = sx/gcd; -SY = sy/gcd; -BB = bb/GCD2; theL1 =NewLine (SX,SY,BB,false); the                     } the  the                     //Check Each of the exsiting line. -                     if(Linelist.containskey (L1.uid)) { the Linelist.get (l1.uid). Pointset.add (Points[i]); the Linelist.get (l1.uid). Pointset.add (Points[j]); the Curpset.removeall (Linelist.get (l1.uid). PointSet);94                          the}Else {                         the Linelist.put (L1.UID,L1); the Uidlist.add (l1.uid);98 L1.pointSet.add (Points[i]); About L1.pointSet.add (Points[j]); -                     }101                }102         }103 104         intMax = 0; the          for(intI=0;i<uidlist.size (); i++)106             if(Linelist.get (Uidlist.get (i)). Pointset.size () >max)107Max =Linelist.get (Uidlist.get (i)). Pointset.size ();108 109         returnMax; the         111     } the 113  the      Public intGETGCD (intAintb) { the         intleft = a%b; the          while(left!=0){117A=b;118b=Left ;119left=a%b; -         }121         returnb;122     }123 124  the}

Leetcode-max Points on a line

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.