Skip to content

Commit 299bce2

Browse files
committed
feat: add solutions to lc problem: No.0558
No.0558.Logical OR of Two Binary Grids Represented as Quad-Trees
1 parent 9f53859 commit 299bce2

File tree

6 files changed

+621
-2
lines changed

6 files changed

+621
-2
lines changed

solution/0500-0599/0558.Logical OR of Two Binary Grids Represented as Quad-Trees/README.md

Lines changed: 210 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,224 @@ class Node {
121121
<!-- 这里可写当前语言的特殊实现逻辑 -->
122122

123123
```python
124-
124+
"""
125+
# Definition for a QuadTree node.
126+
class Node:
127+
def __init__(self, val, isLeaf, topLeft, topRight, bottomLeft, bottomRight):
128+
self.val = val
129+
self.isLeaf = isLeaf
130+
self.topLeft = topLeft
131+
self.topRight = topRight
132+
self.bottomLeft = bottomLeft
133+
self.bottomRight = bottomRight
134+
"""
135+
136+
137+
class Solution:
138+
def intersect(self, quadTree1: "Node", quadTree2: "Node") -> "Node":
139+
def dfs(t1, t2):
140+
if t1.isLeaf and t2.isLeaf:
141+
return Node(t1.val or t2.val, True)
142+
if t1.isLeaf:
143+
return t1 if t1.val else t2
144+
if t2.isLeaf:
145+
return t2 if t2.val else t1
146+
res = Node()
147+
res.topLeft = dfs(t1.topLeft, t2.topLeft)
148+
res.topRight = dfs(t1.topRight, t2.topRight)
149+
res.bottomLeft = dfs(t1.bottomLeft, t2.bottomLeft)
150+
res.bottomRight = dfs(t1.bottomRight, t2.bottomRight)
151+
isLeaf = (
152+
res.topLeft.isLeaf
153+
and res.topRight.isLeaf
154+
and res.bottomLeft.isLeaf
155+
and res.bottomRight.isLeaf
156+
)
157+
sameVal = (
158+
res.topLeft.val
159+
== res.topRight.val
160+
== res.bottomLeft.val
161+
== res.bottomRight.val
162+
)
163+
if isLeaf and sameVal:
164+
res = res.topLeft
165+
return res
166+
167+
return dfs(quadTree1, quadTree2)
125168
```
126169

127170
### **Java**
128171

129172
<!-- 这里可写当前语言的特殊实现逻辑 -->
130173

131174
```java
175+
/*
176+
// Definition for a QuadTree node.
177+
class Node {
178+
public boolean val;
179+
public boolean isLeaf;
180+
public Node topLeft;
181+
public Node topRight;
182+
public Node bottomLeft;
183+
public Node bottomRight;
184+
185+
public Node() {}
186+
187+
public Node(boolean _val,boolean _isLeaf,Node _topLeft,Node _topRight,Node _bottomLeft,Node _bottomRight) {
188+
val = _val;
189+
isLeaf = _isLeaf;
190+
topLeft = _topLeft;
191+
topRight = _topRight;
192+
bottomLeft = _bottomLeft;
193+
bottomRight = _bottomRight;
194+
}
195+
};
196+
*/
197+
198+
class Solution {
199+
public Node intersect(Node quadTree1, Node quadTree2) {
200+
return dfs(quadTree1, quadTree2);
201+
}
202+
203+
private Node dfs(Node t1, Node t2) {
204+
if (t1.isLeaf && t2.isLeaf) {
205+
return new Node(t1.val || t2.val, true);
206+
}
207+
if (t1.isLeaf) {
208+
return t1.val ? t1 : t2;
209+
}
210+
if (t2.isLeaf) {
211+
return t2.val ? t2 : t1;
212+
}
213+
Node res = new Node();
214+
res.topLeft = dfs(t1.topLeft, t2.topLeft);
215+
res.topRight = dfs(t1.topRight, t2.topRight);
216+
res.bottomLeft = dfs(t1.bottomLeft, t2.bottomLeft);
217+
res.bottomRight = dfs(t1.bottomRight, t2.bottomRight);
218+
boolean isLeaf = res.topLeft.isLeaf && res.topRight.isLeaf && res.bottomLeft.isLeaf && res.bottomRight.isLeaf;
219+
boolean sameVal = res.topLeft.val == res.topRight.val && res.topRight.val == res.bottomLeft.val && res.bottomLeft.val == res.bottomRight.val;
220+
if (isLeaf && sameVal) {
221+
res = res.topLeft;
222+
}
223+
return res;
224+
}
225+
}
226+
```
227+
228+
### **C++**
229+
230+
```cpp
231+
/*
232+
// Definition for a QuadTree node.
233+
class Node {
234+
public:
235+
bool val;
236+
bool isLeaf;
237+
Node* topLeft;
238+
Node* topRight;
239+
Node* bottomLeft;
240+
Node* bottomRight;
241+
242+
Node() {
243+
val = false;
244+
isLeaf = false;
245+
topLeft = NULL;
246+
topRight = NULL;
247+
bottomLeft = NULL;
248+
bottomRight = NULL;
249+
}
250+
251+
Node(bool _val, bool _isLeaf) {
252+
val = _val;
253+
isLeaf = _isLeaf;
254+
topLeft = NULL;
255+
topRight = NULL;
256+
bottomLeft = NULL;
257+
bottomRight = NULL;
258+
}
259+
260+
Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {
261+
val = _val;
262+
isLeaf = _isLeaf;
263+
topLeft = _topLeft;
264+
topRight = _topRight;
265+
bottomLeft = _bottomLeft;
266+
bottomRight = _bottomRight;
267+
}
268+
};
269+
*/
270+
271+
class Solution {
272+
public:
273+
Node* intersect(Node* quadTree1, Node* quadTree2) {
274+
return dfs(quadTree1, quadTree2);
275+
}
276+
277+
Node* dfs(Node* t1, Node* t2) {
278+
if (t1->isLeaf && t2->isLeaf) return new Node(t1->val || t2->val, true);
279+
if (t1->isLeaf) return t1->val ? t1 : t2;
280+
if (t2->isLeaf) return t2->val ? t2 : t1;
281+
Node* res = new Node();
282+
res->topLeft = dfs(t1->topLeft, t2->topLeft);
283+
res->topRight = dfs(t1->topRight, t2->topRight);
284+
res->bottomLeft = dfs(t1->bottomLeft, t2->bottomLeft);
285+
res->bottomRight = dfs(t1->bottomRight, t2->bottomRight);
286+
bool isLeaf = res->topLeft->isLeaf && res->topRight->isLeaf && res->bottomLeft->isLeaf && res->bottomRight->isLeaf;
287+
bool sameVal = res->topLeft->val == res->topRight->val && res->topRight->val == res->bottomLeft->val && res->bottomLeft->val == res->bottomRight->val;
288+
if (isLeaf && sameVal) res = res->topLeft;
289+
return res;
290+
}
291+
};
292+
```
132293

294+
### **Go**
295+
296+
```go
297+
/**
298+
* Definition for a QuadTree node.
299+
* type Node struct {
300+
* Val bool
301+
* IsLeaf bool
302+
* TopLeft *Node
303+
* TopRight *Node
304+
* BottomLeft *Node
305+
* BottomRight *Node
306+
* }
307+
*/
308+
309+
func intersect(quadTree1 *Node, quadTree2 *Node) *Node {
310+
var dfs func(*Node, *Node) *Node
311+
dfs = func(t1, t2 *Node) *Node {
312+
if t1.IsLeaf && t2.IsLeaf {
313+
return &Node{Val: t1.Val || t2.Val, IsLeaf: true}
314+
}
315+
if t1.IsLeaf {
316+
if t1.Val {
317+
return t1
318+
}
319+
return t2
320+
}
321+
if t2.IsLeaf {
322+
if t2.Val {
323+
return t2
324+
}
325+
return t1
326+
}
327+
res := &Node{}
328+
res.TopLeft = dfs(t1.TopLeft, t2.TopLeft)
329+
res.TopRight = dfs(t1.TopRight, t2.TopRight)
330+
res.BottomLeft = dfs(t1.BottomLeft, t2.BottomLeft)
331+
res.BottomRight = dfs(t1.BottomRight, t2.BottomRight)
332+
isLeaf := res.TopLeft.IsLeaf && res.TopRight.IsLeaf && res.BottomLeft.IsLeaf && res.BottomRight.IsLeaf
333+
sameVal := res.TopLeft.Val == res.TopRight.Val && res.TopRight.Val == res.BottomLeft.Val && res.BottomLeft.Val == res.BottomRight.Val
334+
if isLeaf && sameVal {
335+
res = res.TopLeft
336+
}
337+
return res
338+
}
339+
340+
return dfs(quadTree1, quadTree2)
341+
}
133342
```
134343

135344
### **...**

0 commit comments

Comments
 (0)