|
| 1 | +package com.chen.algorithm.znn; |
| 2 | + |
| 3 | + |
| 4 | +import org.junit.Test; |
| 5 | + |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.HashMap; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Map; |
| 10 | + |
| 11 | +public class InterviewTest { |
| 12 | + |
| 13 | + /* |
| 14 | + [ |
| 15 | +{id:1, name:xx1, pid:0}, |
| 16 | +{id:2, name:xx2, pid:0}, |
| 17 | +{id:3, name:xx3, pid:1}, |
| 18 | +{id:4, name:xx4, pid:3}, |
| 19 | +{id:5, name:xx5, pid:4}, |
| 20 | +] |
| 21 | + */ |
| 22 | + |
| 23 | + class Node{ |
| 24 | + private int id; |
| 25 | + private String name; |
| 26 | + private int pid; |
| 27 | + private List<Node> children; |
| 28 | + public Node(){} |
| 29 | + public Node(int pid){ |
| 30 | + this.pid = pid; |
| 31 | + } |
| 32 | + |
| 33 | + public Node(int id,String name, int pid){ |
| 34 | + this.id = id; |
| 35 | + this.name = name; |
| 36 | + this.pid = pid; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + public Node getNode(List<Node> nodeList){ |
| 41 | + if(nodeList == null || nodeList.size() == 0){ |
| 42 | + return null; |
| 43 | + } |
| 44 | + Map<Integer,List<Node>> nodeMap = new HashMap<>(); |
| 45 | + Map<Integer, Node> originNodeMap = new HashMap<>(); |
| 46 | + |
| 47 | + for(int i =0;i<nodeList.size();i++){ |
| 48 | + Node originNode = nodeList.get(i); |
| 49 | + int pid = originNode.pid; |
| 50 | + originNodeMap.put(originNode.id, originNode); |
| 51 | + |
| 52 | + if(!nodeMap.containsKey(pid)){ |
| 53 | + nodeMap.put(pid, Arrays.asList(originNode)); |
| 54 | + }else{ |
| 55 | + nodeMap.get(pid).add(originNode); |
| 56 | + } |
| 57 | + } |
| 58 | + Node root = new Node(-1,"root",-2); |
| 59 | + nodeMap.keySet().forEach(pid->{ |
| 60 | + Node fNode = originNodeMap.get(pid); |
| 61 | + if(fNode == null){ |
| 62 | + fNode = new Node(pid,"xxxpid",-1); |
| 63 | + if(root.children == null){ |
| 64 | + root.children = Arrays.asList(fNode); |
| 65 | + }else{ |
| 66 | + root.children.add(fNode); |
| 67 | + } |
| 68 | + } |
| 69 | + fNode.children = nodeMap.get(pid); |
| 70 | + }); |
| 71 | + return root; |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void test(){ |
| 76 | + |
| 77 | +// [ |
| 78 | +// {id:1, name:xx1, pid:0}, |
| 79 | +// {id:2, name:xx2, pid:0}, |
| 80 | +// {id:3, name:xx3, pid:1}, |
| 81 | +// {id:4, name:xx4, pid:3}, |
| 82 | +// {id:5, name:xx5, pid:4}, |
| 83 | +//] |
| 84 | + System.out.println(isHuiwen("12321")); |
| 85 | + } |
| 86 | + |
| 87 | + public boolean isPalling(String s){ |
| 88 | + if(s == null || s.length() == 0){ |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | + int len = s.length(); |
| 93 | + boolean[][] dp = new boolean[len][len]; |
| 94 | + for(int x = 0;x<len;x++){ |
| 95 | + for(int i =0;i+x<len;i++){ |
| 96 | + int j = i+x; |
| 97 | + if(x == 0){ |
| 98 | + dp[i][j] = true; |
| 99 | + }else if(x == 1){ |
| 100 | + dp[i][j] = s.charAt(i) == s.charAt(j); |
| 101 | + }else{ |
| 102 | + dp[i][j] =(dp[i+1][j-1]) &&s.charAt(i) == s.charAt(j); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + return dp[0][len-1]; |
| 107 | + } |
| 108 | + |
| 109 | + public boolean isHuiwen(String s){ |
| 110 | + if(s == null || s.length() == 0){ |
| 111 | + return false; |
| 112 | + } |
| 113 | + if(s.length() == 1){ |
| 114 | + return true; |
| 115 | + } |
| 116 | + int len = s.length(); |
| 117 | + int j =len; |
| 118 | + for(int i =0;i<len/2;i++){ |
| 119 | + char head = s.charAt(i); |
| 120 | + char tail = s.charAt(j-1); |
| 121 | + if(head != tail){ |
| 122 | + return false; |
| 123 | + } |
| 124 | + j--; |
| 125 | + } |
| 126 | + return true; |
| 127 | + } |
| 128 | +} |
0 commit comments