版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址

http://blog.csdn.net/lzuacm

C#版 - Leetcode 593. 有效的正方形 - 题解

Leetcode 593. Valid Square

在线提交:

https://leetcode.com/problems/valid-square/

题目描述


给定二维空间中四点的坐标,返回四点是否可以构造一个正方形。

一个点的坐标(x, y)由一个有两个整数的整数数组表示。

示例 1:

输入: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1]

输出: True

Case 2:

输入:

  [ 0, 0 ]
  [ 0, 0 ]
  [ 0, 0 ]
  [ 0, 0 ]

输出:

False

Case 3:

输入:

  [ 1, 1 ]
  [ 0, 1 ]
  [ 1, 2 ]
  [ 0, 0 ]

输出:

False

注意:

  1. 所有输入整数都在 [-10000,10000] 范围内。
  2. 一个有效的正方形有四个等长的正长和四个等角(90度角)。
  3. 输入点没有顺序。

  ●  题目难度: Medium
  • 通过次数:164
  • 提交次数:484
  • 贡献者:fallcreek

  • 相关话题 数学


思路:

临界情况: 4个输入的点中有两个或多个相同,直接返回false。

方法1: 利用坐标系,向量长度和向量点乘来判断~

如果是复杂的向量、矩阵运算,还可使用开源的.net库mathnet,

C#中使用mathnet学习笔记(二) - 冰呆瓜 - 博客园

已AC代码:

using System.Collections.Generic;
using System.Linq;

namespace Leetcode539_ValidSquare
{
    public class Solution
    {
        public bool ValidSquare(int[] p1, int[] p2, int[] p3, int[] p4)
        {
            int[] vect1 = { p2[0] - p1[0], p2[1] - p1[1] };
            int[] vect2 = { p4[0] - p1[0], p4[1] - p1[1] };
            int[] vect3 = { p3[0] - p1[0], p3[1] - p1[1] };
            List<int[]> vects = new List<int[]> { vect1, vect2, vect3 };

            if (vects.Any(x => x.SequenceEqual(new[]{0, 0}))) // 输入的点中存在相同的
                return false;

            List<int> lenSquaresFromP1 = new List<int> { GetLenSquare(p2, p1), GetLenSquare(p4, p1), GetLenSquare(p3, p1) };
            List<int> extraLenSquares = new List<int> { GetLenSquare(p2, p3), GetLenSquare(p2, p4), GetLenSquare(p3, p4) };

            if (lenSquaresFromP1.Max() != extraLenSquares.Max())
                return false; // 当从p1出发的最长距离不为所有点两两之间距离的最大值时,只是菱形,不是正方形

            var maxLenSquare = lenSquaresFromP1.Max(); // 后面要remove, 此处作备份
            int maxPos = lenSquaresFromP1.IndexOf(maxLenSquare);
            lenSquaresFromP1.RemoveAt(maxPos);
            vects.RemoveAt(maxPos);

            if (lenSquaresFromP1[0] == lenSquaresFromP1[1] && lenSquaresFromP1[0] * 2 == maxLenSquare &&
                VectorCross(vects[0], vects[1]) == 0)
                return true;

            return false;
        }

        private int VectorCross(int[] vect1, int[] vect2) => vect1[0] * vect2[0] +
                                                             vect1[1] * vect2[1];

        private int GetLenSquare(int[] point1, int[] point2) => (point2[0] - point1[0]) * (point2[0] - point1[0]) +
                                                             (point2[1] - point1[1]) * (point2[1] - point1[1]);
    }

    class Program
    {
        static void Main(string[] args)
        {
            // 以下为test
            Solution sol = new Solution();
            //int[] p1 = { 0, 0 };
            //int[] p2 = { 0, 0 };
            //int[] p3 = { 0, 0 };
            //int[] p4 = { 0, 0 };

            //int[] p1 = { 1, 1 };
            //int[] p2 = { 0, 1 };
            //int[] p3 = { 1, 2 };
            //int[] p4 = { 0, 0 };
            int[] p1 = { 0, 0 };
            int[] p2 = { 1, 1 };
            int[] p3 = { 1, 0 };
            int[] p4 = { 0, 1 };
            bool result = sol.ValidSquare(p1, p2, p3, p4);
        }
    }
}

Rank:

You are here! Your runtime beats 30.77% of csharp submissions.

方法2: 判断4条边完全相等。

已AC代码:

public class Solution
{
    public bool ValidSquare(int[] p1, int[] p2, int[] p3, int[] p4)
    {
        if (GetLenSquare(p1, p2) == 0 || GetLenSquare(p2, p3) == 0 || GetLenSquare(p3, p4) == 0 || GetLenSquare(p1, p4) == 0)
            return false;

        return GetLenSquare(p1, p2) == GetLenSquare(p3, p4) && GetLenSquare(p1, p3) == GetLenSquare(p2, p4) && GetLenSquare(p1, p4) == GetLenSquare(p2, p3) &&
               (GetLenSquare(p1, p2) == GetLenSquare(p1, p3) || GetLenSquare(p1, p2) == GetLenSquare(p1, p4) || GetLenSquare(p1, p3) == GetLenSquare(p1, p4));
    }

    private int GetLenSquare(int[] point1, int[] point2) => (point2[0] - point1[0]) * (point2[0] - point1[0]) +
                                                         (point2[1] - point1[1]) * (point2[1] - point1[1]);
}

Rank:

You are here! Your runtime beats 69.23% of csharp submissions.

C#版 - Leetcode 593. 有效的正方形 - 题解的更多相关文章

  1. C#版 - Leetcode 13. 罗马数字转整数 - 题解

    C#版 - Leetcode 13. 罗马数字转整数 - 题解 Leetcode 13. Roman to Integer 在线提交: https://leetcode.com/problems/ro ...

  2. C#版 - Leetcode 633. 平方数之和 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  3. C#版 - Leetcode 414. Third Maximum Number题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  4. LeetCode 593. 有效的正方形(向量做法)

    题目 题目链接:593. 有效的正方形 题意:给出二维平面上四个点的坐标,判断这四个点是否能构成一个正方形,四个点的输入顺序不做任何保证. 思路 通过向量运算可以很轻松地解决这道题.任取一点向其他三点 ...

  5. Java实现 LeetCode 593 有效的正方形(判断正方形)

    593. 有效的正方形 给定二维空间中四点的坐标,返回四点是否可以构造一个正方形. 一个点的坐标(x,y)由一个有两个整数的整数数组表示. 示例: 输入: p1 = [0,0], p2 = [1,1] ...

  6. C#版[击败100.00%的提交] - Leetcode 6. Z字形变换 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  7. C#刷遍Leetcode面试题系列连载(5):No.593 - 有效的正方形

    上一篇 LeetCode 面试题中,我们分析了一道难度为 Easy 的数学题 - 自除数,提供了两种方法.今天我们来分析一道难度为 Medium 的面试题. 今天要给大家分析的面试题是 LeetCod ...

  8. C#版 - Leetcode 504. 七进制数 - 题解

    C#版 - Leetcode 504. 七进制数 - 题解 Leetcode 504. Base 7 在线提交: https://leetcode.com/problems/base-7/ 题目描述 ...

  9. C#版 - Leetcode 306. 累加数 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

随机推荐

  1. c++/qt的数据序列化和反序列化

    序列化以及反序列化的实现 struct Body { double weight; double height; }; //结构体 struct People { int age; Body dBod ...

  2. 第六届Code+程序设计网络挑战赛

    弹指能算(easy)模式只做出一道签到题,还WA了一发,我好菜啊啊啊啊...虽然我菜,但是比赛体验一点都不好,服务器老是崩. 比赛链接:https://moocoder.xuetangx.com/de ...

  3. hive动态分区和混合分区

    各位看官,今天我们来讨论下再Hive中的动态分区和混合分区方面的一些知识点以及相关的一些问题. 前面我们已经讲过管理表和外部表的一般分区的一些知识点,对于需要对表创建很多的分区,那么用户就需要些很多的 ...

  4. springmvc是如何工作的

    上图便是springmvc的工作流程,看着条条框框的,其实说的直白一点,springmvc就是负责处理用户的需求(request/url),它的负责人(核心组件)就是前端控制器(DispatcherS ...

  5. :nth-child() 与 :nth-of-type(n)的区别

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. centos7安装kubeadm

    安装配置docker v1.9.0版本推荐使用docker v1.12, v1.11, v1.13, 17.03也可以使用,再高版本的docker可能无法正常使用. 测试发现17.09无法正常使用,不 ...

  7. 再回首数据结构—数组(Golang实现)

    数组为线性数据结构,通常编程语言都有自带了数组数据类型结构,数组存放的是有个相同数据类型的数据集: 为什么称数组为线性数据结构:因为数组在内存中是连续存储的数据结构,数组中每个元素最多只有左右两个方向 ...

  8. Java 将容器List里面的内容保存到数组

    import java.util.List; import java.util.ArrayList; public class listToArr { public static void main( ...

  9. C#线程 ---- 线程同步详解

    线程同步 说明:接上一篇,注意分享线程同步的必要性和线程同步的方法. 测试代码下载:https://github.com/EkeSu/C-Thread-synchronization-C-.git 一 ...

  10. cf 1142 C

    忽然觉得这个题很有必要写题解. 移项 y-x^2 = bx+c 那么其实就是找有多少条直线上方没有点 所以就是一个上凸壳有多少条直线/点. 绝妙啊!!!! 太妙了啊!!!! 神乎其技卧槽!!! (我是 ...