跳至主要内容

149. 直线上最多的点数

/**
 * Definition for a point.
 * struct Point {
 *     int x;
 *     int y;
 *     Point() : x(0), y(0) {}
 *     Point(int a, int b) : x(a), y(b) {}
 * };
 */
class Solution {
public:
    int maxPoints(vector<Point>& points) {
       
        if (points.size() <= 2)
        {
            return points.size();
        }
       
        set<double> find;
        set<double> findx;
        set<double> findy;

        int max = 0;
        for (int i = 0; i < points.size() - 1; i++)
        {
            for (int j = i + 1; j < points.size(); j++)
            {
                if (points[j].x != points[i].x && points[j].y != points[i].y)
                {
                    double d = (double)(points[j].x - points[i].x) / (points[j].y - points[i].y);
                    if (find.find(d)!=find.end())
                    {
                        continue;
                    }
                    find.insert(d);

                    int num = 1;
                    for (int z = 0; z < points.size(); z++)
                    {
                        if (z != i)
                        {
                            double d1 = (double)(points[z].x - points[i].x) / (points[z].y - points[i].y);

                            if (d == d1 || (points[z].x == points[i].x && points[z].y == points[i].y))
                            {
                                num++;
                            }
                        }
                    }

                    if (num > max)
                    {
                        max = num;
                    }
                }
                else if (points[j].y != points[i].y)
                {
                    double d = points[j].x;
                    if (findx.find(d) != findx.end())
                    {
                        continue;
                    }
                    findx.insert(d);

                    int num = 1;
                    for (int z = 0; z < points.size(); z++)
                    {
                        if (z != i)
                        {
                            if (points[z].x == points[i].x)
                            {
                                num++;
                            }
                        }
                    }

                    if (num > max)
                    {
                        max = num;
                    }
                }
                else if (points[j].x != points[i].x)
                {
                    double d = points[j].y;
                    if (findy.find(d) != findy.end())
                    {
                        continue;
                    }

                    findy.insert(d);

                    int num = 1;
                    for (int z = 0; z < points.size(); z++)
                    {
                        if (z != i)
                        {
                            if (points[z].y == points[i].y)
                            {
                                num++;
                            }
                        }
                    }

                    if (num > max)
                    {
                        max = num;
                    }
                }
                else
                {
                    continue;
                }
            }
        }

        if (max == 0)
        {
            return points.size();
        }

        return max;
    }
};

评论

此博客中的热门博文

ncurses与readline结合

  #define _XOPEN_SOURCE 700       /* for wcswidth and 700 is for mbsnrtowcs */ #include<wchar.h> #include<ncurses.h>       /* ncurses.h includes stdio.h */ #include<stdlib.h> #include<string.h> #include<readline/readline.h> #include<locale.h>     int mygetstr( char *str, int y, int x){    WINDOW *win;    int size, col;    int ok = 0;    int width;    wchar_t wstr[80];    char *p;        getmaxyx(stdscr, size, col);        void getaline( char *s){      str = s;      rl_callback_handler_remove();      ok = 1;    }        rl_callback_handler_install( "" , getaline);    win = newwin(1, col-x, y, x);    while (1){      rl_callback_read_char(); ...

简单的整数最小乘积的解法

给定 n 个整数,每次可以从剩下的整数中取走两个整数并计算这两个整数的积。 若该操作进行 m 次,求每次计算的积之和的最小值。 Input / 输入格式 有多组测试数据。第一行输入一个整数 T(约 30)代表测试数据组数,对于每组数据: 第一行输入两个整数 n 和 m(1≤n≤10​5​​, 0≤m≤​2​​n​​),它们的含义如题中所述。 第二行输入 n 个整数 a​1​​,a​2​​,⋯,a​n​​(0≤a​i​​≤10​4​​)表示给定的整数。 Output / 输出格式 每组数据输出一行一个整数,表示积之和的最小值。 Sample Input / 样例输入 3 4 2 1 3 2 4 3 1 2 3 1 4 0 1 3 2 4 Sample Output / 样例输出 10 2 0   Hint / 样例说明 对于第一组样例数据,答案是 1×4+3×2=10。 对于第二组样例数据,答案是 2×1=2。 package main import (         "bufio"         "fmt"         "os"         "sort"         "strconv"         "strings" ) var ...

利用yellowdns解决dns污染问题

 很多网站的dns直接被污染成了127.0.0.1,这样一般就无法访问了,很多翻墙软件也认为是局域网,所以访问不了 这时候,使用yellowdns,将dns转发到远程。然后listen本地的53端口。再将dns服务器都改成本地 vi /etc/resolv.conf windows和路由器,也可以都更改