回复: 1
跳转到指定楼层
上一主题 下一主题
收起左侧

Valid square 和 follow up的时间复杂度

全局:

() @ - -   | | |

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

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

x
LC 593, 时间复杂度是O(1),因为只有四个点。

Follow up,给
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
(n^2)呢
  1. class Solution {
    . 1point3acres.com
  2.     // Calculate each pair's distance
  3.     // If distance ==0, return false
  4.     // else put distance into a set
  5.     // When finished, if set just contains 2 value
  6.     // return true; else return false.google  и
  7.    public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
  8.         // Tell if the point is valid
  9.         // Put four points into a list
  10.         List<int[]> list = new LinkedList<>();
  11.         list.add(p1);
  12.         list.add(p2);
  13.         list.add(p3);
  14.         list.add(p4);
  15.         .
  16.         for(int[] p : list){
  17.             if(p == null || p.length != 2)
  18.                 return false;
  19.         } ..
  20.         Set<Integer> set = new HashSet<>();
  21.         for(int i = 0; i < 3; i ++){
  22.             for(int j = i + 1; j < 4; j ++){
  23.                 int[] px = list.get(i);.
  24.                 int[] py = list.get(j);
  25.                 int distance = calDistance(px, py);
  26.                 if(distance == 0)
  27.                     return false;
  28.                 set.add(distance);. 1point 3 acres
  29.             }
  30.         }
  31.         return set.size() == 2;
  32.     }
  33.     private int calDistance(int[] p1, int[] p2){
  34.         int x1 = p1[0], y1 = p1[1];
  35.         int x2 = p2[0], y2 = p2[1];
  36.         return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);   
  37.     }
  38.     // The follow up question is, given a series of point, tell number of squares.
  39.     // First of all, tell if the list contains at least 4 points. 1point3acres.com
  40.    public int validSquareII(List<int[]> list){
  41.        if(list == null || list.size() < 4)
  42.            return 0; ..
  43.        int length = list.size();
  44.        int result = 0;
  45.        for(int i = 0; i < length - 3; i ++){
  46.            for(int j = i + 1; j < length - 2; j ++){
  47.                for(int k = j + 1; k < length -1; k ++){
  48.                    for(int l = k + 1; l < length; l ++){
  49.                        int[] p1 = list.get(i);
  50.                        int[] p2 = list.get(j);
  51.                        int[] p3 = list.get(k);
  52.                        int[] p4 = list.get(l);
  53.                        if (validSquare(p1,p2,p3,p4)){
  54.                            result ++;
  55.                        }
  56.                    }
  57.                }.google  и
  58.            }
  59.        }
  60.        return result;   
  61.    } . 1point3acres.com

  62. }
复制代码

评分

参与人数 2大米 +6 收起 理由
beer + 3 https://leetcode.com/problems/valid-squa
zhy119000 + 3 很有用的信息!

查看全部评分


上一篇:Wayfair Business Intelligence OA
下一篇:丢盒子 电面
🔗
namarie 2019-2-23 10:01:05 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
回复

使用道具 举报

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

本版积分规则

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