`
hcx2013
  • 浏览: 88731 次
社区版块
存档分类
最新评论

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.

 

/**
 * Definition for a point.
 * class Point {
 *     int x;
 *     int y;
 *     Point() { x = 0; y = 0; }
 *     Point(int a, int b) { x = a; y = b; }
 * }
 */
public class Solution {
	public int maxPoints(Point[] points) {
		if (points.length == 0 || points == null) {
			return 0;
		}
		if (points.length == 1) {
			return 1;
		}
		int res = 1;
		for (int i = 0; i < points.length; i++) {
			HashMap<Float,Integer> hashMap = new HashMap<Float, Integer>();
			int same = 0;
			int max = 1;
			for (int j = 0; j < points.length; j++) {
				if (i == j) {
					continue;
				}
				if (points[i].x == points[j].x && points[i].y == points[j].y) {
					same++;
					continue;
				}
				float tmp = (float)(points[i].y - points[j].y) / (points[i].x - points[j].x);
				if (hashMap.containsKey(tmp)) {
					hashMap.put(tmp, hashMap.get(tmp) + 1);
				} else {
					hashMap.put(tmp, 2);
				}
			}
			for (Integer value : hashMap.values()) {
				max = Math.max(max, value);
			}
			max += same;
			res = Math.max(max, res);
		}
		return res;
	}
}

 

0
1
分享到:
评论

相关推荐

    js-leetcode题解之149-max-points-on-a-line.js

    javascript js_leetcode题解之149-max-points-on-a-line.js

    python-leetcode题解之149-Max-Points-on-a-Line.py

    python python_leetcode题解之149_Max_Points_on_a_Line.py

    dna匹配leetcode-leetcode:leetcode刷题

    dna匹配 leetcode leetcode刷题--C++ 哈希表 Longest Substring Without Repeating Characters ...Points on a Line 斜率 map, int&gt; Fraction to Recurring Decimal map long long 正负号 Repeated DNA S

    LeetCode最全代码

    Up to date (2016-12-18), there are `447` Algorithms / `13` Database / `4` Shell / `4` Draft questions on [LeetCode Online Judge](https://leetcode.com/). The number of questions is increasing recently...

    leetcodemaxpointsonaline-leetcode:leetcode

    max points on a line leetcode ISCAS15 - leetcode - week1 唐波 任杰 王建飞 殷康 张一鸣 ISCAS15 - leetcode - week2 曾靖 刘重瑞 沉雯婷 刘旭斌 王建飞 ISCAS15 - leetcode - week3 殷康 张一鸣 赵伟 任杰 唐波 ...

    Selective_Leetcode_solutions

    2. 题目149 - "Max Points on a Line" 这道题目要求找到平面上最多共线的点。解决方法通常涉及线性代数,如计算两点之间的斜率,并通过哈希表记录斜率及其出现的次数。在Java中,可以使用HashMap来存储斜率和对应的...

    leetcodemaxpointsonaline-leetcode:https://oj.leetcode.com/的解决方案

    在LeetCode平台上,题目"Max Points on a Line"是一道典型的几何与图论结合的问题,旨在测试编程者对数据结构和算法的理解。本题要求找到二维平面上最多共线的点的数量。这个问题的关键在于理解如何有效地计算每一对...

    leetcode-answer-and-analysis(part).zip_图形图像处理_Java_

    2. **Max points on a line** (平面上最多的共线点): 此题考察的是线性代数和几何知识,以及如何在二维平面上找到共线点。在Java中,可以使用数据结构如HashMap来存储斜率和点的数量,从而有效地计算最大共线点数。 ...

    np难问题近似算法(绝版好书)

    13.6 Online load balancing and virtual circuit routing 13.6.1 Load balancing on unrelated machines 13.6.2 Online virtual circuit routing 13.6.3 Recent results 13.7 Variants of competitive analysis ...

    微软内部资料-SQL性能优化3

    For example, a shared intent lock placed at the table level means that a transaction intends on placing shared (S) locks on pages or rows within that table. Setting an intent lock at the table level ...

    NGUI Next-Gen UI 3.0.7 f1.unitypackage

    - FIX: Max line count on labels should now work again. - FIX: Fixed the Drag Objects script on mobile devices. It was not applying momentum properly. - DEL: OnHover is no longer sent via selection ...

    微软内部资料-SQL性能优化2

     Generate a hypothesis based on performance counters captured by System Monitor.  For each hypothesis generated, identify at least two other non-System Monitor pieces of information that would ...

    LeetCode:LeetCode解决方案

    LeetCodeLeetCode solutions(Java)树Minimum Depth of Binary Tree栈evaluate-reverse-polish-notation穷举max-points-on-a-line链表sort-list排序insertion-sort-list树binary-tree-postorder-traversal树binary-...

    王小平版遗传算法的光盘源代码

    either a population params file or `none on the command line for each population. Note: since the random seed value is not a population parameter but it can be specified in the population ...

    计算机网络第六版答案

    An Internet Exchange Points (IXP) (typically in a standalone building with its own switches) is a meeting point where multiple ISPs can connect and/or peer together. An ISP earns its money by ...

    FreeFileSync

    Built-in support for very long filenames (more than MAX_PATH = 260 characters) Synchronization database for propagation of deleted files and conflict detection Support for multiple folder pairs with...

    Introduction_to_Optimum_Design.pdf

    - **More on Step Size Determination:** Explores additional techniques for step size determination, including polynomial interpolation and inaccurate line search. - **Scaling of Design Variables:** ...

    poj题目分类...

    POJ(Purdue Online Judge)是一个知名的编程比赛平台,为编程爱好者和学生提供了大量的编程题目。然而,在做 POJ 题目的时候,如果没有分类的话,可能会感到很迷茫。因此,本文将对 POJ 题目进行分类,并对每个分类...

    tweenjs.min.js文件

    e.push(b):d[a]=[b],b},a.on=function(a,b,c,d,e,f){return b.handleEvent&&(c=c||b,b=b.handleEvent),c=c||this,this.addEventListener(a,function(a){b.call(c,a,e),d&&a.remove()},f)},a.removeEventListener=...

Global site tag (gtag.js) - Google Analytics