摘要:題目解法這道題主要是判斷個點(diǎn)是否沿某條線對稱,可以從提示看出來所有的點(diǎn)應(yīng)該要滿足所以先把所有的點(diǎn)掃一遍存下來,找到和然后再掃一遍,判定是否點(diǎn)都是延直線對稱的。時間復(fù)雜度空間復(fù)雜度代碼
題目:
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.
解法:
這道題主要是判斷N個點(diǎn)是否沿某條線對稱,可以從提示看出來所有的點(diǎn)應(yīng)該要滿足 2Y = minX + maxX;所以先把所有的點(diǎn)掃一遍存下來,找到minX和minX. 然后再掃一遍,判定是否點(diǎn)都是延直線對稱的。 時間復(fù)雜度O(n),空間復(fù)雜度O(n).
代碼:
public class Solution { public boolean isReflected(int[][] points) { int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE; Setset = new HashSet (); for (int[] p : points) { set.add(p[0] + "," + p[1]); min = Math.min(min, p[0]); max = Math.max(max, p[0]); } int sum = min + max; for (int[] p : points) { if (!set.contains((sum - p[0]) + "," + p[1])) { return false; } } return true; } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/66358.html
摘要:實(shí)際上這題的要求是所有點(diǎn)的關(guān)于一個軸對稱,坐標(biāo)左右全部對稱,就是說就是對的,但是就不對,因?yàn)闆]有和它對稱的點(diǎn)。也是對的,都是關(guān)于這個軸對稱??戳死锩嬷苯佑眉由戏指舴麃肀硎军c(diǎn),這樣不用自己定義簡單點(diǎn)。 356. Line Reflection 題目鏈接:https://leetcode.com/problems... 這題給的例子太神了根本看不懂。實(shí)際上這題的要求是所有點(diǎn)的關(guān)于一個y軸對...
摘要:問題解答這個解法是看的里的,看著簡單,但想到很難。我們要求是不是對稱,就是要求每一個點(diǎn)是不是有個點(diǎn)跟它對應(yīng)。因?yàn)榭梢砸粋€點(diǎn)重復(fù)出現(xiàn),決定我們用來做。記錄每一個出現(xiàn)的點(diǎn),然后再用來找其對應(yīng)的點(diǎn)。 問題:Given n points on a 2D plane, find if there is such a line parallel to y-axis that reflect the...
摘要:說明中經(jīng)常使用的反射特性來設(shè)計代碼,本文主要學(xué)習(xí)的反射特性,來提高寫代碼時的設(shè)計質(zhì)量。提供一套檢測的兩個工具包和,類似于探針一樣的東西來探測這些一等公民。限于篇幅,下篇再聊下反射。 說明:Laravel中經(jīng)常使用PHP的反射特性來設(shè)計代碼,本文主要學(xué)習(xí)PHP的反射特性,來提高寫代碼時的設(shè)計質(zhì)量。PHP提供一套檢測class, interface, trait, property, me...
Problem Given a non-empty 2D array grid of 0s and 1s, an island is a group of 1s (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are s...
閱讀 3476·2021-11-24 09:39
閱讀 1863·2021-11-17 09:33
閱讀 3814·2021-10-12 10:12
閱讀 5223·2021-09-22 15:51
閱讀 1166·2019-08-30 13:11
閱讀 3626·2019-08-30 10:59
閱讀 645·2019-08-30 10:48
閱讀 1375·2019-08-26 13:48