查找表类算法//直线上最多的点数

给定一个二维平面,平面上有 个点,求最多有多少个点在同一条直线上。

示例 1:

输入: [[1,1],[2,2],[3,3]]
输出: 3
解释:
^
|
|        o
|     o
|  o  
+------------->
0  1  2  3  4

示例 2:

输入: [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
输出: 4
解释:
^
|
|  o
|     o        o
|        o
|  o        o
+------------------->
0  1  2  3  4  5  6
/**
 * Definition for a point.
 * class Point {
 *     int x;
 *     int y;
 *     Point() { x = 0; y = 0; }
 *     Point(int a, int b) { x = a; y = b; }
 * }
 */
class Solution {
    public int maxPoints(Point[] points) {
		if(points.length == 0) {
			return 0;
		}
		int[] count = new int[points.length];
		for (int i = 0; i < points.length; i++) {
			count[i] = 1;
			int size = 1;
			int same = 0;
			HashMap<Integer[], Integer> hashMap = new HashMap<>();
			for (int j = 0; j < points.length; j++) {
				if(i != j) {
					if(points[i].x != points[j].x) {
						int dy = points[i].y - points[j].y;
						int dx = points[i].x - points[j].x; 
						int gcd = generateGCD(dy, dx);
						if(gcd != 0) {
							dy = dy / gcd;
							dx = dx / gcd;
						}
						Integer[] nums = new Integer[2];
						nums[0] = dy;
						nums[1] = dx;
						boolean flag = false;
						for (Integer[] array : hashMap.keySet()) {
							if(nums[0] == array[0] && nums[1] == array[1]) {
								flag = true;
								hashMap.put(array, hashMap.get(array) + 1);
							}
						}
						if(!flag) {
							hashMap.put(nums, 1);
						}
					}else {
						if(points[i].y == points[j].y) {
							same++;
						}
						size++;
					}
				}	
			}
			for (Integer[] array : hashMap.keySet()) {
				if(hashMap.get(array) + 1 > count[i]) {
					count[i] = hashMap.get(array) + 1;
				}
			}
			count[i] += same;
			count[i] = Math.max(count[i], size);
		}
		int maxIndex = 0;
		for (int i = 1; i < count.length; i++) {
			if(count[i] > count[maxIndex]) {
				maxIndex = i;
			}
		}
		return count[maxIndex];
	}
 
	// 欧几里得算法:计算最大公约数
	private int generateGCD(int x, int y) {
		if (y == 0)
			return x;
		return generateGCD(y, x % y);
	}

}
/**
 * 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:
    /*
     * @param points: an array of point
     * @return: An integer
     */
    int maxPoints(vector<Point> &points) {
        // write your code here
        int res = 0;
        
        for(int i=0; i<points.size(); i++){
            map<pair<int, int>, int> m;
            int common = 1;//记录相同点的个数
            for(int j = i+1; j<points.size(); j++){
                //处理相同的点
                if(points[i].x==points[j].x && points[i].y==points[j].y){
                    common++;
                    continue;
                }
                //处理不同的点
                int distance_x = points[i].x - points[j].x;//斜率有正有负
                int distance_y = points[i].y - points[j].y;
                int d = gcd(distance_x, distance_y);
                m[{distance_x/d, distance_y/d}]++;
                
            }
            res = max(res, common);//处理相同的点的个数
            //处理不同点的个数
            map<pair<int, int> ,int>::iterator it;
            for(it = m.begin(); it != m.end(); it++){
                res = max(res, it->second+common);
            }
        }
        return res;
    }    
    
    int gcd(int a, int b){//a,b最大公约数
        return (b==0) ? a : gcd(b, a%b);
    }
};

 

class Solution {
public:
    int maxPoints(vector<Point>& points) {
        int count = 0;
        for(int i = 0; i < points.size(); i++){
            map<pair<int,int>,int> m;
            int cnt = 0;
            int samePointCnt = 0;
            for(int j = i+1; j < points.size(); j++){
                if(points[i].x == points[j].x&&points[i].y==points[j].y)
                    samePointCnt++;
                else{
                    int xDiff = points[i].x - points[j].x;
                    int yDiff = points[i].y - points[j].y;
                    int g = gcd(xDiff,yDiff);
                    xDiff/=g;
                    yDiff/=g;
                    cnt = max(cnt,++m[make_pair(xDiff,yDiff)]);
                }
            }
            count = max(count,cnt+samePointCnt+1);
        }
        return count;
    }
private:
    int gcd(int a,int b){
        if(b == 0)
            return a;
        else
            return gcd(b,a%b);
    }
};

 

posted @ 2018-11-08 23:16  strawqqhat  阅读(101)  评论(0编辑  收藏  举报
#home h1{ font-size:45px; } body{ background-image: url("放你的背景图链接"); background-position: initial; background-size: cover; background-repeat: no-repeat; background-attachment: fixed; background-origin: initial; background-clip: initial; height:100%; width:100%; } #home{ opacity:0.7; } .wall{ position: fixed; top: 0; left: 0; bottom: 0; right: 0; } div#midground{ background: url("https://i.postimg.cc/PP5GtGtM/midground.png"); z-index: -1; -webkit-animation: cc 200s linear infinite; -moz-animation: cc 200s linear infinite; -o-animation: cc 200s linear infinite; animation: cc 200s linear infinite; } div#foreground{ background: url("https://i.postimg.cc/z3jZZD1B/foreground.png"); z-index: -2; -webkit-animation: cc 253s linear infinite; -o-animation: cc 253s linear infinite; -moz-animation: cc 253s linear infinite; animation: cc 253s linear infinite; } div#top{ background: url("https://i.postimg.cc/PP5GtGtM/midground.png"); z-index: -4; -webkit-animation: da 200s linear infinite; -o-animation: da 200s linear infinite; animation: da 200s linear infinite; } @-webkit-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @-o-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @-moz-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @keyframes cc { 0%{ background-position: 0 0; } 100%{ background-position: 600% 0; } } @keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-webkit-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-moz-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-ms-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } }