max-points-on-a-line(Leetcode)

原创 2016年05月30日 18:02:55

题目描述

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
题意是给定一个2D平面图(平面坐标系),找出出现最多的点的那条直线,并返回在该直线上点的个数。暴力破解即可。

package sxd.learn.java.leetcode;


/**
 * 
 * @author lab
 * 2016/5/12
 *  Given n points on a 2D plane, find the maximum number of 
 *  points that lie on the same straight line.
 */
public class Leetcode3 {

    public static void main(String[] args) {

    }

    public int maxPoints(Point[] points) {
        int length = points.length;
        if(length == 0)
            return 0;
        if(length == 1)
            return 1;
        int count = 0;
        int max = 0;
        for(int i = 0; i < length; i++){
            for (int j = i + 1; j < length; j++) {
                count = 0;
                //同一垂直线上
                if(points[i].x == points[j].x && points[i].y == points[j].y){
                    for (int k = 0; k < length; k++) {
                        if(points[k].x == points[i].x && points[k].y == points[i].y){
                            count ++;
                        }
                    }
                    if(count > max)
                        max = count;
                    continue;
                }

                //斜率
                for (int k = 0; k < length; k++) {
                    if((points[k].y - points[j].y) * (points[i].x - points[j].x) == (points[i].y - points[j].y) * (points[k].x - points[j].x)){
                        count++;
                    }
                }

                if(count > max)
                    max = count;
            }
        }
        return max;
    }

    class Point {
         int x;
         int y;
         Point() { x = 0; y = 0; }
         Point(int a, int b) {
             x = a; y = b;
         }
    }

}

相关文章推荐

LeetCode 149. Max Points on a Line(直线上的点)

原题网址:https://leetcode.com/problems/max-points-on-a-line/ Given n points on a 2D plane, find the max...
  • jmspan
  • jmspan
  • 2016年05月26日 01:12
  • 343

LeetCode:Max Points on a Line

这是LeetCode上的一道题,题目如下: Given n points on a 2D plane, find the maximum number of points that lie on...

LeetCode OJ --问题与解答 Max Points on a Line

题目 Given n points on a 2D plane, find the maximum number of points that lie on ...
  • seawade
  • seawade
  • 2014年02月17日 22:24
  • 441

【leetcode每日一题】149.Max Points on a line

题目:Given n points on a 2D plane, find the maximum number of points that lie on the same straight lin...

【LeetCode】Max Points on a Line

参考: http://blog.csdn.net/ict2014/article/details/17400345 题目: Max Points on a Line   Given ...

【LeetCode】Max Points on a Line 解题报告

【题意】 求二维平面上n个点中,最多共线的点数。 【思路】 比较直观的方法是,三层循环,以任意两点划线,判断第三个点是否在这条直线上。 【Java代码】 /** * Definition for a...
  • ljiabin
  • ljiabin
  • 2014年08月28日 22:05
  • 6038

leetcode 149:Max Points on a Line(java实现)细致分析

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

leetcode Max Points on a Line

文章摘自:http://blog.csdn.net/doc_sgl/article/details/17103427 分析: 任意一条直线都可以表述为 y = ax + ...

LeetCode:Max Points on a Line (Java)

题目: Max Points on a Line Given n points on a 2D plane, find the maximum number of points that li...

(leetcode)Max Points on a Line

Max Points on a Line
  • fcunren
  • fcunren
  • 2014年08月18日 20:33
  • 201
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:max-points-on-a-line(Leetcode)
举报原因:
原因补充:

(最多只允许输入30个字)