Problem statement:

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:

  1. All the input integers are in the range [-10000, 10000].
  2. A valid square has four equal sides with positive length and four equal angles (90-degree angles).
  3. Input points have no order.

Solution one: a set to find the relative of four points(AC).

As the second problem in this contest, the key points are to find the right order of these four points and check the if one/two/three/fours points are overlapped. It will be very easier if we know the relative positions of these four points. The answer comes out to check the length of four sides and two diagonals.

I use the default sorting characteristic of a set instead of designing an algorithm to find their relative positions.

The final order after sorting by a set is 0, 1, 3, 2 in counterclockwise.

The efficiency does not matter in this problem, correctness is on the top.

class Solution {
public:
bool validSquare(vector<int>& p1, vector<int>& p2, vector<int>& p3, vector<int>& p4) {
vector<vector<int>> points(, vector<int>(, ));
set<vector<int>> points_set;
points_set.insert(p1);
points_set.insert(p2);
points_set.insert(p3);
points_set.insert(p4);
if(points_set.size() != ){
return false;
}
int idx = ;
for(auto point : points_set){
points[idx] = point;
idx++;
}
if( dis_square(points[], points[]) == dis_square(points[], points[])
&& dis_square(points[], points[]) == dis_square(points[], points[])
&& dis_square(points[], points[]) == dis_square(points[], points[])
&& dis_square(points[], points[]) == dis_square(points[], points[])
&& dis_square(points[], points[]) == dis_square(points[], points[])){
return true;
}
return false;
}
private:
int dis_square(vector<int> p1, vector<int> p2){
return (p1[] - p2[]) * (p1[] - p2[]) + (p1[] - p2[]) * (p1[] - p2[]);
}
};

Solution two: STL sort algorithm(AC). This is concise and easy to understand(Better).

Another good alternative approach to find the relative position of these four points are the sort algorithm in STL.

The default sorting algorithm sort the first element and sort the second element. The sorted order is still 0, 1, 3, 2 in counterclockwise.

But, we need to check if one or more positions are overlapped before sorting.

class Solution {
public:
bool validSquare(vector<int>& p1, vector<int>& p2, vector<int>& p3, vector<int>& p4) {
// check if one or more points are overlapped
if(p1 == p2 || p1 == p3 || p1 == p4 || p2 == p3 || p2 == p4 || p3 == p4){
return false;
}
vector<vector<int>> points = {p1, p2, p3, p4};
sort(points.begin(), points.end());
if(dis_square(points[], points[]) == dis_square(points[], points[]) // check four sides
&& dis_square(points[], points[]) == dis_square(points[], points[])
&& dis_square(points[], points[]) == dis_square(points[], points[])
&& dis_square(points[], points[]) == dis_square(points[], points[])
// check the diagonals
&& dis_square(points[], points[]) == dis_square(points[], points[])){
return true;
}
return false;
}
private:
int dis_square(vector<int> p1, vector<int> p2){
return (p1[] - p2[]) * (p1[] - p2[]) + (p1[] - p2[]) * (p1[] - p2[]);
}
};

最新文章

  1. Webservice 65535 错误
  2. C语言 自动修改文件名小程序
  3. p4 是否能自动merge
  4. E2 2014.6.3 更新日志
  5. BZOJ 1005: [HNOI2008]明明的烦恼 Purfer序列 大数
  6. mysql中使用count()统计的特殊之处
  7. 20160327javaweb 之JSP入门
  8. 【转】android UI进阶之实现listview中checkbox的多选与记录--不错
  9. 对Big O的新的认识
  10. shell中break 与 continue
  11. 再起航,我的学习笔记之JavaScript设计模式05(简单工程模式)
  12. [Swift]LeetCode605. 种花问题 | Can Place Flowers
  13. Python手记(二)
  14. vue和webpack打包 项目相对路径修改
  15. mysql 解锁
  16. dom4j 通过 org.dom4j.DocumentFactory 设置命名空间来支持 带namespace 的 xpath
  17. Cisco交换机堆叠与HSRP之间的区别
  18. VLC简介及使用说明
  19. 关于easyUI分页
  20. 修改数据库的instance_name和db_name

热门文章

  1. Java多线程——线程的优先级和生命周期
  2. poj3050 Hopscotch
  3. git + git flow 的简单介绍
  4. 【工具】sublime使用技巧
  5. 从单机到2000万 QPS 并发的 Redis 高性能缓存实践之路
  6. React全家桶之一 react-router之高级
  7. Java语法基础-final关键字
  8. CAS server 连接mysql的deployerConfigContext.xml配置
  9. 【C++】模板简述(四):模板为什么不支持分离编译?
  10. 解决国内无法安装android sdk的问题