小米 小米Git
来源:互联网 发布:java程序员工作总结 编辑:程序博客网 时间:2024/04/30 01:15
题目描述
git是一种分布式代码管理工具,git通过树的形式记录文件的更改历史,比如: base’<–base<–A<–A’ ^ | — B<–B’ 小米工程师常常需要寻找两个分支最近的分割点,即base.假设git 树是多叉树,请实现一个算法,计算git树上任意两点的最近分割点。 (假设git树节点数为n,用邻接矩阵的形式表示git树:字符串数组matrix包含n个字符串,每个字符串由字符’0’或’1’组成,长度为n。matrix[i][j]==’1’当且仅当git树种第i个和第j个节点有连接。节点0为git树的根节点。)
输入例子:
[01011,10100,01000,10000,10000],1,2
输出例子:
1
/** * 返回git树上两点的最近分割点 * 即求多叉树的两个节点的公共父节点, * 从树的跟节点按层往下遍历多叉树,记录每个节点的父节点和深度 * * @param matrix 接邻矩阵,表示git树,matrix[i][j] == '1' 当且仅当git树中第i个和第j个节点有连接,节点0为git树的跟节点 * @param indexA 节点A的index * @param indexB 节点B的index * @return 整型 */ public int getSplitNode(String[] matrix, int indexA, int indexB) { int n = matrix.length; //每个节点的父节点 int[] parent = new int[n]; //每个节点的深度 int[] depth = new int[n]; char[][] tree = new char[n][]; Queue<Integer> que = new LinkedList<>(); for (int i = 0; i < n; i++) { tree[i] = matrix[i].toCharArray(); parent[i] = -1; depth[i] = 1; } que.add(0); parent[0] = 0; //从跟节点往下遍历多叉树,按层遍历 while (!que.isEmpty()) { int v = que.poll(); for (int i = 0; i < n; i++) { if (tree[v][i] == '1' && parent[i] == -1) { que.add(i); parent[i] = v; depth[i] = depth[v] + 1; } } } while (depth[indexA] > depth[indexB]) { indexA = parent[indexA]; } while (depth[indexA] < depth[indexB]) { indexB = parent[indexB]; } while (indexA != indexB) { indexA = parent[indexA]; indexB = parent[indexB]; } return indexA; }
1 0
- 小米 小米Git
- 小米Git
- 小米Git
- 小米Git
- 小米Git
- 小米Git
- 小米Git
- 小米Git
- 小米Git
- 小米笔试题-小米Git
- 2016小米-小米Git-Java
- 小米
- 算法题-小米Git
- 算法: 小米Git
- [编程题]小米git
- 小米git笔试题
- [小米面试题]小米Git
- 牛客网---2016---小米Git
- 【华为 OJ 】 字符串匹配
- Java实现1+2+3+4+5+......+99+100的和
- POJ 1094 拓扑排序
- [iOS 报错] LunchImage 加载报错 "LaunchI did not have any applicable content.
- 源代码形式编译 Qt 的操作步骤指南Instructions for building QT from source code
- 小米 小米Git
- Python中的循环以及break/continue/else/pass
- 数字图像处理学习笔记五
- android开发 在隐藏statusbar及navigationbar
- 项目08-03
- Sorting Json Array
- Android测试工具UiAutomator(Demo篇)
- redis和jedis在java中的基本用法
- CocosCreator自定义事件的清除方法