1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
| public class Apple { public static class TreeNode { int val; TreeNode left; TreeNode right; public TreeNode(int val) { this.val = val; } } public static void main(String[] args) { TreeNode root = new TreeNode(0); TreeNode n1 = new TreeNode(1); TreeNode n2 = new TreeNode(2); TreeNode n3 = new TreeNode(3); TreeNode n4 = new TreeNode(4); TreeNode n5 = new TreeNode(5); TreeNode n6 = new TreeNode(6); TreeNode n7 = new TreeNode(7); root.left = n1; root.right = n2; n1.left = n5; n1.right = n6; n2.left = n3; n2.right = n4; n3.left = n7; System.out.println(findPathBetween(root, n2, n5)); } public boolean workBreak(String s, List<String> wordDict) { if (s == null || s.length() == 0 || wordDict == null || wordDict.size() == 0) { return false; } Set<String> wordSet = new HashSet<>(); int maxLen = 0; for (String word: wordDict) { wordSet.add(word); maxLen = Math.max(maxLen, word.length()); } boolean[] dp = new boolean [s.length() + 1]; for (int i = 1; i <= s.length(); i++) { for (int j = i - 1; j >= 0 && i - j <= maxLen; j--) { if (dp[j] && wordSet.contains(s.substring(j, i))) { dp[i] = true; break; } } } return dp[s.length()]; } public int firstUniqChar2(String s) { if (s == null || s.length() == 0) { return -1; } char[] sChar = s.toCharArray(); int[] bucket = new int [256]; for (int i = 0; i < sChar.length; i++) { bucket[sChar[i]]++; } for (int i = 0; i < sChar.length; i++) { if (bucket[sChar[i]] == 1) { return i; } } return -1; }
public int firstUniqChar1(String s) { if (s == null || s.length() == 0) { return -1; } char[] sChar = s.toCharArray(); int[] bucket = new int [256]; Map<Character, Integer> map = new LinkedHashMap<>(); for (int i = 0; i < sChar.length; i++) { if (bucket[sChar[i]]++ == 0) { map.put(sChar[i], i); } else { map.remove(sChar[i]); } } Iterator it = map.entrySet().iterator(); if (it.hasNext()) { Map.Entry pair = (Map.Entry)it.next(); return (Integer)pair.getValue(); } return -1; } private static TreeNode getLCA(TreeNode root, TreeNode n1, TreeNode n2) { if (root == null) { return null; } if (root == n1 || root == n2) { return root; } TreeNode left = getLCA(root.left, n1, n2); TreeNode right = getLCA(root.right, n1, n2); if (left != null && right != null) { return root; } if (left == null && right == null) { return null; } return left == null? right: left; }
private TreeNode prev1 = null; private boolean validateBST1(TreeNode root) { if (root == null) { return true; } if (!validateBST1(root.left)) { return false; } if (prev1 != null && prev1.val >= root.val) { return false; } prev1 = root; return validateBST1(root.right); } private boolean validateBST2(TreeNode root) { if (root == null) { return true; } Stack<TreeNode> stack = new Stack<>(); TreeNode prev = null; while (root != null || !stack.isEmpty()) { while (root != null) { stack.push(root); root = root.left; } root = stack.pop(); if (prev != null && prev.val >= root.val) { return false; } prev = root; root = root.right; } return true; } private boolean validateBST3(TreeNode root) { return dvcq(root, Long.MIN_VALUE, Long.MAX_VALUE); } private boolean dvcq(TreeNode root, long min, long max) { if (root == null) { return true; } if (root.val <= min || root.val >= max) { return false; } return dvcq(root.left, min, Math.min(root.val, max)) && dvcq(root.right, Math.max(root.val, min), max); } private static String findPathBetween(TreeNode root, TreeNode start, TreeNode end) { if (root == null) { return ""; } TreeNode lca = getLCA(root, start, end); List<TreeNode> pathToStart = new ArrayList<TreeNode>(); findPathTo(lca, start, pathToStart); List<TreeNode> pathToEnd = new ArrayList<TreeNode>(); findPathTo(lca, end, pathToEnd); StringBuilder sb = new StringBuilder(); for (int i = pathToStart.size() - 1; i > 0; i--) { if (sb.length() == 0) { sb.append(pathToStart.get(i).val); } else { sb.append("->"); sb.append(pathToStart.get(i).val); } } for (TreeNode node : pathToEnd) { sb.append("->"); sb.append(node.val); } return sb.toString(); } private static boolean findPathTo(TreeNode start, TreeNode end, List<TreeNode> curr) { curr.add(start); if (start == end) { return true; } else { if (start.left != null) { if (findPathTo(start.left, end, curr)) { return true; } } if (start.right != null) { if (findPathTo(start.right, end, curr)) { return true; } } } curr.remove(curr.size() - 1); return false; } }
|