Leetcode-Max points on a line [AC source code]

Source: Internet
Author: User
1 package COM. LW. leet3; 2 3 Import Java. util. hashmap; 4 Import Java. util. iterator; 5 import Java. util. map; 6 Import Java. util. map. entry; 7 8 9/** 10 * @ classname: solution 11 * @ Description: Max points on a line 12 * given n points on a 2D plane, find the maximum number of points that 13 * lie on the same straight line. 14 * @ author Liuwei 15 * @ date 2:51:08 on October 16 * @ Mail [email protected] 17 */18 19/*** 20 * definition for a point. 21 * class point {22 * int X; 23 * int y; 24 * point () {x = 0; y = 0;} 25 * point (int A, int B) {x = A; y = B;} 26 *} 27 */28 29 public class solution {30 31 public int maxpoints (point [] points) {32 int max = 0; 33 If (points. length <= 2) {34 max = points. length; 35} 36 else {37 for (INT I = 0; I <points. length; I ++) {38 int partition num = 0; 39 Map <double, integer> map = new hashmap <double, integer> (); 40 for (Int J = I + 1; j <points. length; j ++) {41 if (points [I]. X = points [J]. X & points [I]. y = points [J]. y) {42 rows num ++; 43 continue; 44} 45 46 Double K = 0; 47 If (points [I]. X = points [J]. x) {48 k = double. max_value; 49} 50 else {51 k = (double) (points [I]. y-points [J]. y)/(points [I]. x-points [J]. x); 52/** 53 * Submission result: Wrong answer 54 * input: [(2, 3), (3, 3), (-5, 3)] 55 * output: 2 56 * expected: 3 57*58 * avoid K = + 0.0-0.0 59 **/60 if (k = 0) {61 K = 0; 62} 63} 64 65 if (map. containskey (k) {66 map. put (K, map. get (k) + 1); 67} 68 else {69 map. put (K, 2); 70} 71} 72 73/** 74 * Submission result: Wrong answer 75 * input: [(1, 1), (1, 1), (1, 1)] 76 * output: 0 77 * expected: 3 78*79 * avoid the points are all equal 80 **/81 If (larger num> MAX) {82 max = larger num + 1; 83} 84 iterator <entry <double, integer> iter = map. entryset (). iterator (); 85 while (ITER. hasnext () {86 Entry <double, integer> entry = ITER. next (); 87 int num = entry. getvalue (); 88 If (Num + interval num> MAX) {89 max = num + interval num; 90} 91} 92} 93} 94 return Max; 95} 96 97 Public static void main (string [] ARGs) {98 point [] P = {New Point (2, 3), new point (3, 3 ), new Point (-)}; 99 // point [] P = {New Point (1, 1), new point (), new point )}; 100 101 solution S = new solution (); 102 system. out. println (S. maxpoints (p); 103 104} 105 106}

 

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.