查看: 4553| 回复: 3
跳转到指定楼层
上一主题 下一主题
收起左侧

[其他] 15行搞定leetcode难题max points on a line

全局:

2014(1-3月)-CS硕士+fresh grad 无实习或全职 | Other| 码农类General全职@

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
. Χ

评分

参与人数 2大米 +8 收起 理由
writecoffee1 + 5 Thanks for sharing!!!
BreakingDown-_- + 3 很有用的信息!

查看全部评分


上一篇:一般实习的时候公司会负责保险吗?
下一篇:请问大家一个微软入职Relocation的问题,谢谢。
全局:
Java 20 ~ 23 行也可以搞定了, hahahahahahahahahaha
  1. public int maxPoints(Point[] points) {
  2.     int n = points.length;. 1point 3acres
  3.     int gMax = 0;
  4. -baidu 1point3acres
  5.     for (int i = 0; i < n; i++) {
  6.         HashMap<Double, Integer> h = new HashMap<Double, Integer>();
  7.         int lMax = 1, dup = 0;. 1point 3 acres

  8.         for (int j = i + 1; j < n; j++) {
  9.             if (points[j].x == points[i].x && points[j].y == points[i].y) {
  10.                 dup++;. 1point3acres.com
  11.             } else {
  12.                 double dx = points[j].x - points[i].x, dy = points[j].y - points[i].y;
  13.                 double slope = dx == 0.0 ? Integer.MAX_VALUE : 0.0 + dy / dx;

  14.                 if (!h.containsKey(slope)) {
  15.                     h.put(slope, 1);
  16.                 }
  17. . check 1point3acres for more.
  18.                 lMax = Math.max(lMax, h.put(slope, h.get(slope) + 1) + 1);
  19.             }
  20.         }. .и

  21.         gMax = Math.max(lMax + dup, gMax);
  22.     }
  23. .--
  24.     return gMax;
  25. }
复制代码
回复

使用道具 举报

推荐
 楼主| Linzertorte 2014-5-15 05:07:01 | 只看该作者
全局:
  1. # Definition for a point
  2. # class Point:
  3. #     def __init__(self, a=0, b=0):
  4. #         self.x = a
  5. #         self.y = b
  6. .--
  7. class Solution:
  8.     # @param points, a list of Points
  9.     # @return an integer
  10.     def maxPoints(self, points):
  11.         best = 0. 1point 3acres
  12.         for p in points:
  13.             angle_cnt=dict()
  14.             same = 0
  15.             lm = 0
  16.             for q in points:
  17.                 angle=math.atan2(p.y-q.y,p.x-q.x)
  18.                 if p.x==q.x and p.y==q.y:
  19.                     same += 1
  20.                 else:. 1point3acres.com
  21.                     angle_cnt[angle] = angle_cnt[angle]+1 if angle in angle_cnt else 1
  22.                     lm=max(lm,angle_cnt[angle])
  23.             best=max(best,same+lm)
  24.         return best
复制代码
一亩三分地严打"顶""好贴""收藏了"之类的垃圾回复帖!被警告三次,系统会自动封杀ID!
. check 1point3acres for more.
想支持楼主,请点击帖子下方的"好苗""分享""收藏"键,酌情给楼主加大米(系统不扣你自己的分)。
积分不够看不了帖子,请参考论坛导航里的"帮助","新手提纲"里有攒积分指南
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表