20240711 Finished

This commit is contained in:
jackfiled 2024-07-11 10:20:39 +08:00
parent c779dcecf8
commit bf2d07f763
2 changed files with 154 additions and 0 deletions

View File

@ -0,0 +1,17 @@
namespace LeetCodeSharp.Tests;
using LeetCodeSharp.Problems427;
public class P427Tests
{
[Fact]
public void Test1()
{
int[][] grid = [[0, 1], [1, 0]];
Solution solution = new();
Node root = solution.Construct(grid);
Assert.False(root.isLeaf);
}
}

View File

@ -0,0 +1,137 @@
// [427] Construct Quad Tree
namespace LeetCodeSharp.Problems427
{
// Submission codes start here
/*
// Definition for a QuadTree node.
public class Node {
public bool val;
public bool isLeaf;
public Node topLeft;
public Node topRight;
public Node bottomLeft;
public Node bottomRight;
public Node() {
val = false;
isLeaf = false;
topLeft = null;
topRight = null;
bottomLeft = null;
bottomRight = null;
}
public Node(bool _val, bool _isLeaf) {
val = _val;
isLeaf = _isLeaf;
topLeft = null;
topRight = null;
bottomLeft = null;
bottomRight = null;
}
public Node(bool _val,bool _isLeaf,Node _topLeft,Node _topRight,Node _bottomLeft,Node _bottomRight) {
val = _val;
isLeaf = _isLeaf;
topLeft = _topLeft;
topRight = _topRight;
bottomLeft = _bottomLeft;
bottomRight = _bottomRight;
}
}
*/
public class Node
{
public bool val;
public bool isLeaf;
public Node topLeft;
public Node topRight;
public Node bottomLeft;
public Node bottomRight;
public Node()
{
val = false;
isLeaf = false;
topLeft = null;
topRight = null;
bottomLeft = null;
bottomRight = null;
}
public Node(bool _val, bool _isLeaf)
{
val = _val;
isLeaf = _isLeaf;
topLeft = null;
topRight = null;
bottomLeft = null;
bottomRight = null;
}
public Node(bool _val, bool _isLeaf, Node _topLeft, Node _topRight, Node _bottomLeft, Node _bottomRight)
{
val = _val;
isLeaf = _isLeaf;
topLeft = _topLeft;
topRight = _topRight;
bottomLeft = _bottomLeft;
bottomRight = _bottomRight;
}
}
public class Solution
{
public Node Construct(int[][] grid)
{
var length = grid.Length;
return ConstructRecursive(grid, 0, 0, length);
}
private Node ConstructRecursive(int[][] grid, int startX, int startY, int length)
{
if (IsSameGrid(grid, startX, startY, length))
{
return new Node(grid[startX][startY] == 1, true);
}
var middleX = startX + length / 2;
var middleY = startY + length / 2;
var middleLength = length / 2;
var root = new Node
{
topLeft = ConstructRecursive(grid, startX, startY, middleLength),
topRight = ConstructRecursive(grid, startX, middleY, middleLength),
bottomLeft = ConstructRecursive(grid, middleX, startY, middleLength),
bottomRight = ConstructRecursive(grid, middleX, middleY, middleLength)
};
return root;
}
private bool IsSameGrid(int[][] grid, int startX, int startY, int length)
{
var value = grid[startX][startY];
for (var i = 0; i < length; i++)
{
for (var j = 0; j < length; j++)
{
if (grid[startX + i][startY + j] != value)
{
return false;
}
}
}
return true;
}
}
// Submission codes end here
}