海康Java笔试编程第二题 - 部门树结构

海康编程题有毒,算法不是重点,输入输出字符串才是重点。没怎么做过这种输入输出,没有在规定时间内做出来。
以下为完整代码(未在线测试过),处理输入构建树结构,然后树的深度遍历输出所有路径。
import java.util.*;
import java.lang.*;
/*
1,A,0;2,B,1;3,C,2;4,D,2
A-B-C;A-B-D

1,A,0;B,1,2;
false

1,A,0;2,B,1;3,C,2;4,D,2;
A-B-C;A-B-D

2,A,0;1,B,1;3,C,2;
false
*/
public class Main {
    public static final String ERROR = "incorrect data";
    public static Map<Integer, Node> nodesMap = new HashMap<>();
    public static boolean findFirstPath = true;

    public static boolean parseNode(String[] arrs) {
        for (int i = 0; i < arrs.length; i++) {
            String[] paramsStr = arrs[i].split(",");
            if (paramsStr.length != 3) return false;
            int ID = -1;
            int upID = -1;
            try {
                ID = Integer.parseInt(paramsStr[0]);
                upID = Integer.parseInt(paramsStr[2]);
            } catch (NumberFormatException e) {
                return false;
            }

            String name = paramsStr[1];

            if ( (ID == -1 || upID == -1) || (ID == 1 && upID != 0) || (upID == 0 && ID != 1) )
                return false;

            Node node;
            if ( !nodesMap.containsKey(ID) ) {
                node = new Node(ID, name);
                nodesMap.put(ID, node);
            }
            node = nodesMap.get(ID);
            node.setName(name);

            Node pNode = null;
            if ( !nodesMap.containsKey(upID) ) {
                pNode = new Node(upID, "");
                nodesMap.put(upID, pNode);
            }
            pNode = nodesMap.get(upID);
            pNode.addChild(ID);
        }

        return true;
    }


    public static void solution(String[] arrs) {

        if ( !parseNode(arrs) ) {
            System.out.println(ERROR);
            return;
        }

        Node startRoot = nodesMap.get(0);
        if (startRoot == null || startRoot.kidsID.size() != 1 || startRoot.kidsID.get(0) != 1 ) {
            System.out.println(ERROR);
            return;
        }

        depthTravel(nodesMap.get(1), "");
    }

    public static void depthTravel(Node node, String path) {
        if (node.kidsID.size() == 0) {
            path += node.name;
            if (!findFirstPath) {
                System.out.printf(";");
            }
            System.out.printf("%s", path);
            findFirstPath = false;
            return;
        }

        path = path + node.name + "-";
        for (int i = 0; i < node.kidsID.size(); i++) {
            int nextId = node.kidsID.get(i);
            depthTravel( nodesMap.get(nextId), path);
        }
    }

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        if ( !cin.hasNextLine() ) {
            System.out.println(ERROR);
        }
        String line = cin.nextLine();
        String[] arrs = line.split(";");

        solution(arrs);
    }
}

class Node{
    public int ID;
    public String name;
    public List<Integer> kidsID;

    public Node(int ID, String name)  {
        this.ID = ID;
        this.name = name;
        kidsID = new LinkedList<>();
    }

    public void setName(String name) {
        this.name = name;
    }

    public void addChild(int curkidID) {
        kidsID.add(curkidID);
    }
}


全部评论
😔同样被淘汰了,可能我还太菜了吧
点赞 回复 分享
发布于 2017-09-16 00:26

相关推荐

ResourceUt...:楼主有自己的垃圾箱,公司也有自己的人才库
点赞 评论 收藏
分享
评论
点赞
12
分享

创作者周榜

更多
正在热议
更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务