lintcode Max Points on a Line

本是古典 何须时尚 2022-07-15 13:19 236阅读 0赞

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

Have you met this question in a real interview?

Yes

Example

Given 4 points: (1,2), (3,6), (0,0), (1,3).

The maximum number is 3.

Tags

  1. package tju.cs.bigdata;
  2. import java.util.HashSet;
  3. import java.util.Set;
  4. public class MaxPointsonaLine {
  5. /**
  6. * @param points an array of point
  7. * @return an integer
  8. * </br>ac
  9. */
  10. public int maxPoints(Point[] points) {
  11. // Write your code here
  12. int res = 0;
  13. if(points == null) return res;
  14. // points = delSame(points);
  15. int len = points.length;
  16. if(len < 3) return len;
  17. int samecount = 0;
  18. for(int i = 0 ; i < len; i++){
  19. int tmps = 1;
  20. for(int j = 0; j < len; j++){
  21. if(i == j)continue;
  22. if(points[i].x == points[j].x && points[i].y == points[j].y){
  23. tmps++;
  24. continue;
  25. }
  26. int tmpc = 2;
  27. for(int k = 0 ;k < len; k++){
  28. if(k == i || k == j){
  29. continue;
  30. }
  31. if(isOnLine(points[i] , points[j] , points[k])){
  32. tmpc++;
  33. }
  34. }
  35. if(tmpc > res)res = tmpc;
  36. }
  37. if(tmps > samecount)samecount = tmps;
  38. }
  39. return Math.max(samecount,res);
  40. }
  41. /**
  42. * @param points an array of point
  43. * @return an integer
  44. * 思路錯誤
  45. */
  46. public int maxPoints1(Point[] points) {
  47. // Write your code here
  48. int res = 0;
  49. if(points == null) return res;
  50. int len = points.length;
  51. if(len < 3) return len;
  52. for(int i = 0 ; i < len-1; i++){
  53. int tmpc = 2;
  54. for(int j = 0; j < len; j++){
  55. if(j == i || j == i + 1){
  56. continue;
  57. }
  58. if(isOnLine(points[i] , points[i+1] , points[j])){
  59. tmpc++;
  60. }
  61. }
  62. if(tmpc > res)res = tmpc;
  63. }
  64. return res;
  65. }
  66. public boolean isOnLine(Point a, Point b, Point c){
  67. int left = (a.x - b.x)*(c.y - b.y);
  68. int right = (a.y - b.y)*(c.x - b.x);
  69. if(left == right){
  70. return true;
  71. }else{
  72. return false;
  73. }
  74. }
  75. public static void main(String[] args){
  76. // String string = "[[0,9],[138,429],[115,359],[115,359],[-30,-102],[230,709],[-150,-686],[-135,-613],[-60,-248],[-161,-481],[207,639],[23,79],[-230,-691],[-115,-341],[92,289],[60,336],[-105,-467],[135,701],[-90,-394],[-184,-551],[150,774]]";
  77. String string = "[[0,-12],[5,2],[2,5],[0,-5],[1,5],[2,-2],[5,-4],[3,4],[-2,4],[-1,4],[0,-5],[0,-8],[-2,-1],[0,-11],[0,-9]]";
  78. System.out.println(string);
  79. String[] stes = string.split("],\\[");
  80. int steslen = stes.length;
  81. stes[0] = stes[0].substring(2, stes[0].length());
  82. stes[steslen - 1] = stes[steslen - 1].substring(0, stes[steslen - 1].length() - 2);
  83. Set<Point> setpoints = new HashSet<>();
  84. for(int i = 0 ; i < stes.length; i ++){
  85. System.out.println(stes[i] + " ");
  86. int x = Integer.parseInt(stes[i].split(",")[0]);
  87. int y = Integer.parseInt(stes[i].split(",")[1]);
  88. setpoints.add(new Point(x,y));
  89. }
  90. Point[] points = setpoints.toArray(new Point[setpoints.size()]);
  91. System.out.println("最大的:" + new MaxPointsonaLine().maxPoints(points));
  92. }
  93. }

发表评论

表情:
评论列表 (有 0 条评论,236人围观)

还没有评论,来说两句吧...

相关阅读