[leetcode-593-Valid Square]
Given the coordinates of four points in 2D space, return whether the four points could construct a square.
The coordinate (x,y) of a point is represented by an integer array with two integers.
Example:
Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1]
Output: True
Note:
All the input integers are in the range [-10000, 10000].
A valid square has four equal sides with positive length and four equal angles (90-degree angles).
Input points have no order.
思路:
4个点,求出任意两点之间的距离,总共6条线段,存在其中4条相等,而且另外两条也相等就是正方形。
double length(vector<int>& p1, vector<int>& p2)
{
int x = abs(p1[] - p2[]);
int y = abs(p1[] - p2[]);
return x*x + y*y;
}
bool validSquare(vector<int>& p1, vector<int>& p2, vector<int>& p3, vector<int>& p4)
{
map<double,int>table;
table[length(p1,p2)]++;
table[length(p1,p3)]++;
table[length(p1,p4)]++;
table[length(p2,p3)]++;
table[length(p2,p4)]++;
table[length(p3,p4)]++;
if(table.size()!=) return false; map<double ,int>::iterator it = table.begin();
map<double ,int>::iterator temp = it;
temp++;
if((it->second == && temp->second == ) ||(it->second == && temp->second ==) )return true;
return false;
}
参考:
http://blog.csdn.net/yuer158462008/article/details/47374545
[leetcode-593-Valid Square]的更多相关文章
- LeetCode 题解 593. Valid Square (Medium)
LeetCode 题解 593. Valid Square (Medium) 判断给定的四个点,是否可以组成一个正方形 https://leetcode.com/problems/valid-squa ...
- 【LeetCode】593. Valid Square 解题报告(Python)
[LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 593. Valid Square
Problem statement: Given the coordinates of four points in 2D space, return whether the four points ...
- LC 593. Valid Square
Given the coordinates of four points in 2D space, return whether the four points could construct a s ...
- C#版 - Leetcode 593. 有效的正方形 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- [LeetCode] Valid Square 验证正方形
Given the coordinates of four points in 2D space, return whether the four points could construct a s ...
- [Swift]LeetCode593. 有效的正方形 | Valid Square
Given the coordinates of four points in 2D space, return whether the four points could construct a s ...
- [LeetCode] Longest Valid Parentheses
第一种方法,用栈实现,最容易想到,也比较容易实现,每次碰到‘)’时update max_len,由于要保存之前的‘(’的index,所以space complexity 是O(n) // 使用栈,时间 ...
- [LeetCode] 036. Valid Sudoku (Easy) (C++)
指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 036. ...
- LeetCode:36. Valid Sudoku,数独是否有效
LeetCode:36. Valid Sudoku,数独是否有效 : 题目: LeetCode:36. Valid Sudoku 描述: Determine if a Sudoku is valid, ...
随机推荐
- [刷题]算法竞赛入门经典 3-7/UVa1368 3-8/UVa202 3-9/UVa10340
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 都是<算法竞赛入门经典(第二版)>的题目,标题上没写(第二版) 题目:算法竞赛入门经典 3-7/UVa13 ...
- anjular中Service、Factory、Provider的使用与js中创建对象的总结
在学习anjular中Service的使用时,发现和js中的创建对象的方式有一定的联系,所以总结了anjular中Service.Factory.Provider的使用方式与js创建对象的方式 一.先 ...
- 【JAVAWEB学习笔记】15_request
HttpServletRequest 学习目标 案例一.完成用户注册 案例二.完成登录错误信息的回显 1.HttpServletRequest概述 我们在创建Servlet时会覆盖service()方 ...
- 最全DOS的CMD命令,程序员必会
CMD命令:开始->运行->键入cmd或command(在命令行里可以看到系统版本.文件系统版本)1. appwiz.cpl:程序和功能 2. calc:启动计算器 3. certmgr. ...
- 《高性能javascript》 --- in case of odd number of items(奇怪的条目的数量)
不知道是做着故意放的还是什么原因.总之运行后就会出现问题(奇怪的条目的数量) function merge(left, right){ var result = []; while (left.len ...
- iOS 生成随机字符串 从指定字符串随机产生n个长度的新字符串
随机字符串 - 生成指定长度的字符串 -(NSString *)randomStringWithLength:(NSInteger)len { NSString *letters = @"a ...
- Java学习笔记——MySQL开放3306接口与设置用户权限
系统Ubuntu16.04 LTS 1.开放3306端口查看端口状态:netstat -an|grep 3306tcp 0 0 127.0.0.1:3306 ...
- Flume简介及安装
Hadoop业务的大致开发流程以及Flume在业务中的地位: 从Hadoop的业务开发流程图中可以看出,在大数据的业务处理过程中,对于数据的采集是十分重要的一步,也是不可避免的一步,从而引出我们本文的 ...
- crontab表达式执行时间计算,crontab在线测试
熟悉Unix和Linux的朋友都知道Crontab表达式,通过crontab指令可以周期性调用或执行某个程序. 但是大家写完crontab表达式后,心里总是担心表达式写的不对,可以又没法去验证.比 ...
- 2016计蒜之道复赛B题:联想专卖店促销
题解 思路: 二分答案,设我们要check的值为x. 注意到每一个礼包都有,一个U盘,一个鼠标. 剩余的,分别为一个机械键盘,一个U盘,一个鼠标. 当礼包数目为x时,我们至多可以提供a-x个普通,b- ...