LeetCode 热题 Hot 100
 
 
class Solution {
    public void sortColors(int[] nums) {
        int lastZero = -1, firstTwo = nums.length;
        int curr = 0;
        while (curr < firstTwo) {
            
            if (nums[curr] == 0) {
                
                swap(nums, curr, lastZero + 1);
                lastZero++;
                   
                curr++;
            }
            
            else if (nums[curr] == 1) {
                curr++;
            } else {
                
                swap(nums, curr, firstTwo - 1);
                firstTwo--;
            }
        }
    }
    private void swap(int[] nums, int i, int j) {
        if (i == j) return;
        nums[i] = nums[i] ^ nums[j];
        nums[j] = nums[i] ^ nums[j];
        nums[i] = nums[i] ^ nums[j];
    }
}
 
 
class Solution {
    
    public String minWindow(String s, String t) {
        
        if (s == null || t == null || s.length() == 0 || t.length() == 0) return "";
        
        Map<Character, Integer> ht = new HashMap<>();
        
        Map<Character, Integer> hs = new HashMap<>();
        
        for (char c : t.toCharArray()) {
            ht.put(c, ht.getOrDefault(c, 0) + 1);
        }
        String answer = "";
        int minLen = Integer.MAX_VALUE;
        int n = s.length();
        
        int end = 0;
        
        int matched = 0;
        for (int start = 0; start < n; start++) {
            
            while (end < n && matched < ht.size()) {
                char ch = s.charAt(end);
                hs.put(ch, hs.getOrDefault(ch, 0) + 1);
                
                if (hs.get(ch).equals(ht.getOrDefault(ch, 0))) {
                    matched++;
                }
                
                end++;
            }
            
            if (matched == ht.size()) {
                if (end - start < minLen) {
                    minLen = end - start;
                    answer = s.substring(start, end);
                }
            }
            
            char ch = s.charAt(start);
            hs.put(ch, hs.get(ch) - 1);
            if (hs.get(ch).equals(ht.getOrDefault(ch, 0) - 1)) {
                matched--;
            }
        }
        return answer;
    }
}
 
 
class Solution {
  LinkedList<List<Integer>> pathList = new LinkedList<>();
    LinkedList<Integer> path = new LinkedList<>();
    public List<List<Integer>> subsets(int[] nums) {
        backtrack(nums, 0);
        return pathList;
    }
    private void backtrack(int[] nums, int start) {
        pathList.add(new LinkedList<>(path));
        while (start < nums.length) {
            path.add(nums[start]);
            backtrack(nums, start + 1);
            path.removeLast();
            start++;
        }
    }
}
 
 
    int m;
    int n;
    int w;
    char[] letters;
    char[][] board;
    boolean[][] visited;
    
    public boolean exist(char[][] board, String word) {
        this.m = board.length;
        this.n = board[0].length;
        this.w = word.length();
        this.letters = word.toCharArray();
        this.board = board;
        this.visited = new boolean[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                
                boolean res = backtrack(i, j, 0);
                if (res) return true;
            }
        }
        return false;
    }
    private boolean backtrack(int i, int j, int wordIndex) {
        if (wordIndex >= w) return true;
        if (i < 0 || j < 0 || i >= m || j >= n || letters[wordIndex] != board[i][j] || visited[i][j]) return false;
        
        visited[i][j] = true;
        boolean res = backtrack(i + 1, j, wordIndex + 1) || backtrack(i, j + 1, wordIndex + 1)
                ||
                backtrack(i - 1, j, wordIndex + 1) || backtrack(i, j - 1, wordIndex + 1);
        visited[i][j] = false;
        return res;
    }
 
 
 
 
 
 
 
class Solution {
  
    public boolean isSymmetric(TreeNode root) {
        if (root == null) return true;
        
        return check(root.left, root.right);
    }
    private boolean check(TreeNode left, TreeNode right) {
        if (left == null && right == null) return true;
        if (left == null || right == null) return false;
        
        if (left.val != right.val) return false;
        
        return check(left.right, right.left) && check(left.left, right.right);
    }
}
 
 
class Solution {
  
    public int numTrees(int n) {
        int[] dp = new int[n + 1];
        dp[0] = 1;
        for (int i = 1; i <= n; i++) {
            for (int j = 0; j <= i - 1; j++) {
                dp[i] += dp[j] * dp[i - 1 - j ];
            }
        }
        return dp[n];
    }
}