zoukankan      html  css  js  c++  java
  • java 实现递归实现tree(2)

    import com.google.common.collect.Lists;
    import org.springframework.cglib.beans.BeanCopier;
    
    import java.util.*;
    import java.util.stream.Collectors;
    public class tset {
    	 private static Map<String, List<String>> map = new HashMap<>();
    
    	    static {
    	        map.put("2", Lists.newArrayList("A", "B", "C"));
    	        map.put("3", Lists.newArrayList("D", "E", "F"));
    	        map.put("4", Lists.newArrayList("G", "H", "I"));
    	        map.put("5", Lists.newArrayList("J", "K", "L"));
    	        map.put("6", Lists.newArrayList("M", "N", "O"));
    	        map.put("7", Lists.newArrayList("P", "Q", "R", "S"));
    	        map.put("8", Lists.newArrayList("T", "U", "V"));
    	        map.put("9", Lists.newArrayList("W", "X", "Y", "Z"));
    	    }
    
    	    public static void main(String[] args) {
    	        Scanner scanner = new Scanner(System.in);
    	        Map<String, List<Node>> nodeMap;
    	        while (true) {
    	            nodeMap = new HashMap<>();
    	            String num = scanner.next();
    	            char[] chars = num.toCharArray();
    	            LinkedList<Node> list = new LinkedList<>();
    	            if (chars.length == 1) {
    	                list.addAll(map.get(String.valueOf(chars[0])).stream().map(Node::new).collect(Collectors.toList()));
    	            } else {
    	                for (int i = 0; i < chars.length - 1; i++) {
    	                    char aChar = chars[i];
    	                    List<Node> collect0 = nodeMap.computeIfAbsent(String.valueOf(aChar), a -> map.get(String.valueOf(aChar)).stream().map(Node::new).collect(Collectors.toList()));
    	                    if (i == 0) {
    	                        list.addAll(collect0);
    	                    }
    	                    List<Node> collect = map.get(String.valueOf(chars[i + 1])).stream().map(Node::new).collect(Collectors.toList());
    	                    nodeMap.put(String.valueOf(chars[i + 1]), collect);
    	                    for (Node node : collect0) {
    	                        node.children = collect;
    	                    }
    	                }
    	            }
    	            String name;
    	            String preName;
    	            List<String> result = new ArrayList<>();
    	            while (!list.isEmpty()) {
    	                Node node = list.removeFirst();
    	                name = node.name;
    	                preName = node.preName != null ? node.preName + name : name;
    	                if (node.children != null) {
    	                    for (Node node1 : node.children) {
    	                        Node newNode = new Node();
    	                        BeanCopier beanCopier = BeanCopier.create(node1.getClass(), newNode.getClass(), false);
    	                        beanCopier.copy(node1, newNode, null);
    	                        newNode.preName = preName;
    	                        if (newNode.children != null) {
    	                            list.add(newNode);
    	                        } else {
    	                            result.add(preName + newNode.name);
    	                        }
    	                    }
    	                } else {
    	                    result.add(name);
    	                }
    	            }
    	            System.out.println(result);
    	        }
    
    	    }
    
    	    public static class Node {
    	        String name;
    	        String preName;
    	        List<Node> children;
    
    	        Node() {
    	        }
    
    	        Node(String name) {
    	            this.name = name;
    	        }
    
    	        public String getName() {
    	            return name;
    	        }
    
    	        public void setName(String name) {
    	            this.name = name;
    	        }
    
    	        public String getPreName() {
    	            return preName;
    	        }
    
    	        public void setPreName(String preName) {
    	            this.preName = preName;
    	        }
    
    	        public List<Node> getChildren() {
    	            return children;
    	        }
    
    	        public void setChildren(List<Node> children) {
    	            this.children = children;
    	        }
    	    }
    
    }
    

      

  • 相关阅读:
    对于指定区块div,如何区分区块内的点击 和 区块外的点击?
    broadcom代码中httpd进程启动流程介绍
    一个简单的搜索布局样式
    一种在视频OBJECT标签上放置均分四个区域的框选方法
    JQuery执行DOM批量克隆并插入的提效方法
    DevOps技术路线图
    后端开发技术路线图
    Angular route传参
    Angular使用echarts
    TypeScript Array Remove
  • 原文地址:https://www.cnblogs.com/-flq/p/10997656.html
Copyright © 2011-2022 走看看