关注
楼主第二题能用你的样例测一下这个代码么,我用你的代码跑了一下,和我的输出是相同的,不知道错了哪个
样例。
import java.util.*; class Node {
String name; int parent; ArrayList<Node> children; Node() { this.children = new ArrayList<>(); }
} public class Mainb { public static void dfs(Node nodes, String s) { if (nodes.children.size() == 0) return; for (int i = 0; i < nodes.children.size(); i++) {
Node node = nodes.children.get(i); String s1; if (i != nodes.children.size() - 1)
s1 = s + "|"; else s1 = s + "`"; System.out.println(s1 + "-- " + node.name); if (node.children.size() > 0 && i != nodes.children.size() - 1) {
String ss = ""; if (s == "")
ss = s + " |"; else ss = s + "| "; dfs(node, ss); } else if (node.children.size() > 0 && i == nodes.children.size() - 1) {
String ss = ""; if (s == "")
ss = s + " "; else ss = s + " "; dfs(node, ss); }
}
} public static void main(String [] args) {
Scanner sc = new Scanner(System.in); int n = sc.nextInt(); Node [] nodes = new Node[n]; Node root; HashMap<Integer, Node> map = new HashMap<>(); for (int i = 0; i < n; i++) {
nodes[i] = new Node(); nodes[i].name = sc.next(); nodes[i].parent = sc.nextInt(); if (i == 0)
root = nodes[i]; map.put(i, nodes[i]); } for (int i = 1; i < n; i++) {
Node parent = map.get(nodes[i].parent); if (parent != null)
parent.children.add(nodes[i]); } for (int i = 0; i < n; i++) {
Collections.sort(nodes[i].children,new Comp()); }
System.out.println(nodes[0].name); String s = ""; dfs(nodes[0], s); }
} class Comp implements Comparator { public int compare(Object s11, Object s22) {
Node node1 = (Node) s11; Node node2 = (Node) s22; String s1 = node1.name; String s2 = node2.name; int n1 = s1.length(); int n2 = s2.length(); int min = Math.min(n1, n2); for (int i = 0; i < min; i++) { char c1 = s1.charAt(i); char c2 = s2.charAt(i); if (c1 != c2) {
c1 = Character.toUpperCase(c1); c2 = Character.toUpperCase(c2); if (c1 != c2) {
c1 = Character.toLowerCase(c1); c2 = Character.toLowerCase(c2); if (c1 != c2) { // No overflow because of numeric promotion return c1 - c2; }
}
}
} return n1 - n2; }
} /* 10 my-app -1 src 0 main 1 java 2 resources 2 webapp 2 test 1 java 6 resources 6 pom.xml 0 my-app |-- pom.xml `-- src |-- main | |-- java | |-- resources | `-- webapp `-- test |-- java `-- resources */
查看原帖
点赞 2
相关推荐
点赞 评论 收藏
分享
点赞 评论 收藏
分享
牛客热帖
更多
- 1... 你会和mentor进行deeptalk吗?2375
- 2... 双非本2025秋招总结:65w+SSP三选一,最终还是“有鹅选鹅”|附面试心路历程1900
- 3... 金丹后期牛友!我们新年再见1847
- 4... 希望新的一年,我依然是走向幸福的那个人1330
- 5... 牛客运营们,我保证这是我最后一次消费烤肠了!1231
- 6... 学院本 末 211 硕勇闯 java 后端实习美团 oc 逆袭指南1202
- 7... 27届学院本一段中厂一段中大厂实习,简历求锐评846
- 8... 元旦前被裁员了701
- 9... 27前端已没招641
- 10... 2025年牛客年度作者丨颁奖典礼✨615
正在热议
更多
# 对2025年忏悔 #
6484次浏览 119人参与
# 互联网行业现在还值得去吗 #
48081次浏览 356人参与
# 实习没人带,苟住还是跑路? #
14940次浏览 290人参与
# 春招前还要继续实习吗? #
7528次浏览 90人参与
# 一人说一家双休的公司 #
9348次浏览 111人参与
# 移动求职进展汇总 #
18852次浏览 149人参与
# 你找工作的时候用AI吗? #
166160次浏览 865人参与
# 国企秋招,你投了吗? #
55407次浏览 364人参与
# 元旦假期你打算怎么过 #
9781次浏览 187人参与
# 工作前VS工作后,你的心态变化 #
31715次浏览 249人参与
# 面试官问过你最刁钻的问题是什么? #
12016次浏览 112人参与
# 职场新人生存指南 #
491874次浏览 9518人参与
# 大家实习都在做什么? #
9627次浏览 102人参与
# 我的AI电子员工 #
24499次浏览 155人参与
# 我们是不是被“优绩主义”绑架了? #
10218次浏览 308人参与
# OPPO求职进展汇总 #
758878次浏览 5392人参与
# 你觉得专业和学校哪个对薪资影响最大 #
87886次浏览 587人参与
# 华为工作体验 #
279164次浏览 1360人参与
# 通信/硬件公司求职体验 #
184502次浏览 1032人参与
# 通信硬件薪资爆料 #
1189007次浏览 7185人参与