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


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

    示例 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);
        }
    };
  • 相关阅读:
    react脚手架和JSX
    promise
    防抖和节流
    call/apply/bind 用法
    js this指向
    vue单页面应用刷新网页后vuex的state数据丢失的解决方案
    Echarts基础
    继承
    原型链
    vue项目中使用生成动态二维码
  • 原文地址:https://www.cnblogs.com/strawqqhat/p/10602378.html
Copyright © 2020-2023  润新知