public class 剑指Offer_35_复杂链表的复制 {
public Node copyRandomList(Node head) {
if (head == null) return null;
Map<Node, Node> hashMap = new HashMap<>();
Node cur = head;
while (cur != null) {
hashMap.put(cur, new Node(cur.val));
cur = cur.next;
}
cur = head;
while (cur != null) {
hashMap.get(cur).next = hashMap.get(cur.next);
hashMap.get(cur).random = hashMap.get(cur.random);
cur = cur.next;
}
return hashMap.get(head);
}
public Node copyRandomList2(Node head) {
if (head == null) return null;
Node cur = head;
while (cur != null) {
Node copyNode = new Node(cur.val);
copyNode.next = cur.next;
cur.next = copyNode;
cur = copyNode.next;
}
cur = head;
while (cur != null) {
Node copy = cur.next;
if (cur.random != null) {
copy.random = cur.random.next;
}
cur = copy.next;
}
cur = head;
Node newHead = cur.next;
while (cur.next != null) {
Node nextNode = cur.next;
cur.next = nextNode.next;
cur = nextNode;
}
return newHead;
}
}
public class 剑指Offer_36_二叉搜索树与双向链表 {
TreeNode pre, head;
public TreeNode treeToDoublyList(TreeNode root) {
if (root == null) return null;
inOrder(root);
pre.right = head;
head.left = pre;
return head;
}
private void inOrder(TreeNode cur) {
if (cur == null) return;
inOrder(cur.left);
if (pre != null) pre.right = cur;
else {
head = cur;
}
cur.left = pre;
pre = cur;
inOrder(cur.right);
}
}
public class 剑指Offer_37_序列化二叉树 {
public String serialize(TreeNode root) {
if (root == null) return "#!";
StringBuilder sb = new StringBuilder();
postSerialize(root, sb);
return sb.toString();
}
private void postSerialize(TreeNode root, StringBuilder sb) {
if (root.left == null) {
sb.append("#!");
} else {
postSerialize(root.left, sb);
}
if (root.right == null) {
sb.append("#!");
} else {
postSerialize(root.right, sb);
}
sb.append(root.val).append("!");
}
public TreeNode deserialize(String data) {
String[] strings = data.split("!");
Deque<String> deque = new LinkedList<>();
for (String string : strings) {
deque.addFirst(string);
}
return postDeserialize(deque);
}
private TreeNode postDeserialize(Deque<String> deque) {
String poll = deque.poll();
if (poll.equals("#")) return null;
TreeNode head = new TreeNode(Integer.valueOf(poll));
head.right = postDeserialize(deque);
head.left = postDeserialize(deque);
return head;
}
public static void main(String[] args) {
String s = "#";
System.out.println(s == "#");
}
}
public class 剑指Offer_38_字符串的排列 {
public String[] permutation(String s) {
char[] chars = s.toCharArray();
if (chars == null) return null;
List<String> list = new ArrayList<>();
if (chars.length == 0) return new String[0];
dfs(0, chars, list);
String[] result = new String[list.size()];
for (int i = 0; i < list.size(); i++) {
result[i] = list.get(i);
}
return result;
}
private void dfs(int index, char[] chars, List<String> list) {
if (index == chars.length) {
list.add(String.valueOf(chars));
return;
}
for (int i = index; i < chars.length; i++) {
if (isRepeat(chars, index, i)) continue;
swap(chars, index, i);
dfs(index + 1, chars, list);
swap(chars, index, i);
}
}
private boolean isRepeat(char[] chars, int index, int i) {
for (int j = index; j < i; j++) {
if (chars[j] == chars[i]) return true;
}
return false;
}
private void swap(char[] chars, int index, int i) {
char temp = chars[index];
chars[index] = chars[i];
chars[i] = temp;
}
}