|
1 | 1 | package com.example; |
2 | 2 |
|
3 | | - |
4 | | -import com.inuker.solution.BinaryTreePreorderTraversal; |
5 | | -import com.inuker.solution.PathSumIII; |
6 | | -import com.leetcode.library.TreeNode; |
7 | | - |
8 | | -import java.sql.Timestamp; |
9 | | -import java.util.Calendar; |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.HashSet; |
10 | 6 | import java.util.LinkedList; |
11 | 7 | import java.util.List; |
12 | 8 |
|
13 | 9 | public class Main { |
14 | 10 |
|
15 | 11 | public static void main(String[] args) { |
16 | | - TreeNode nodef32 = new TreeNode(-3); |
17 | | - TreeNode node32 = new TreeNode(3, nodef32, null); |
18 | | - TreeNode nodef2 = new TreeNode(-2); |
19 | | - TreeNode node1 = new TreeNode(1); |
20 | | - TreeNode node11 = new TreeNode(11); |
21 | | - |
22 | | - TreeNode node3 = new TreeNode(3, node32, nodef2); |
23 | | - TreeNode node2 = new TreeNode(2, null, node1); |
24 | | - |
25 | | - TreeNode node5 = new TreeNode(5, node3, node2); |
26 | | - TreeNode nodef3 = new TreeNode(-3, null, node11); |
27 | | - |
28 | | - TreeNode root = new TreeNode(10, node5, nodef3); |
29 | | - |
30 | | -// List<Integer> list = new BinaryTreePreorderTraversal().preorderTraversal(root); |
31 | | -// for (Integer n : list) { |
32 | | -// System.out.print(n + " "); |
33 | | -// } |
34 | | - |
35 | | - List<String> paths = pathSum(root, 8); |
36 | | - for (String s : paths) { |
37 | | - System.out.println(s); |
38 | | - } |
| 12 | + boolean result = canArrangeWords(new String[] { |
| 13 | + "abc", "ce", "ef", "fu", "cd", "dx", "xc" |
| 14 | + }); |
| 15 | + System.out.print(result); |
39 | 16 | } |
40 | 17 |
|
41 | | - public static List<String> pathSum(TreeNode root, int sum) { |
42 | | - List<String> result = new LinkedList<>(); |
43 | | - pathSum(root, sum, result, ""); |
44 | | - return result; |
| 18 | + public static int perfectNumberCheck(int n) { |
| 19 | + int k = n; |
| 20 | + for (int i = 1; i < n; i++) { |
| 21 | + if (n % i == 0) { |
| 22 | + k -= i; |
| 23 | + } |
| 24 | + } |
| 25 | + return k == 0 ? 1 : 0; |
45 | 26 | } |
46 | 27 |
|
47 | | - private static void pathSum(TreeNode root, int sum, List<String> list, String path) { |
48 | | - if (root == null) { |
49 | | - return; |
| 28 | + public static boolean canArrangeWords(String[] words) { |
| 29 | + List<String> set = new ArrayList<String>(); |
| 30 | + if (helper(null, words, set)) { |
| 31 | + for (String s : set) { |
| 32 | + System.out.print(s + "->"); |
| 33 | + } |
| 34 | + return true; |
50 | 35 | } |
51 | | - |
52 | | - pathSumWithRoot(root, sum, list, path); |
53 | | - |
54 | | - pathSum(root.left, sum, list, ""); |
55 | | - pathSum(root.right, sum, list, ""); |
| 36 | + return false; |
56 | 37 | } |
57 | 38 |
|
58 | | - private static void pathSumWithRoot(TreeNode root, int sum, List<String> list, String path) { |
59 | | - if (root == null) { |
60 | | - return; |
| 39 | + public static boolean helper(String word, String[] words, List<String> set) { |
| 40 | + if (set.size() == words.length) { |
| 41 | + return true; |
61 | 42 | } |
62 | 43 |
|
63 | | - String prefix = path.isEmpty() ? "" : path + "->"; |
64 | | - |
65 | | - if (root.val == sum) { |
66 | | - list.add(prefix + root.val); |
| 44 | + for (String s : words) { |
| 45 | + if (!set.contains(s) && (word == null || s.charAt(0) == word.charAt(word.length() - 1))) { |
| 46 | + set.add(s); |
| 47 | + if (helper(s, words, set)) { |
| 48 | + return true; |
| 49 | + } |
| 50 | + set.remove(s); |
| 51 | + } |
67 | 52 | } |
68 | | - pathSumWithRoot(root.left, sum - root.val, list, prefix + root.val); |
69 | | - pathSumWithRoot(root.right, sum - root.val, list, prefix + root.val); |
| 53 | + return false; |
70 | 54 | } |
71 | 55 | } |
72 | 56 |
|
0 commit comments