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

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
分享到:
评论

相关推荐

    cpp-算法精粹

    仅仅是作为搬运工。...Max Points on a Line Community QQ 群: 237669375 Github: https://www.github.com/soulmachine/algorithm-essentials 微博: @灵魂机器 License Book License: CC BY-SA 3.0 License

    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 殷康 张一鸣 赵伟 任杰 唐波 ...

    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 ...

    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...

    计算机网络第六版答案

    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 ...

    Graphics Gems (Vol.3)

    7. Nonuniform Random Points Sets via Warping 80 Peter Shirley 8. Cross Product in Four Dimensions and Beyond 84 Ronald N. Goldman 9. Face-Connected Line Segment Generation in an n-Dimensional Space C ...

    Introduction_to_Optimum_Design.pdf

    4.5.3 Effect of Scaling a Constraint on Its Lagrange Multiplier 147 4.5.4 Generalization of Constraint Variation Sensitivity Result 148 4.6 Global Optimality 149 4.6.1 Convex Sets 149 4.6.2 Convex ...

    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=...

    python3.6.5参考手册 chm

    PEP 519: Adding a file system path protocol PEP 495: Local Time Disambiguation PEP 529: Change Windows filesystem encoding to UTF-8 PEP 528: Change Windows console encoding to UTF-8 PEP 520: ...

    occam一维反演

    c descr = description line in iteration files c modfil = model file name c datfil = data file name c c parameters integer iof1, iof2 parameter (iof1 = 13, iof2 = 15) c iof1 = unit number for the LOG...

    eac3to V3.17

    * damaged first max 5MB and max 5% of a TS/m2ts file are automatically skipped * video/audio tracks which can't be parsed, are now demuxed in raw form * added support for "line 21" closed captions in ...

Global site tag (gtag.js) - Google Analytics