• [leetcode]Max Points on a Line


    思路是O(n^2)的,就是取一个点,和其他所有的点比,如果斜率一样的就在一条线上。这里要注意有重复的点,另外,学会了float也可以做map的key,还有map[k]++也覆盖了一开始count为0的情况。

    参考:http://blog.csdn.net/doc_sgl/article/details/17103427

    #include <unordered_map>
    using namespace std;
    class Solution {
    public:
        int maxPoints(vector<Point> &points) {
            int ans = 0;
            int size = points.size();
            if (size == 0) return 0;
            for (int i = 0; i < size; i++) {
                int duplicate = 1;
                unordered_map<float, int> mp;
                for (int j = 0; j < size; j++) {
                    if (i == j) continue;
                    if (points[i].x == points[j].x
                        && points[i].y == points[j].y) {
                        duplicate++;
                        continue;
                    }
                    float k = 0;
                    if (points[i].x == points[j].x) k = INT_MAX;
                    else {
                        k = (float) (points[i].y - points[j].y) / (points[i].x - points[j].x);
                    }
                    mp[k]++;
                }
                for (unordered_map<float, int>::iterator it = mp.begin(); it != mp.end(); it++) {
                    if (it->second + duplicate > ans)
                        ans = it->second + duplicate;
                }
                if (mp.size() == 0 && duplicate > ans)
                    ans = duplicate;
            }
            return ans;
        }
    };
    

      

  • 相关阅读:
    Spring IOC知识点一网打尽
    Spring中-IOC-Bean的初始化-循环依赖的解决
    原型模式(Prototype)
    生成器模式
    工厂模式
    单例模式
    查询性能优化
    索引
    sql游标的使用入门
    js和C#中的编码和解码
  • 原文地址:https://www.cnblogs.com/lautsie/p/3493304.html
Copyright © 2020-2023  润新知