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 maximum number of points that lie on the same straight line.

开始除了暴力破解没想到什么解决方案,暴力破解就是随机取两个点,判断其他的点在这两个点构成的直线上。代码比较繁琐就不实现了。

后来参考http://blog.csdn.net/linhuanmars/article/details/21060933

直接判断每一个点和其它点构成的斜率,然后统计同斜率的个数就好。

存在几个陷阱

1、当x1==x2,即斜率的无穷大时的处理。代码使用int.MaxValue,我觉得存在点问题:如果p1(1,-1),p2(2,int.MaxValue-1),那么这个两个点也是的斜率k就是int.MaxValue。比如输入是[[1,-1],[1,0],[2,2147483646]]这三个点,正确输出应该是2,但是当前解决方案却输出为3。

同时还发现一个问题,提交参考博文中的代码中,用以上测试用例的Expected answer=2,但是也能通过


而使用我的C#代码提交,用以上测试用例的Expected answer=3,也能通过。同时其它语言的Expected answer都是2,有鬼...


2、当点重合时

3、当全部点重合时,hashtable为空的判断

/**
 * Definition for a point.
 * public class Point {
 *     public int x;
 *     public int y;
 *     public Point() { x = 0; y = 0; }
 *     public Point(int a, int b) { x = a; y = b; }
 * }
 */
public class Solution 
{
    public int MaxPoints(Point[] points) 
    {
        int n = points.Length, max = 2, duplicate;
        if (n < 2)
            return n;
        double k;
        Hashtable table = new Hashtable();
        for (int i = 0; i < n; i++)
        {
            duplicate = 0;
            
            for (int j = 0; j < n; j++)
            {
                if (i != j)
                {
                    if (points[i].x == points[j].x && points[i].y == points[j].y)
                        duplicate++;
                    else
                    {
                        if (points[i].x == points[j].x)
                            k = int.MaxValue;
                        else
                            k = (double)(points[i].y - points[j].y) / (points[i].x - points[j].x);
                        if (table.Contains(k))
                            table[k] = (int)table[k] + 1;
                        else
                            table.Add(k, 2);
                    }
                }
            }
            if (table.Count == 0)
                return n;
            foreach (int val in table.Values)
                max = Math.Max(max, val + duplicate);
            table.Clear();
        }
        return max;
    }
}

另外,根据参考的代码,循环变量i,j的取值不一样,也需要消化一下

public int MaxPoints(Point[] points)
        {
            int n = points.Length, max = 2, duplicate;
            if (n == 0)
                return 0;
            double k;
            Hashtable table = new Hashtable();
            for (int i = 0; i < n-1; i++)
            {
                duplicate = 0;
                for (int j = i+1; j < n; j++)
                {
                    if (points[i].x == points[j].x && points[i].y == points[j].y)
                        duplicate++;
                    else
                    {
                        if (points[i].x == points[j].x)
                            k = int.MaxValue;
                        else
                            k = (double)(points[i].y - points[j].y) / (points[i].x - points[j].x);
                        if (table.Contains(k))
                            table[k] = (int)table[k] + 1;
                        else
                            table.Add(k, 2);
                    }
                }
                if (table.Count == 0)
                    return n;
                foreach (int val in table.Values)
                    max = Math.Max(max, val + duplicate);
                table.Clear();
            }
            return max;
        }


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值