摘要:題目解法這道題主要是判斷個(gè)點(diǎn)是否沿某條線對(duì)稱,可以從提示看出來所有的點(diǎn)應(yīng)該要滿足所以先把所有的點(diǎn)掃一遍存下來,找到和然后再掃一遍,判定是否點(diǎn)都是延直線對(duì)稱的。時(shí)間復(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個(gè)點(diǎn)是否沿某條線對(duì)稱,可以從提示看出來所有的點(diǎn)應(yīng)該要滿足 2Y = minX + maxX;所以先把所有的點(diǎn)掃一遍存下來,找到minX和minX. 然后再掃一遍,判定是否點(diǎn)都是延直線對(duì)稱的。 時(shí)間復(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)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/66358.html
摘要:實(shí)際上這題的要求是所有點(diǎn)的關(guān)于一個(gè)軸對(duì)稱,坐標(biāo)左右全部對(duì)稱,就是說就是對(duì)的,但是就不對(duì),因?yàn)闆]有和它對(duì)稱的點(diǎn)。也是對(duì)的,都是關(guān)于這個(gè)軸對(duì)稱。看了里面直接用加上分隔符來表示點(diǎn),這樣不用自己定義簡(jiǎn)單點(diǎn)。 356. Line Reflection 題目鏈接:https://leetcode.com/problems... 這題給的例子太神了根本看不懂。實(shí)際上這題的要求是所有點(diǎn)的關(guān)于一個(gè)y軸對(duì)...
摘要:?jiǎn)栴}解答這個(gè)解法是看的里的,看著簡(jiǎn)單,但想到很難。我們要求是不是對(duì)稱,就是要求每一個(gè)點(diǎn)是不是有個(gè)點(diǎn)跟它對(duì)應(yīng)。因?yàn)榭梢砸粋€(gè)點(diǎn)重復(fù)出現(xiàn),決定我們用來做。記錄每一個(gè)出現(xiàn)的點(diǎn),然后再用來找其對(duì)應(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è)計(jì)代碼,本文主要學(xué)習(xí)的反射特性,來提高寫代碼時(shí)的設(shè)計(jì)質(zhì)量。提供一套檢測(cè)的兩個(gè)工具包和,類似于探針一樣的東西來探測(cè)這些一等公民。限于篇幅,下篇再聊下反射。 說明:Laravel中經(jīng)常使用PHP的反射特性來設(shè)計(jì)代碼,本文主要學(xué)習(xí)PHP的反射特性,來提高寫代碼時(shí)的設(shè)計(jì)質(zhì)量。PHP提供一套檢測(cè)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...
閱讀 3518·2021-11-24 09:39
閱讀 1943·2021-11-17 09:33
閱讀 3953·2021-10-12 10:12
閱讀 5276·2021-09-22 15:51
閱讀 1192·2019-08-30 13:11
閱讀 3647·2019-08-30 10:59
閱讀 680·2019-08-30 10:48
閱讀 1415·2019-08-26 13:48