关注
楼主第二题能用你的样例测一下这个代码么,我用你的代码跑了一下,和我的输出是相同的,不知道错了哪个
样例。
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
牛客热帖
更多
正在热议
更多
# 为了去实习,我赌上了___ #
6298次浏览 67人参与
# 2025年终总结 #
879次浏览 27人参与
# 哪一瞬间让你觉得“这班不如不上” #
3555次浏览 65人参与
# 父母对你找工作是助力还是阻力? #
5173次浏览 108人参与
# 十二月请对我好一点 #
12369次浏览 207人参与
# 一人推荐一个值得做的项目 #
4178次浏览 68人参与
# 滴滴工作体验 #
35220次浏览 145人参与
# uu们,春招你还来吗? #
2907次浏览 33人参与
# 工作前VS工作后,你的心态变化 #
5514次浏览 70人参与
# 得物app工作体验 #
39293次浏览 97人参与
# 高薪高压 vs 低薪wlb,你怎么选? #
4886次浏览 52人参与
# 你的实习什么时候入职 #
321357次浏览 2170人参与
# 工作中出现了XX情况正常吗 #
14866次浏览 142人参与
# 产品实习,你更倾向大公司or小公司 #
184560次浏览 2040人参与
# 秋招有哪些公司要求提前实习 #
91646次浏览 492人参与
# 公司福利里最没用的一项是啥 #
3222次浏览 64人参与
# 被AI治愈的瞬间 #
82249次浏览 674人参与
# 回顾今年你干过的最“勇”的一件事 #
6324次浏览 92人参与
# 产品人求职现状 #
294369次浏览 2357人参与
# 入职第四天,心情怎么样 #
42982次浏览 462人参与

