日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

LeetCode 2013. 检测正方形(字典)

發布時間:2024/7/5 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 LeetCode 2013. 检测正方形(字典) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

    • 1. 題目
    • 2. 解題
      • 2.1 超時
      • 2.1 改進

1. 題目

給你一個在 X-Y 平面上的點構成的數據流。設計一個滿足下述要求的算法:

  • 添加 一個在數據流中的新點到某個數據結構中。可以添加 重復 的點,并會視作不同的點進行處理。
  • 給你一個查詢點,請你從數據結構中選出三個點,使這三個點和查詢點一同構成一個 面積為正 的 軸對齊正方形 ,統計 滿足該要求的方案數目

軸對齊正方形 是一個正方形,除四條邊長度相同外,還滿足每條邊都與 x-軸 或 y-軸 平行或垂直。

實現 DetectSquares 類:

  • DetectSquares() 使用空數據結構初始化對象
  • void add(int[] point) 向數據結構添加一個新的點 point = [x, y]
  • int count(int[] point) 統計按上述方式與點 point = [x, y] 共同構造 軸對齊正方形 的方案數。

示例:

輸入: ["DetectSquares", "add", "add", "add", "count", "count", "add", "count"] [[], [[3, 10]], [[11, 2]], [[3, 2]], [[11, 10]], [[14, 8]], [[11, 2]], [[11, 10]]] 輸出: [null, null, null, null, 1, 0, null, 2]解釋: DetectSquares detectSquares = new DetectSquares(); detectSquares.add([3, 10]); detectSquares.add([11, 2]); detectSquares.add([3, 2]); detectSquares.count([11, 10]); // 返回 1 。你可以選擇:// - 第一個,第二個,和第三個點 detectSquares.count([14, 8]); // 返回 0 。查詢點無法與數據結構中的這些點構成正方形。 detectSquares.add([11, 2]); // 允許添加重復的點。 detectSquares.count([11, 10]); // 返回 2 。你可以選擇:// - 第一個,第二個,和第三個點// - 第一個,第三個,和第四個點提示: point.length == 2 0 <= x, y <= 1000 調用 add 和 count 的 總次數 最多為 5000

來源:力扣(LeetCode) 鏈接:https://leetcode-cn.com/problems/detect-squares
著作權歸領扣網絡所有。商業轉載請聯系官方授權,非商業轉載請注明出處。

2. 解題

2.1 超時

50 / 51 個通過測試用例

class DetectSquares:def __init__(self):self.x = {}self.y = {}self.p = {}def add(self, point: List[int]) -> None:if (point[0], point[1]) not in self.p:self.p[(point[0], point[1])] = 1else:self.p[(point[0], point[1])] += 1if point[0] not in self.x:self.x[point[0]] = [(point[0], point[1])]else:self.x[point[0]].append((point[0], point[1]))if point[1] not in self.y:self.y[point[1]] = [(point[0], point[1])]else:self.y[point[1]].append((point[0], point[1]))def count(self, point: List[int]) -> int:ans = 0if point[0] in self.x and point[1] in self.y:for xp in self.x[point[0]]:if xp[1] == point[1]:continued = abs(xp[1]-point[1])for yp in self.y[point[1]]:if yp[0] == point[0]:continueif abs(yp[0]-point[0])==d and (yp[0], xp[1]) in self.p:ans += self.p[(yp[0], xp[1])]return ans

2.1 改進

  • 枚舉對角線的另一點
class DetectSquares:def __init__(self):self.p = {}def add(self, point: List[int]) -> None:if (point[0], point[1]) not in self.p:self.p[(point[0], point[1])] = 1else:self.p[(point[0], point[1])] += 1def count(self, point: List[int]) -> int:ans = 0for p, num in self.p.items():if p[0]==point[0] or p[1]==point[1] or abs(p[0]-point[0]) != abs(p[1]-point[1]):continuep1 = (p[0], point[1])p2 = (point[0], p[1])if p1 in self.p and p2 in self.p:ans += num*self.p[p1]*self.p[p2] return ans

1520 ms 16.7 MB Python3


我的CSDN博客地址 https://michael.blog.csdn.net/

長按或掃碼關注我的公眾號(Michael阿明),一起加油、一起學習進步!

總結

以上是生活随笔為你收集整理的LeetCode 2013. 检测正方形(字典)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。