Line Reflection
来源:互联网 发布:python 迭代器 生成器 编辑:程序博客网 时间:2024/04/29 22:29
Given n points on a 2D plane, find if there is such a line parallel to y-axis that reflect the given points.
Example 1:
Given points = [[1,1],[-1,1]]
, return true
.
Example 2:
Given points = [[1,1],[-1,-1]]
, return false
.
Follow up:
Could you do better than O(n2)?
Hint:
- Find the smallest and largest x-value for all points.
- If there is a line then it should be at y = (minX + maxX) / 2.
- For each point, make sure that it has a reflected point in the opposite side.
Credits:
Special thanks to @memoryless for adding this problem and creating all test cases.
public class Solution { public boolean isReflected(int[][] points) { if(points == null ) return false; int minX = Integer.MAX_VALUE; int maxX = Integer.MIN_VALUE; HashMap<Integer, Set<Integer>> hashmap = new HashMap<Integer, Set<Integer>>(); for(int i=0; i<points.length; i++){ minX = Math.min(minX, points[i][0]); maxX = Math.max(maxX, points[i][0]); if(hashmap.containsKey(points[i][0])){ hashmap.get(points[i][0]).add(points[i][1]); } else { Set<Integer> hashset = new HashSet<Integer>(); hashset.add(points[i][1]); hashmap.put(points[i][0], hashset); } } double mid = (minX + maxX)/2.0; for(int i=0; i<points.length; i++){ int key = (int)(2*mid-points[i][0]); if(!hashmap.containsKey(key) || !hashmap.get(key).contains(points[i][1])){ return false; } } return true; }}
0 0
- Line Reflection
- Line Reflection
- LeetCode 356. Line Reflection
- LeetCode 356. Line Reflection
- 356. Line Reflection
- LeetCode 356. Line Reflection(线反射)
- [leetcode] 356. Line Reflection 解题报告
- [Leetcode] 356. Line Reflection 解题报告
- Reflection
- Reflection
- Reflection
- Reflection
- Reflection
- Reflection
- Reflection
- Reflection
- LINE
- line
- [LeetCode338]Counting Bits
- 运算符重载
- 4_7计数
- MySQL事件Event
- 指针铁律2/3:间接赋值是指针存在的最大意义
- Line Reflection
- [LeetCode104]Maximum Depth of Binary Tree
- 【ES6】对象的扩展2
- 唐灵波_2015级精英班_2015~2016第一学期总结
- Timer和TimerTask总结
- 配置nagios监控oracle,check_oracle_health
- 基于Netty 的webSocket开发
- HTTP协议1
- 网络编程(8)—— IP地址和域名之间的转换