当前位置: 首页 > news >正文

leetcode427.建立四叉树

 区间x0到x1和区间y0到y1都是左闭右开的
解题基本思路是先判断当前矩阵是不是全0或全1,如果是就直接返回新建的一个节点值(矩阵的统一值,叶子节点),如果不是那就新建一个节点值,非叶并且左上右上左下右下四个方向上递归创建节点

/*
// Definition for a QuadTree node.
class Node {public boolean val;public boolean isLeaf;public Node topLeft;public Node topRight;public Node bottomLeft;public Node bottomRight;public Node() {this.val = false;this.isLeaf = false;this.topLeft = null;this.topRight = null;this.bottomLeft = null;this.bottomRight = null;}public Node(boolean val, boolean isLeaf) {this.val = val;this.isLeaf = isLeaf;this.topLeft = null;this.topRight = null;this.bottomLeft = null;this.bottomRight = null;}public Node(boolean val, boolean isLeaf, Node topLeft, Node topRight, Node bottomLeft, Node bottomRight) {this.val = val;this.isLeaf = isLeaf;this.topLeft = topLeft;this.topRight = topRight;this.bottomLeft = bottomLeft;this.bottomRight = bottomRight;}
}
*/class Solution {private Node construct(int[][] grid, int x0, int y0, int x1, int y1) {for (int i = x0; i < x1; i++)for (int j = y0; j < y1; j++)if (grid[i][j] != grid[x0][y0])return new Node(true, false,construct(grid, x0, y0, (x0 + x1) / 2, (y0 + y1) / 2),construct(grid, x0, (y0 + y1) / 2, (x0 + x1) / 2, y1),construct(grid, (x0 + x1) / 2, y0, x1, (y0 + y1) / 2),construct(grid, (x0 + x1) / 2, (y0 + y1) / 2, x1, y1));return new Node(grid[x0][y0] == 1, true);}public Node construct(int[][] grid) {return construct(grid, 0, 0, grid.length, grid.length);}
}

 

http://www.dtcms.com/a/263019.html

相关文章:

  • 利润才是机器视觉企业的的“稳定器”,机器视觉企业的利润 = (规模经济 + 技术差异化 × 场景价值) - 竞争强度
  • ViT与CLIP:图像×文本 多模态读心术揭秘
  • 大数据系统架构实践(三):Hbase集群部署
  • 嘉讯科技:医疗信息化、数字化、智能化三者之间的关系和区别
  • EPLAN 中定制 自己的- A3 图框的详细指南(一)
  • 【机器学习深度学习】适合微调的模型选型指南
  • DAOS集群部署-Docker模式
  • CloudBase AI Toolkit 让我用“嘴”开发出的第一款网页游戏
  • 网络安全运维与攻防演练综合实训室解决方案
  • 服务器被入侵的常见迹象有哪些?
  • CentOS服务器SSH远程连接全指南
  • HarmonyOS NEXT应用元服务常见列表操作多类型列表项场景
  • 2025年数字信号、计算机通信与软件工程国际会议(DSCCSE 2025)
  • Excel 如何让表看起来更清晰、专业,而不是花里胡哨?
  • 低功耗MM32L0180系列MCU
  • 【Kafka】docker 中配置带 Kerberos 认证的 Kafka 环境(全过程)
  • [springboot系列] 探秘 JUnit 5:现代 Java 单元测试利器
  • Spring Boot 实现不同用户不同访问权限
  • 基于uniapp的老年皮肤健康管理微信小程序平台(源码+论文+部署+安装+售后)
  • 跨时间潜运动迁移以实现操作中的多帧预测
  • Instrct-GPT 强化学习奖励模型 Reward modeling 的训练过程原理实例化详解
  • nifi1.28.1集群部署详细记录
  • 大语言模型LLM在训练/推理时的padding
  • 用户行为序列建模(篇十一)-小结篇(篇一)
  • 如何读取运行jar中引用jar中的文件
  • C++ --- list
  • 《Effective Python》第十一章 性能——使用 timeit 微基准测试优化性能关键代码
  • 分发糖果
  • Spring Boot 集成 tess4j 实现图片识别文本
  • Springboot + vue + uni-app小程序web端全套家具商场