zoukankan      html  css  js  c++  java
  • 一 克隆图

    广搜

    133 Clone Graph

        public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
            if (node == null) return node;
            LinkedList<UndirectedGraphNode> queue = new LinkedList<>();
            HashMap<UndirectedGraphNode, UndirectedGraphNode> map = new HashMap<>();
            queue.offer(node);
            UndirectedGraphNode copy = new UndirectedGraphNode(node.label);
            map.put(node, copy);
            while (!queue.isEmpty()){
                UndirectedGraphNode cur = queue.poll();
                for (int i = 0; i < cur.neighbors.size(); i++){
                    if (!map.containsKey(cur.neighbors.get(i))){
                        copy = new UndirectedGraphNode(cur.neighbors.get(i).label);
                        map.put(cur.neighbors.get(i), copy);
                        queue.offer(cur.neighbors.get(i));
                    }
                    map.get(cur).neighbors.add(map.get(cur.neighbors.get(i)));
                }
            }
            return map.get(node);
        }
    View Code
        public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
            if (node == null) return node;
            LinkedList<UndirectedGraphNode> stack = new LinkedList<>();
            HashMap<UndirectedGraphNode, UndirectedGraphNode> map = new HashMap<>();
            stack.push(node);
            UndirectedGraphNode copy = new UndirectedGraphNode(node.label);
            map.put(node, copy);
            while (!stack.isEmpty()){
                UndirectedGraphNode cur = stack.poll();
                for (int i = 0; i < cur.neighbors.size(); i++){
                    if(!map.containsKey(cur.neighbors.get(i))){
                        copy = new UndirectedGraphNode(cur.neighbors.get(i).label);
                        map.put(cur.neighbors.get(i), copy);
                        stack.push(cur.neighbors.get(i));
                    }
                    map.get(cur).neighbors.add(map.get(cur.neighbors.get(i)));  
                }
                
            }
            return map.get(node);
        }
    View Code
        public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {  
            if(node == null)  
                return null;  
            HashMap<UndirectedGraphNode, UndirectedGraphNode> map = new HashMap<UndirectedGraphNode, UndirectedGraphNode>();  
            UndirectedGraphNode copy = new UndirectedGraphNode(node.label);  
            map.put(node,copy);  
            helper(node,map);  
            return copy;  
        }  
        private void helper(UndirectedGraphNode node, HashMap<UndirectedGraphNode, UndirectedGraphNode> map)  
        {  
            for(int i=0;i<node.neighbors.size();i++)  
            {   
                UndirectedGraphNode cur = node.neighbors.get(i);  
                if(!map.containsKey(cur))  
                {  
                    UndirectedGraphNode copy = new UndirectedGraphNode(cur.label);  
                    map.put(cur,copy);  
                    helper(cur,map);  
                }  
                map.get(node).neighbors.add(map.get(cur));  
            }  
        }  
    View Code
  • 相关阅读:
    微信公众平台接口测试账号申请
    Windows平台下tomcat+java的web程序持续占cpu问题调试
    mysql存储过程基础
    Mysql权限控制
    MySQL开启federated引擎实现数据库表映射
    JAVA中Set集合--HashSet的使用
    在js中嵌套java代码
    MySql计算两个日期的时间差函数
    关于easyui combobox下拉框实现多选框的实现
    利用excel办公软件快速拼凑sql语句
  • 原文地址:https://www.cnblogs.com/whesuanfa/p/7007843.html
Copyright © 2011-2022 走看看