摘要:實(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ì)稱,x坐標(biāo)左右全部對(duì)稱,就是說[[-1,1], [1, 1], [3, 1], [-3, 1]]就是對(duì)的,但是[[1, 1], [3, 1], [-3, 1]]就不對(duì),因?yàn)閇1, 1]沒有和它對(duì)稱的點(diǎn)。[[1, 1], [3, 1]]也是對(duì)的,這時(shí)候x坐標(biāo)關(guān)于x = 2對(duì)稱。[[-1, 1], [1, 1], [-2, -1], [2, -1]]也是對(duì)的,都是關(guān)于x = 0這個(gè)y軸對(duì)稱。
那么關(guān)鍵就是要求一下對(duì)稱軸,x最大值和最小值的中點(diǎn)就是對(duì)稱軸,先用hashset存一下所有的點(diǎn),然后根據(jù)對(duì)稱軸找對(duì)稱點(diǎn)是否在set里面,從而判斷是否正確。x1 - pivot == pivot - x2, x1 > x2。
public class Solution { public boolean isReflected(int[][] points) { if(points.length == 0) return true; // x value and y value Setset = new HashSet(); int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE; for(int[] point : points) { set.add(new Point(point[0], point[1])); min = Math.min(min, point[0]); max = Math.max(max, point[0]); } int d = min + max; for(int[] point : points) { if(!set.contains(new Point(d - point[0], point[1]))) return false; } return true; } class Point { int x; int y; Point(int x, int y) { this.x = x; this.y = y; } @Override public int hashCode() { return this.x * this.y; } @Override public boolean equals(Object o) { if(!(o instanceof Point)) return false; Point p = (Point) o; return this.x == p.x && this.y == p.y; } } }
看了discussion里面直接用string加上分隔符來表示點(diǎn),這樣不用自己定義class簡(jiǎn)單點(diǎn)。
public class Solution { public boolean isReflected(int[][] points) { if(points.length == 0) return true; // x value and y value Setset = new HashSet(); int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE; for(int[] point : points) { set.add(point[0] + "#" + point[1]); min = Math.min(min, point[0]); max = Math.max(max, point[0]); } int d = min + max; for(int[] point : points) { if(!set.contains(d - point[0] + "#" + point[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/66663.html
摘要:題目解法這道題主要是判斷個(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 gi...
摘要:?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...
閱讀 2755·2021-09-13 10:26
閱讀 2011·2021-09-03 10:28
閱讀 2096·2019-08-30 15:44
閱讀 895·2019-08-29 14:07
閱讀 470·2019-08-29 13:12
閱讀 2224·2019-08-26 11:44
閱讀 2411·2019-08-26 11:36
閱讀 2090·2019-08-26 10:19