[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, ...
 
随机推荐
- Spring 日期时间处理
			
1 Spring自身的支持 1.1 factory-bean <bean id="dateFormat" class="java.text.SimpleDateFo ...
 - CoreOS, Kubernetes, etcd
			
CoreOS CoreOS Container Linux is the leading container operating system, designed to be managed and ...
 - javaWeb学习总结(10)- Filter(过滤器)学习
			
一.Filter简介 Filter也称之为过滤器,它是Servlet技术中最激动人心的技术,WEB开发人员通过Filter技术,对web服务器管理的所有 web资源:例如Jsp, Servlet, 静 ...
 - javaWeb学习总结(9)- JSTL标签库之核心标签
			
一.JSTL标签库介绍 JSTL标签库的使用是为弥补html标签的不足,规范自定义标签的使用而诞生的.使用JSLT标签的目的就是不希望在jsp页面中出现java逻辑代码 二.JSTL标签库的分类 核心 ...
 - 开涛spring3(2.1) - IoC基础
			
2.1.1 IoC是什么 Ioc—Inversion of Control,即“控制反转”,不是什么技术,而是一种设计思想.在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不是传统的在 ...
 - GitBook 使用
			
介绍 GitBook是一个基于Node.js的命令行工具,可使用 Github/Git和Markdown来制作精美的电子书,GitBook 并非关 Git的教程. 导出格式有PDF.HTML等,需要添 ...
 - Spring+SpringMVC+MyBatis深入学习及搭建(五)——动态sql
			
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6908763.html 前面有讲到Spring+SpringMVC+MyBatis深入学习及搭建(四)——My ...
 - VR全景项目领导者,VR全景智慧城市
			
在互联网大趋势下,实体商家都迫切需要一个好的线上广告宣传方式,来推广自己的店铺及产品,传统的线上宣传方式已经无法满足消费者需求,360度全景展示能更真实直观的把商家展示给用户消费者,给商家带来客流及收 ...
 - 结构化CSS设计思维
			
LESS.SASS等预处理器给CSS开发带来了语法的灵活和便利,其本身却没有给我们带来结构化设计思维.很少有人讨论CSS的架构设计,而很多框架本身,如Bootstrap确实有架构设计思维作为根基. 要 ...
 - 【MyBatis源码分析】insert方法、update方法、delete方法处理流程(下篇)
			
Configuration的newStatementHandler分析 SimpleExecutor的doUpdate方法上文有分析过: public int doUpdate(MappedState ...