CS-Notes/notes/剑指 offer 题解.md
2018-02-22 14:47:22 +08:00

46 KiB
Raw Blame History

????? ??????????????

2. ??? Singleton

???????

????????У???о???????????????????????????????????????????????????????????о??????????????????????????????????????????????????????????????? if(uniqueInstance == null) ??????飬????????????? uniqueInstance ??о????????

public class Singleton {
    private static Singleton uniqueInstance;
    private Singleton() {
    }
    public static Singleton getUniqueInstance() {
        if (uniqueInstance == null) {
            uniqueInstance = new Singleton();
        }
        return uniqueInstance;
    }
}

???????????????????

?????? getUniqueInstance() ????????????????÷????????????????????????????? uniqueInstance ???????ж?????????????????????????????????????????????????????????????????

public static synchronized Singleton getUniqueInstance() {
    if (uniqueInstance == null) {
        uniqueInstance = new Singleton();
    }
    return uniqueInstance;
}

????????????????????

????????????????????????????

private static Singleton uniqueInstance = new Singleton();

????????????????????

????????????????????????? getUniqueInstance() ???????м?????????????????? uniqueInstance = new Singleton(); ?????????????ɡ??????????????????ж? uniqueInstance ???????????????????????????????????

public class Singleton {
    private volatile static Singleton uniqueInstance;
    private Singleton() {
    }
    public static synchronized Singleton getUniqueInstance() {
        if (uniqueInstance == null) {
            synchronized (Singleton.class) {
                if (uniqueInstance == null) {
                    uniqueInstance = new Singleton();
                }
            }
        }
        return uniqueInstance;
    }
}

3. ???????????????

???????

?????????? n ???????????????????? 0 ?? n-1 ???Χ??? ???????Щ????????????????????м??????????????????????????????????Ρ????????????????????????????? ???磬?????????? 7 ?????? {2, 3, 1, 0, 2, 5, 3}???????????????????????????? 2??

?????·

????????????? [0, n-1] ??Χ?????????????? i ????????? i ??λ?????

public boolean duplicate(int numbers[], int length, int[] duplication) {
    for (int i = 0; i < length; i++) {
        while (numbers[i] != i && numbers[i] != numbers[numbers[i]]) {
            swap(numbers, i, numbers[i]);
        }
        if (numbers[i] != i && numbers[i] == numbers[numbers[i]]) {
            duplication[0] = numbers[i];
            return true;
        }
    }
    return false;
}

private void swap(int[] numbers, int i, int j) {
    int t = numbers[i];
    numbers[i] = numbers[j];
    numbers[j] = t;
}

4. ????????е????

???????

?????????????У????ж?????????????????????????ж????????????????????????????????????????????????????????????????????ж???????????и???????

public boolean Find(int target, int [][] array) {
    if (array == null || array.length == 0 || array[0].length == 0) return false;
    int m = array.length, n = array[0].length;
    int row = 0, col = n - 1;
    while (row < m && col >= 0) {
        if (target == array[row][col]) return true;
        else if (target < array[row][col]) col--;
        else row++;
    }
    return false;
}

5. ?滻???

???????

?????????????????????????е????滻?ɡ?%20???????磬???????? We Are Happy. ????滻?????????? We%20Are%20Happy??

??????

?? O(1) ???临????????

public String replaceSpace(StringBuffer str) {
    int n = str.length();
    for (int i = 0; i < n; i++) {
        if (str.charAt(i) == ' ') str.append("  "); // β?????????
    }

    int idxOfOriginal = n - 1;
    int idxOfNew = str.length() - 1;
    while (idxOfOriginal >= 0 && idxOfNew > idxOfOriginal) {
        if (str.charAt(idxOfOriginal) == ' ') {
            str.setCharAt(idxOfNew--, '0');
            str.setCharAt(idxOfNew--, '2');
            str.setCharAt(idxOfNew--, '%');
        } else {
            str.setCharAt(idxOfNew--, str.charAt(idxOfOriginal));
        }
        idxOfOriginal--;
    }
    return str.toString();
}

6. ??β??????????

????????????? Collections.reverse().

public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
    ArrayList<Integer> ret = new ArrayList<>();
    while (listNode != null) {
        ret.add(listNode.val);
        listNode = listNode.next;
    }
    Collections.reverse(ret);
    return ret;
}

???

public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
    ArrayList<Integer> ret = new ArrayList<>();
    if(listNode != null) {
        ret.addAll(printListFromTailToHead(listNode.next));
        ret.add(listNode.val);
    }
    return ret;
}

???????????????????????????????????????巨???????????

public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
    ListNode head = new ListNode(-1); // ????
    ListNode cur = listNode;
    while (cur != null) {
        ListNode next = cur.next;
        cur.next = head.next;
        head.next = cur;
        cur = next;
    }
    ArrayList<Integer> ret = new ArrayList<>();
    head = head.next;
    while (head != null) {
        ret.add(head.val);
        head = head.next;
    }
    return ret;
}

7. ?????????

???????

??????????????????????????????????????????????

public TreeNode reConstructBinaryTree(int[] pre, int[] in) {
    return reConstructBinaryTree(pre, 0, pre.length - 1, in, 0, in.length - 1);
}
private TreeNode reConstructBinaryTree(int[] pre, int preL, int preR, int[] in, int inL, int inR) {
    if(preL > preR || inL > inR) return null;
    TreeNode root = new TreeNode(pre[preL]);
    if (preL != preR) {
        int idx = inL;
        while (idx <= inR && in[idx] != root.val) idx++;
        int leftTreeLen = idx - inL;
        root.left = reConstructBinaryTree(pre, preL + 1, preL + leftTreeLen, in, inL, inL + leftTreeLen - 1);
        root.right = reConstructBinaryTree(pre, preL + leftTreeLen + 1, preR, in, inL + leftTreeLen + 1, inR);
    }
    return root;
}

8. ????????????????

???????

??????????????????е???????????????????????????????????????????е????????????????????????????????

public TreeLinkNode GetNext(TreeLinkNode pNode) {
    if (pNode == null) return null;
    if (pNode.right != null) {
        pNode = pNode.right;
        while (pNode.left != null) pNode = pNode.left;
        return pNode;
    } else {
        TreeLinkNode parent = pNode.next;
        while (parent != null) {
            if (parent.left == pNode) return parent;
            pNode = pNode.next;
            parent = pNode.next;
        }
    }
    return null;
}

9. ?????????????

Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();

public void push(int node) {
    stack1.push(node);
}

public int pop() {
    if (stack2.isEmpty()) {
        while (!stack1.isEmpty()) {
            stack2.push(stack1.pop());
        }
    }
    return stack2.pop();
}

10.1 ??????????

private int[] fib = new int[40];

public Solution() {
    fib[1] = 1;
    fib[2] = 2;
    for (int i = 2; i < fib.length; i++) {
        fib[i] = fib[i - 1] + fib[i - 2];
    }
}

public int Fibonacci(int n) {
    return fib[n];
}

10.2 ?????

public int JumpFloor(int target) {
    if (target == 1) return 1;
    int[] dp = new int[target];
    dp[0] = 1;
    dp[1] = 2;
    for (int i = 2; i < dp.length; i++) {
        dp[i] = dp[i - 1] + dp[i - 2];
    }
    return dp[target - 1];
}

10.3 ????????

public int JumpFloorII(int target) {
    int[] dp = new int[target];
    Arrays.fill(dp, 1);
    for (int i = 1; i < target; i++) {
        for (int j = 0; j < i; j++) {
            dp[i] += dp[j];
        }
    }
    return dp[target - 1];
}

10.4 ???θ???

???????

????????? 2*1 ??С???κ?????????????????????Ρ??????? n ?? 2*1 ??С????????????????? 2*n ?????Σ?????ж??????????

public int RectCover(int target) {
    if (target <= 2) return target;
    return RectCover(target - 1) + RectCover(target - 2);
}

11. ??????????С????

???????

?????????????????????????????β??????????????????? ??????????????????????????????????????????С???? ???????? {3, 4, 5, 1, 2} ? {1, 2, 3, 4, 5} ???????????????????С?? 1?? NOTE???????????????????? 0?????????С? 0?????? 0??

public int minNumberInRotateArray(int[] array) {
    if (array.length == 0) return 0;
    for (int i = 0; i < array.length - 1; i++) {
        if (array[i] > array[i + 1]) return array[i + 1];
    }
    return 0;
}

12. ?????е?·??

???????

???????????????????ж????????????????????????????????????????·????·???????????е??????????????????????????????????????????????????????????????????·????????????е?????????????·???????????????? ???? a b c e s f c s a d e e ?????а??????????? "bcced" ??·????????????в????? "abcb" ·????????????????????? b ?????????е????е???????????·????????ν????????

private int[][] next = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};

public boolean hasPath(char[] matrix, int rows, int cols, char[] str) {
    if (rows == 0 || cols == 0) return false;
    char[][] m = new char[rows][cols];
    for (int i = 0, idx = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            m[i][j] = matrix[idx++];
        }
    }

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            if (backtracking(m, rows, cols, str, new boolean[rows][cols], 0, i, j)) return true;
        }
    }
    return false;
}

private boolean backtracking(char[][] m, int rows, int cols, char[] str, boolean[][] used, int path, int r, int c) {
    if (path == str.length) return true;
    if (r < 0 || r >= rows || c < 0 || c >= cols) return false;
    if (m[r][c] != str[path]) return false;
    if (used[r][c]) return false;
    used[r][c] = true;
    for (int i = 0; i < next.length; i++) {
        if (backtracking(m, rows, cols, str, used, path + 1, r + next[i][0], c + next[i][1])) return true;
    }
    used[r][c] = false;
    return false;
}

13. ????????????Χ

???????

????????? m ?к? n ?е?????????????????? 0, 0 ?????????????????????????????????????????????????????????????????????λ?????? k ?????? ???磬?? k ? 18 ??????????????????35, 37??????? 3+5+3+7 = 18??????????????????35, 38??????? 3+5+3+8 = 19???????????????????????????

private int cnt = 0;
private int[][] next = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
private int[][] digitSum;

public int movingCount(int threshold, int rows, int cols) {
    initDigitSum(rows, cols);
    dfs(new boolean[rows][cols], threshold, rows, cols, 0, 0);
    return cnt;
}

private void dfs(boolean[][] visited, int threshold, int rows, int cols, int r, int c) {
    if (r < 0 || r >= rows || c < 0 || c >= cols) return;
    if (visited[r][c]) return;
    visited[r][c] = true;
    if (this.digitSum[r][c] > threshold) return;
    this.cnt++;
    for (int i = 0; i < this.next.length; i++) {
        dfs(visited, threshold, rows, cols, r + next[i][0], c + next[i][1]);
    }
}

private void initDigitSum(int rows, int cols) {
    int[] digitSumOne = new int[Math.max(rows, cols)];
    for (int i = 0; i < digitSumOne.length; i++) {
        int n = i;
        while (n > 0) {
            digitSumOne[i] += n % 10;
            n /= 10;
        }
    }
    this.digitSum = new int[rows][cols];
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            this.digitSum[i][j] = digitSumOne[i] + digitSumOne[j];
        }
    }
}

14. ??????

???????

?????????????Σ??????????ε?????????

?????·

???????ü?????? 3 ?????????????????г???? 1 ???????????????????????????кó???? 3 ?????????ó????????? 1 ?????????????????????г????γ???? 2 ???????

int maxProductAfterCuttin(int length) {
    if (length < 2) return 0;
    if (length == 2) return 1;
    if (length == 3) return 2;
    int timesOf3 = length / 3;
    if (length - timesOf3 * 3 == 1) timesOf3--;
    int timesOf2 = (length - timesOf3 * 3) / 2;
    return (int) (Math.pow(3, timesOf3)) * (int) (Math.pow(2, timesOf2));
}

15. ???????? 1 ?????

public int NumberOf1(int n) {
    return Integer.bitCount(n);
}

n&(n-1) ??λ????????? n ??λ??????????????λ???????????????? 10110100????? 1 ??? 10110011???????????????? 10110000??

public int NumberOf1(int n) {
    int cnt = 0;
    while (n != 0) {
        cnt++;
        n &= (n - 1);
    }
    return cnt;
}

?????? ???????????

16. ??????????η?

public double Power(double base, int exponent) {
    if (exponent == 0) return 1;
    if (exponent == 1) return base;
    boolean isNegative = false;
    if (exponent < 0) {
        exponent = -exponent;
        isNegative = true;
    }
    double pow = Power(base * base, exponent / 2);
    if (exponent % 2 != 0) pow = pow * base;
    return isNegative ? 1 / pow : pow;
}

18. ????????????????

public ListNode deleteDuplication(ListNode pHead) {
    if (pHead == null) return null;
    if (pHead.next == null) return pHead;
    if (pHead.val == pHead.next.val) {
        ListNode next = pHead.next;
        while (next != null && pHead.val == next.val) {
            next = next.next;
        }
        return deleteDuplication(next);
    } else {
        pHead.next = deleteDuplication(pHead.next);
        return pHead;
    }
}

19. ??????????

???????

?????????????????????? '.' ?? '*' ??????????????е???? '.' ????????????????? '*' ???????????????????????Σ????? 0 ?Σ??? ??????У???????????????????????????????????磬????? "aaa" ???? "a.a" ?? "ab*ac*a" ????????? "aa.a" ?? "ab*a" ???????

public boolean match(char[] str, char[] pattern) {
    int n = str.length, m = pattern.length;
    boolean[][] dp = new boolean[n + 1][m + 1];
    dp[0][0] = true;
    for (int i = 1; i <= m; i++) {
        if (pattern[i - 1] == '*') dp[0][i] = dp[0][i - 2];
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (str[i - 1] == pattern[j - 1] || pattern[j - 1] == '.') dp[i][j] = dp[i - 1][j - 1];
            else if (pattern[j - 1] == '*') {
                if (pattern[j - 2] != str[i - 1] && pattern[j - 2] != '.') dp[i][j] = dp[i][j - 2];
                else dp[i][j] = dp[i][j - 1] || dp[i][j - 2] || dp[i - 1][j];
            }
        }
    }
    return dp[n][m];
}

20. ?????????????

???????

?????????????????ж??????????????????????????С?????????磬????? "+100","5e2","-123","3.1416" ?? "-1E-16" ?????????? ???? "12e","1a3.14","1.2.3","+-5" ?? "12e+4.3" ???????

public boolean isNumeric(char[] str) {
    String string = String.valueOf(str);
    return string.matches("[\\+-]?[0-9]*(\\.[0-9]*)?([eE][\\+-]?[0-9]+)?");
}

21. ????????????????λ????????

??临??? : O(n2) ??临??? : O(1)

public void reOrderArray(int[] array) {
    int n = array.length;
    for (int i = 0; i < n; i++) {
        if (array[i] % 2 == 0) {
            int nextOddIdx = i + 1;
            while (nextOddIdx < n && array[nextOddIdx] % 2 == 0) nextOddIdx++;
            if (nextOddIdx == n) break;
            int nextOddVal = array[nextOddIdx];
            for (int j = nextOddIdx; j > i; j--) {
                array[j] = array[j - 1];
            }
            array[i] = nextOddVal;
        }
    }
}

??临??? : O(n) ??临??? : O(n)

public void reOrderArray(int[] array) {
    int oddCnt = 0;
    for (int num : array) if (num % 2 == 1) oddCnt++;
    int[] copy = array.clone();
    int i = 0, j = oddCnt;
    for (int num : copy) {
        if (num % 2 == 1) array[i++] = num;
        else array[j++] = num;
    }
}

22. ?????е????? k ?????

public ListNode FindKthToTail(ListNode head, int k) {
    if (head == null) return null;
    ListNode fast, slow;
    fast = slow = head;
    while (fast != null && k-- > 0) fast = fast.next;
    if (k > 0) return null;
    while (fast != null) {
        fast = fast.next;
        slow = slow.next;
    }
    return slow;
}

23. ?????л????????

public ListNode EntryNodeOfLoop(ListNode pHead) {
    if (pHead == null) return null;
    ListNode slow = pHead, fast = pHead;
    while (fast != null && fast.next != null) {
        fast = fast.next.next;
        slow = slow.next;
        if (slow == fast) {
            fast = pHead;
            while (slow != fast) {
                slow = slow.next;
                fast = fast.next;
            }
            return slow;
        }
    }
    return null;
}

24. ???????

public ListNode ReverseList(ListNode head) {
    ListNode newList = new ListNode(-1);
    while (head != null) {
        ListNode next = head.next;
        head.next = newList.next;
        newList.next = head;
        head = next;
    }
    return newList.next;
}

25. ????????????????

public ListNode Merge(ListNode list1, ListNode list2) {
    ListNode head = new ListNode(-1);
    ListNode cur = head;
    while (list1 != null && list2 != null) {
        if (list1.val < list2.val) {
            cur.next = list1;
            list1 = list1.next;
        } else {
            cur.next = list2;
            list2 = list2.next;
        }
        cur = cur.next;
    }
    if (list1 != null) cur.next = list1;
    if (list2 != null) cur.next = list2;
    return head.next;
}

26. ???????

public boolean HasSubtree(TreeNode root1, TreeNode root2) {
    if (root1 == null || root2 == null) return false;
    return isSubtree(root1, root2) || HasSubtree(root1.left, root2) || HasSubtree(root1.right, root2);
}

private boolean isSubtree(TreeNode root1, TreeNode root2) {
    if (root1 == null && root2 == null) return true;
    if (root1 == null) return false;
    if (root2 == null) return true;
    if (root1.val != root2.val) return false;
    return isSubtree(root1.left, root2.left) && isSubtree(root1.right, root2.right);
}

?????? ???????????·

27. ???????????

public void Mirror(TreeNode root) {
    if (root == null) return;
    TreeNode t = root.left;
    root.left = root.right;
    root.right = t;
    Mirror(root.left);
    Mirror(root.right);
}

28.1 ?????????

boolean isSymmetrical(TreeNode pRoot) {
    if (pRoot == null) return true;
    return isSymmetrical(pRoot.left, pRoot.right);
}

boolean isSymmetrical(TreeNode t1, TreeNode t2) {
    if (t1 == null && t2 == null) return true;
    if (t1 == null || t2 == null) return false;
    if (t1.val != t2.val) return false;
    return isSymmetrical(t1.left, t2.right) && isSymmetrical(t1.right, t2.left);
}

28.2 ????????

private boolean isBalanced = true;

public boolean IsBalanced_Solution(TreeNode root) {
    height(root);
    return isBalanced;
}

private int height(TreeNode root) {
    if (root == null) return 0;
    int left = height(root.left);
    int right = height(root.right);
    if (Math.abs(left - right) > 1) isBalanced = false;
    return 1 + Math.max(left, right);
}

29. ??????????

public ArrayList<Integer> printMatrix(int[][] matrix) {
    ArrayList<Integer> ret = new ArrayList<>();
    int r1 = 0, r2 = matrix.length - 1, c1 = 0, c2 = matrix[0].length - 1;
    while (r1 <= r2 && c1 <= c2) {
        for (int i = c1; i <= c2; i++) ret.add(matrix[r1][i]);
        for (int i = r1 + 1; i <= r2; i++) ret.add(matrix[i][c2]);
        if (r1 != r2) for (int i = c2 - 1; i >= c1; i--) ret.add(matrix[r2][i]);
        if (c1 != c2) for (int i = r2 - 1; i > r1; i--) ret.add(matrix[i][c1]);
        r1++; r2--; c1++; c2--;
    }
    return ret;
}

30. ???? min ???????

private Stack<Integer> stack = new Stack<>();
private Stack<Integer> minStack = new Stack<>();
private int min = Integer.MAX_VALUE;

public void push(int node) {
    stack.push(node);
    if (min > node) min = node;
    minStack.push(min);
}

public void pop() {
    stack.pop();
    minStack.pop();
    min = minStack.peek();
}

public int top() {
    return stack.peek();
}

public int min() {
    return minStack.peek();
}

31. ??????????????

public boolean IsPopOrder(int[] pushA, int[] popA) {
    int n = pushA.length;
    Stack<Integer> stack = new Stack<>();
    for (int i = 0, j = 0; i < n; i++) {
        stack.push(pushA[i]);
        while (j < n && stack.peek() == popA[j]) {
            stack.pop();
            j++;
        }
    }
    return stack.isEmpty();
}

32.1 ????????????????

public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
    Queue<TreeNode> queue = new LinkedList<>();
    ArrayList<Integer> ret = new ArrayList<>();
    if (root == null) return ret;
    queue.add(root);
    while (!queue.isEmpty()) {
        int cnt = queue.size();
        for (int i = 0; i < cnt; i++) {
            TreeNode t = queue.poll();
            if (t.left != null) queue.add(t.left);
            if (t.right != null) queue.add(t.right);
            ret.add(t.val);
        }
    }
    return ret;
}

32.3 ???????????????

ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) {
    ArrayList<ArrayList<Integer>> ret = new ArrayList<>();
    if (pRoot == null) return ret;
    Queue<TreeNode> queue = new LinkedList<>();
    queue.add(pRoot);
    while (!queue.isEmpty()) {
        int cnt = queue.size();
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < cnt; i++) {
            TreeNode node = queue.poll();
            list.add(node.val);
            if (node.left != null) queue.add(node.left);
            if (node.right != null) queue.add(node.right);
        }
        ret.add(list);
    }
    return ret;
}

32.3 ??????????????????

public ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) {
    ArrayList<ArrayList<Integer>> ret = new ArrayList<>();
    if (pRoot == null) return ret;
    Queue<TreeNode> queue = new LinkedList<>();
    queue.add(pRoot);
    boolean reverse = false;
    while (!queue.isEmpty()) {
        int cnt = queue.size();
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < cnt; i++) {
            TreeNode node = queue.poll();
            list.add(node.val);
            if (node.left != null) queue.add(node.left);
            if (node.right != null) queue.add(node.right);
        }
        if (reverse) {
            Collections.reverse(list);
            reverse = false;
        } else {
            reverse = true;
        }
        ret.add(list);
    }
    return ret;
}

33. ??????????????????????

public boolean VerifySquenceOfBST(int[] sequence) {
    if (sequence.length == 0) return false;
    return verify(sequence, 0, sequence.length - 1);
}

private boolean verify(int[] sequence, int start, int end) {
    if (end - start <= 1) return true;
    int rootVal = sequence[end];
    int cutIdx = start;
    while (cutIdx < end) {
        if (sequence[cutIdx] > rootVal) break;
        cutIdx++;
    }
    for (int i = cutIdx + 1; i < end; i++) {
        if (sequence[i] < rootVal) return false;
    }
    return verify(sequence, start, cutIdx - 1) && verify(sequence, cutIdx, end - 1);
}

34. ???????к???????·??

private ArrayList<ArrayList<Integer>> ret = new ArrayList<>();

public ArrayList<ArrayList<Integer>> FindPath(TreeNode root, int target) {
    dfs(root, target, 0, new ArrayList<>());
    return ret;
}

private void dfs(TreeNode node, int target, int curSum, ArrayList<Integer> path) {
    if (node == null) return;
    curSum += node.val;
    path.add(node.val);
    if (curSum == target && node.left == null && node.right == null) {
        ret.add(new ArrayList(path));
    } else {
        dfs(node.left, target, curSum, path);
        dfs(node.right, target, curSum, path);
    }
    path.remove(path.size() - 1);
}

35. ????????????

???????

????????????????????????н??????????????????????????????????????????????????????????????????????????? head??????????????????????????е????????????????????????????

???????????????????????????

??????????????? random ??????и????

????????????

public RandomListNode Clone(RandomListNode pHead) {
    if (pHead == null) return null;
    // ????????
    RandomListNode cur = pHead;
    while (cur != null) {
        RandomListNode node = new RandomListNode(cur.label);
        node.next = cur.next;
        cur.next = node;
        cur = node.next;
    }
    // ???? random ????
    cur = pHead;
    while (cur != null) {
        RandomListNode clone = cur.next;
        if (cur.random != null) {
            clone.random = cur.random.next;
        }
        cur = clone.next;
    }
    // ???
    RandomListNode pCloneHead = pHead.next;
    cur = pHead;
    while (cur.next != null) {
        RandomListNode t = cur.next;
        cur.next = t.next;
        cur = t;
    }
    return pCloneHead;
}

36. ???????????????????

???????

????????????????????????????????????????????????????????????κ???????????????н?????????

private TreeNode pre = null;
public TreeNode Convert(TreeNode pRootOfTree) {
    if(pRootOfTree == null) return null;
    inOrder(pRootOfTree);
    while(pRootOfTree.left != null) pRootOfTree = pRootOfTree.left;
    return pRootOfTree;
}

private void inOrder(TreeNode node) {
    if(node == null) return;
    inOrder(node.left);
    node.left = pre;
    if(pre != null) pre.right = node;
    pre = node;
    inOrder(node.right);
}

37. ???л???????

private String serizeString = "";

String Serialize(TreeNode root) {
    if (root == null) return "#";
    return root.val + " " + Serialize(root.left) + " "
        + Serialize(root.right);
}

TreeNode Deserialize(String str) {
    this.serizeString = str;
    return Deserialize();
}

private TreeNode Deserialize() {
    if (this.serizeString.length() == 0) return null;
    int idx = this.serizeString.indexOf(" ");
    if (idx == -1) return null;
    String sub = this.serizeString.substring(0, idx);
    this.serizeString = this.serizeString.substring(idx + 1);
    if (sub.equals("#")) {
        return null;
    }
    int val = Integer.valueOf(sub);
    TreeNode t = new TreeNode(val);
    t.left = Deserialize();
    t.right = Deserialize();
    return t;
}

38. ???????????

???????

???????????? , ????????????????????????????????С?????????????? abc, ??????????? a, b, c ???????г?????????????? abc, acb, bac, bca, cab ?? cba??

private ArrayList<String> ret = new ArrayList<>();

public ArrayList<String> Permutation(String str) {
    if (str.length() == 0) return new ArrayList<>();
    char[] chars = str.toCharArray();
    Arrays.sort(chars);
    backtracking(chars, new boolean[chars.length], "");
    return ret;
}

private void backtracking(char[] chars, boolean[] used, String s) {
    if (s.length() == chars.length) {
        ret.add(s);
        return;
    }
    for (int i = 0; i < chars.length; i++) {
        if (used[i]) continue;
        if (i != 0 && chars[i] == chars[i - 1] && !used[i - 1]) continue; // ????????
        used[i] = true;
        backtracking(chars, used, s + chars[i]);
        used[i] = false;
    }
}

?????? ?????????Ч??

39. ?????г??????????????????

public int MoreThanHalfNum_Solution(int[] array) {
    int cnt = 1, num = array[0];
    for (int i = 1; i < array.length; i++) {
        if (array[i] == num) cnt++;
        else cnt--;
        if (cnt == 0) {
            num = array[i];
            cnt = 1;
        }
    }
    cnt = 0;
    for (int i = 0; i < array.length; i++) {
        if (num == array[i]) cnt++;
    }
    return cnt > array.length / 2 ? num : 0;
}

40. ??С?? K ????

??????С? k ??С?????

??临????O(nlgk) ??临????O(k)

public ArrayList<Integer> GetLeastNumbers_Solution(int[] input, int k) {
    if (k > input.length || k <= 0) return new ArrayList<>();
    PriorityQueue<Integer> pq = new PriorityQueue<>((o1, o2) -> o2 - o1);
    for (int num : input) {
        pq.add(num);
        if (pq.size() > k) {
            pq.poll();
        }
    }
    ArrayList<Integer> ret = new ArrayList<>(pq);
    return ret;
}

??????????

??临????O(n) ??临????O(1)

public ArrayList<Integer> GetLeastNumbers_Solution(int[] input, int k) {
    if (k > input.length || k <= 0) return new ArrayList<>();
    int kthSmallest = findKthSmallest(input, k - 1);
    ArrayList<Integer> ret = new ArrayList<>();
    for (int num : input) {
        if(num <= kthSmallest && ret.size() < k) ret.add(num);
    }
    return ret;
}

public int findKthSmallest(int[] nums, int k) {
    int lo = 0;
    int hi = nums.length - 1;
    while (lo < hi) {
        int j = partition(nums, lo, hi);
        if (j < k) {
            lo = j + 1;
        } else if (j > k) {
            hi = j - 1;
        } else {
            break;
        }
    }
    return nums[k];
}

private int partition(int[] a, int lo, int hi) {
    int i = lo;
    int j = hi + 1;
    while (true) {
        while (i < hi && less(a[++i], a[lo])) ;
        while (j > lo && less(a[lo], a[--j])) ;
        if (i >= j) {
            break;
        }
        exch(a, i, j);
    }
    exch(a, lo, j);
    return j;
}

private void exch(int[] a, int i, int j) {
    final int tmp = a[i];
    a[i] = a[j];
    a[j] = tmp;
}

private boolean less(int v, int w) {
    return v < w;
}

41.1 ???????е???λ??

???????

??ε????????????е???λ????????????????ж???????????????????λ????????????????????λ???м???????????????????ж??????????????????λ?????????????????????м???????????????

private PriorityQueue<Integer> maxHeap = new PriorityQueue<>((o1, o2) -> o2-o1); // ?????????
private PriorityQueue<Integer> minHeep = new PriorityQueue<>(); // ????????????????????????????????
private int cnt = 0;

public void Insert(Integer num) {
    // ??????????????????????
    if(cnt % 2 == 0) { 
        // ????????????????С???????????????????????????????е?????С????С???е????
        maxHeap.add(num);
        minHeep.add(maxHeap.poll());
    } else {
        minHeep.add(num);
        maxHeap.add(minHeep.poll());
    }
    cnt++;
}

public Double GetMedian() {
    if(cnt % 2 == 0) {
        return (maxHeap.peek() + minHeep.peek()) / 2.0;
    } else {
        return (double) minHeep.peek();
    }
}

14.2 ??????е??????????????

???????

?????????????????????????е???????????ε?????????磬???????????????????????? "go" ???????????????ε?????? "g"?????????????ж?????????????google" ???????????????ε?????? "l"??

//Insert one char from stringstream
private int[] cnts = new int[256];
private Queue<Character> queue = new LinkedList<>();

public void Insert(char ch) {
    cnts[ch]++;
    queue.add(ch);
    while (!queue.isEmpty() && cnts[queue.peek()] > 1) {
        queue.poll();
    }
}

//return the first appearence once char in current stringstream
public char FirstAppearingOnce() {
    if (queue.isEmpty()) return '#';
    return queue.peek();
}

42. ???????????????

public int FindGreatestSumOfSubArray(int[] array) {
    if(array.length == 0) return 0;
    int ret = Integer.MIN_VALUE;
    int sum = 0;
    for(int num : array) {
        if(sum <= 0) sum = num;
        else sum += num;
        ret = Math.max(ret, sum);
    }
    return ret;
}

43. ?? 1 ?? n ?????? 1 ????????

????ο???Leetcode : 233. Number of Digit One

public int NumberOf1Between1AndN_Solution(int n) {
    int cnt = 0;
    for (int m = 1; m <= n; m *= 10) {
        int a = n / m, b = n % m;
        cnt += (a + 8) / 10 * m + (a % 10 == 1 ? b + 1 : 0);
    }
    return cnt;
}

45. ???????????С????

???????

????????????????飬????????????????????????????????????????????????????????С??????????????????? {3??32??321}??????????????????????????С????? 321323??

public String PrintMinNumber(int[] numbers) {
    int n = numbers.length;
    String[] nums = new String[n];
    for (int i = 0; i < n; i++) nums[i] = numbers[i] + "";
    Arrays.sort(nums, (s1, s2) -> (s1 + s2).compareTo(s2 + s1));
    String ret = "";
    for (String str : nums) ret += str;
    return ret;
}

49. ????

???????

??????????? 2??3 ?? 5 ??????????????Ugly Number???????? 6??8 ??????????? 14 ?????????????????? 7?? ?????????? 1 ???????????????????С?????????? N ????????

public int GetUglyNumber_Solution(int index) {
    if (index <= 6) return index;
    int i2 = 0, i3 = 0, i5 = 0;
    int cnt = 1;
    int[] dp = new int[index];
    dp[0] = 1;
    while (cnt < index) {
        int n2 = dp[i2] * 2, n3 = dp[i3] * 3, n5 = dp[i5] * 5;
        int tmp = Math.min(n2, Math.min(n3, n5));
        dp[cnt++] = tmp;
        if (tmp == n2) i2++;
        if (tmp == n3) i3++;
        if (tmp == n5) i5++;
    }
    return dp[index - 1];
}

50. ????????????ε????λ??

public int FirstNotRepeatingChar(String str) {
    int[] cnts = new int[256];
    for (int i = 0; i < str.length(); i++) cnts[str.charAt(i)]++;
    for (int i = 0; i < str.length(); i++) if (cnts[str.charAt(i)] == 1) return i;
    return -1;
}

51. ?????е??????

private long cnt = 0;

public int InversePairs(int[] array) {
    mergeSortUp2Down(array, 0, array.length - 1);
    return (int) (cnt % 1000000007);
}

private void mergeSortUp2Down(int[] a, int start, int end) {
    if (end - start < 1) return;
    int mid = start + (end - start) / 2;
    mergeSortUp2Down(a, start, mid);
    mergeSortUp2Down(a, mid + 1, end);
    merge(a, start, mid, end);
}

private void merge(int[] a, int start, int mid, int end) {
    int[] tmp = new int[end - start + 1];
    int i = start, j = mid + 1, k = 0;
    while (i <= mid || j <= end) {
        if (i > mid) tmp[k] = a[j++];
        else if (j > end) tmp[k] = a[i++];
        else if (a[i] < a[j]) tmp[k] = a[i++];
        else {
            tmp[k] = a[j++];
            this.cnt += mid - i + 1; // a[i] > a[j] ????? a[i...mid] ?????? a[j]
        }
        k++;
    }

    for (k = 0; k < tmp.length; k++) {
        a[start + k] = tmp[k];
    }
}

52. ????????????????????

public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
    ListNode l1 = pHead1, l2 = pHead2;
    while (l1 != l2) {
        if (l1 == null) l1 = pHead2;
        else l1 = l1.next;
        if (l2 == null) l2 = pHead1;
        else l2 = l2.next;
    }
    return l1;
}

?????? ?????е????????

53 ???????????????г???????

public int GetNumberOfK(int[] array, int k) {
    int l = 0, h = array.length - 1;
    while (l <= h) {
        int m = l + (h - l) / 2;
        if (array[m] >= k) h = m - 1;
        else l = m + 1;
    }
    int cnt = 0;
    while (l < array.length && array[l++] == k) cnt++;
    return cnt;
}

54. ????????????? k ?????

TreeNode ret;
int cnt = 0;

TreeNode KthNode(TreeNode pRoot, int k) {
    inorder(pRoot, k);
    return ret;
}

private void inorder(TreeNode root, int k) {
    if (root == null) return;
    if (cnt > k) return;
    inorder(root.left, k);
    cnt++;
    if (cnt == k) ret = root;
    inorder(root.right, k);
}

55 ???????????

public int TreeDepth(TreeNode root) {
    if (root == null) return 0;
    return 1 + Math.max(TreeDepth(root.left), TreeDepth(root.right));
}

56. ?????????????ε?????

???????

???????????????????????????????????????????????Σ??????????????

?????·

???????????????λ??????????????λ????????

??????????????????????????????????????????????????

diff &= -diff ????? diff ?????? 0 ??λ??????????????????????????λ??????????????????λ?????????λ??????????????????????

public void FindNumsAppearOnce(int[] array, int num1[], int num2[]) {
    int diff = 0;
    for (int num : array) diff ^= num;
    // ????????λ
    diff &= -diff;
    for (int num : array) {
        if ((num & diff) == 0) num1[0] ^= num;
        else num2[0] ^= num;
    }
}

57.1 ??? S ??????????

???????

???????????????????????????? S?????????в?????????????????????????? S??????ж??????????? S?????????????????С???

public ArrayList<Integer> FindNumbersWithSum(int[] array, int sum) {
    int i = 0, j = array.length - 1;
    while (i < j) {
        int cur = array[i] + array[j];
        if (cur == sum) return new ArrayList<Integer>(Arrays.asList(array[i], array[j]));
        else if (cur < sum) i++;
        else j--;
    }
    return new ArrayList<Integer>();
}

57.2 ??? S ??????????????

???????

??? 100 ???????????? 18, 19, 20, 21, 22

public ArrayList<ArrayList<Integer>> FindContinuousSequence(int sum) {
    ArrayList<ArrayList<Integer>> ret = new ArrayList<>();
    int start = 1, end = 2;
    int mid = sum / 2;
    int curSum = 3;
    while (start <= mid && end < sum) {
        if (curSum > sum) {
            curSum -= start;
            start++;
        } else if (curSum < sum) {
            end++;
            curSum += end;
        } else {
            ArrayList<Integer> list = new ArrayList<>();
            for (int i = start; i <= end; i++) {
                list.add(i);
            }
            ret.add(list);
            curSum -= start;
            start++;
            end++;
            curSum += end;
        }
    }
    return ret;
}

58.1 ????????????

???????

????"I am a student."

?????"student. a am I"

public String ReverseSentence(String str) {
    if (str.length() == 0) return str;
    int n = str.length();
    char[] chars = str.toCharArray();
    int start = 0, end = 0;
    while (end <= n) {
        if (end == n || chars[end] == ' ') {
            reverse(chars, start, end - 1);
            start = end + 1;
        }
        end++;
    }
    reverse(chars, 0, n - 1);
    return new String(chars);
}

private void reverse(char[] c, int start, int end) {
    while (start < end) {
        char t = c[start];
        c[start] = c[end];
        c[end] = t;
        start++;
        end--;
    }
}

58.2 ??????????

???????

???????????????????? S???????????????? K λ???????????????磬??????? S=??abcXYZdef??, ????????????? 3 λ???????????XYZdefabc????

public String LeftRotateString(String str, int n) {
    if (str.length() == 0) return "";
    char[] c = str.toCharArray();
    reverse(c, 0, n - 1);
    reverse(c, n, c.length - 1);
    reverse(c, 0, c.length - 1);
    return new String(c);
}

private void reverse(char[] c, int i, int j) {
    while (i < j) {
        char t = c[i];
        c[i] = c[j];
        c[j] = t;
        i++;
        j--;
    }
}

59. ?????????????

???????

?????????????????????С????????л???????????????????????磬??????????? {2, 3, 4, 2, 6, 2, 5, 1} ????????????С 3???????????? 6 ???????????????????????? {4, 4, 6, 6, 6, 5}??

public ArrayList<Integer> maxInWindows(int[] num, int size) {
    ArrayList<Integer> ret = new ArrayList<>();
    if (size > num.length || size < 1) return ret;
    PriorityQueue<Integer> heap = new PriorityQueue<Integer>((o1, o2) -> o2 - o1);
    for (int i = 0; i < size; i++) heap.add(num[i]);
    ret.add(heap.peek());
    for (int i = 1; i + size - 1 < num.length; i++) {
        heap.remove(num[i - 1]);
        heap.add(num[i + size - 1]);
        ret.add(heap.peek());
    }
    return ret;
}

61. ????????

???????

??????????д?С????????????С? 0???ж?????????????

public boolean isContinuous(int[] numbers) {
    if (numbers.length < 5) return false;
    Arrays.sort(numbers);
    int cnt = 0;
    for (int num : numbers) if (num == 0) cnt++;
    for (int i = cnt; i < numbers.length - 1; i++) {
        if (numbers[i + 1] == numbers[i]) return false;
        int cut = numbers[i + 1] - numbers[i] - 1;
        if (cut > cnt) return false;
        cnt -= cut;
    }
    return true;
}

62. ?????????????

???????

??С??????Χ????????????? , ????????????? m, ????? 0 ??С??????????????κ??? m-1 ?????С????????г???? , ??????????????????????????? , ???????????? , ???????????С?????? , ???? 0...m-1 ???? .... ??????? .... ????????????С???? , ???????????

?????·

????

public int LastRemaining_Solution(int n, int m) {
    if (n == 0) return -1;
    if (n == 1) return 0;
    return (LastRemaining_Solution(n - 1, m) + m) % n;
}

63. ????????????

???????

???????????????????????????????????????????档

public int maxProfit(int[] prices) {
    int n = prices.length;
    if(n == 0) return 0;
    int soFarMin = prices[0];
    int max = 0;
    for(int i = 1; i < n; i++) {
        if(soFarMin > prices[i]) soFarMin = prices[i];
        else max = Math.max(max, prices[i] - soFarMin);
    }
    return max;
}

64. ?? 1+2+3+...+n

???????

?? 1+2+3+...+n?????????ó??????for??while??if??else??switch??case ????????????ж????A?B:C??

public int Sum_Solution(int n) {
    if(n == 0) return 0;
    return n + Sum_Solution(n - 1);
}

65. ???ü??????????

a ^ b ?????п????λ???????????????(a & b) << 1 ?????λ???????????????? (a & b) << 1 ?????????? 0???????????飬??λ?????? 0 ??????????????λ???? 0??????????

public int Add(int num1, int num2) {
    if(num2 == 0) return num1;
    return Add(num1 ^ num2, (num1 & num2) << 1);
}

66. ???????????

???????

??????????? A[0, 1,..., n-1], ??????????? B[0, 1,..., n-1], ???? B ?е???? B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1]????????ó?????

public int[] multiply(int[] A) {
    int n = A.length;
    int[][] dp = new int[n][n];
    for (int i = 0; i < n; i++) {
        dp[i][i] = A[i];
    }
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            dp[i][j] = dp[i][j - 1] * A[j];
        }
    }

    int[] B = new int[n];
    Arrays.fill(B, 1);
    for (int i = 0; i < n; i++) {
        if (i != 0) B[i] *= dp[0][i - 1];
        if (i != n - 1) B[i] *= dp[i + 1][n - 1];
    }
    return B;
}

?????? ???????????

67. ????????????????

public int StrToInt(String str) {
    if (str.length() == 0) return 0;
    char[] chars = str.toCharArray();
    boolean isNegative = chars[0] == '-';
    int ret = 0;
    for (int i = 0; i < chars.length; i++) {
        if (i == 0 && (chars[i] == '+' || chars[i] == '-')) continue;
        if (chars[i] < '0' || chars[i] > '9') return 0;
        ret = ret * 10 + (chars[i] - '0');
    }
    return isNegative ? -ret : ret;
}

68. ??????????????????????

????????????????????????????

public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
    if(root.val > p.val && root.val > q.val) return lowestCommonAncestor(root.left, p, q);
    if(root.val < p.val && root.val < q.val) return lowestCommonAncestor(root.right, p, q);
    return root;
}