leetcode 558. Logical OR of Two Binary Grids Represented as Quad-Trees | 558. 四叉树交集(分治法)
生活随笔
收集整理的這篇文章主要介紹了
leetcode 558. Logical OR of Two Binary Grids Represented as Quad-Trees | 558. 四叉树交集(分治法)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題目
https://leetcode.com/problems/logical-or-of-two-binary-grids-represented-as-quad-trees/
題解
自己寫的沒通過,這題不方便構(gòu)造測(cè)試用例,沒有找到問題在哪兒。后來看了答案:https://leetcode.com/problems/logical-or-of-two-binary-grids-represented-as-quad-trees/discuss/157532/Java-concise-code-beat-100
class Solution {public Node intersect(Node n1, Node n2) {if (n1.isLeaf) return n1.val ? n1 : n2;if (n2.isLeaf) return n2.val ? n2 : n1;n1.topLeft = intersect(n1.topLeft, n2.topLeft);n1.topRight = intersect(n1.topRight, n2.topRight);n1.bottomLeft = intersect(n1.bottomLeft, n2.bottomLeft);n1.bottomRight = intersect(n1.bottomRight, n2.bottomRight);// try merge leafif (n1.topLeft.isLeaf && n1.topRight.isLeaf && n1.bottomLeft.isLeaf && n1.bottomRight.isLeaf &&n1.topLeft.val == n1.topRight.val && n1.topRight.val == n1.bottomLeft.val && n1.bottomLeft.val == n1.bottomRight.val) {n1.isLeaf = true;n1.val = n1.topRight.val;n1.topLeft = null;n1.topRight = null;n1.bottomLeft = null;n1.bottomRight = null;}return n1;} }總結(jié)
以上是生活随笔為你收集整理的leetcode 558. Logical OR of Two Binary Grids Represented as Quad-Trees | 558. 四叉树交集(分治法)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: leetcode 174. Dungeo
- 下一篇: leetcode 55. Jump Ga