LeetCode,无它,唯手熟尔(五)

大家好,我是方圆
无它,唯手熟尔


回溯法

10. 正则表达式匹配

class Solution {
    int ls, lp;
    public boolean isMatch(String s, String p) {
        ls = s.length();
        lp = p.length();
        return match(s, p, 0, 0);
    }
    private boolean match(String s, String p, int i, int j) {
        if(j == lp) return i == ls;
        if(j < lp - 1 && p.charAt(j + 1) == '*') {
            if(i < ls && (p.charAt(j) == '.' || s.charAt(i) == p.charAt(j)))
                return match(s, p, i + 1, j) || match(s, p, i, j + 2);
            return match(s, p, i, j + 2);
        }
        if(i < ls && (p.charAt(j) == '.' || s.charAt(i) == p.charAt(j)))
            return match(s, p, i + 1, j + 1);
        return false;
    }
}

22. 括号的生成

class Solution {
    private List<String> res = new ArrayList<>();

    public List<String> generateParenthesis(int n) {
        dfs(n, n, "");
        return res;
    }

    private void dfs(int left, int right, String curStr) {
        if(left == 0 && right == 0) res.add(curStr);

        if(left > 0) dfs(left - 1, right, curStr + "(");
        if(left < right) dfs(left, right - 1, curStr + ")");
    }
}

40. 组合总数 II

class Solution {
    private List<List<Integer>> res = new ArrayList<>();
    private LinkedList<Integer> list = new LinkedList<>();
    private int[] candidates;

    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        Arrays.sort(candidates);
        this.candidates = candidates;
        recur(0,target);

        return res;
    }

    private void recur(int begin, int target) {
        int last = -1;
        for(int i = begin; i < candidates.length; i++) {
            if(candidates[i] == last) continue;

            if(candidates[i] <= target) {
                list.add(candidates[i]);
                if(candidates[i] == target)
                    res.add((List<Integer>)list.clone());
                else
                    recur(i + 1, target - candidates[i]);
                list.removeLast();
            }else break;

            last = candidates[i];
        }
    }
}

46. 全排列

class Solution {
    private List<List<Integer>> res = new LinkedList<>();
    private LinkedList<Integer> list = new LinkedList<>();

    public List<List<Integer>> permute(int[] nums) {
        recur(nums);
        return res;
    }

    private void recur(int[] nums) {
        if(list.size() == nums.length) 
            res.add((List<Integer>) list.clone());

        for(int i = 0; i < nums.length; i++) {
            if(list.contains(nums[i])) continue;
            list.add(nums[i]);
            recur(nums);
            list.removeLast();
        }
    }
}

字典树(前缀树)

648. 单词替换

class Solution {
    public String replaceWords(List<String> roots, String sentence) {
        TrieNode trie = new TrieNode();
        for (String root : roots) {
            TrieNode cur = trie;
            for (char letter : root.toCharArray()) {
                if (cur.children[letter - 'a'] == null) {
                    cur.children[letter - 'a'] = new TrieNode();
                }
                cur = cur.children[letter - 'a'];
            }
            cur.word = root;
        }

        StringBuilder ans = new StringBuilder();

        for (String word : sentence.split(" ")) {
            if (ans.length() > 0) {
                ans.append(" ");
            }

            TrieNode cur = trie;
            for (char letter : word.toCharArray()) {
                if (cur.children[letter - 'a'] == null || cur.word != null) {
                    break;
                }
                cur = cur.children[letter - 'a'];
            }
            ans.append(cur.word != null ? cur.word : word);
        }
        return ans.toString();
    }
}

class TrieNode {
    TrieNode[] children;
    String word;

    TrieNode() {
        children = new TrieNode[26];
    }
}

树的遍历

94. 二叉树的中序遍历

//迭代
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();

        while(!stack.isEmpty() || root != null) {
            while(root != null) {
                stack.push(root);
                root = root.left;
            }
            root = stack.pop();
            res.add(root.val);
            root = root.right;
        }

        return res;
    }
}
//递归
class Solution {
    private List<Integer> res = new ArrayList<>();

    public List<Integer> inorderTraversal(TreeNode root) {
        dfs(root);
        return res;
    }

    private void dfs(TreeNode root) {
        if(root == null) return;

        dfs(root.left);
        res.add(root.val);
        dfs(root.right);
    }
}

102. 二叉树的层序遍历

class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        if(root == null) return res;

        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while(queue.size() > 0) {
            int size = queue.size();
            List<Integer> temp = new ArrayList<>();
            for(int i = 0; i < size; i++) {
                TreeNode node = queue.poll();
                temp.add(node.val);
                if(node.left != null) queue.offer(node.left);
                if(node.right != null) queue.offer(node.right);
            }
            res.add(temp);
        }

        return res;
    }
}

110. 平衡二叉树

class Solution {
    public boolean isBalanced(TreeNode root) {
        return dfs(root) != -1;
    }

    private int dfs(TreeNode root) {
        if(root == null) return 0;

        int left = dfs(root.left);
        if(left == -1) return -1;
        int right = dfs(root.right);
        if(right == -1) return -1;

        return Math.abs(left - right) < 2 ? Math.max(left, right) + 1 : -1;
    }
}

144. 二叉树的前序遍历

//迭代
class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        if(root == null) return res;

        Stack<TreeNode> stack = new Stack<>();
        while(!stack.isEmpty() || root != null) {
            if(root != null) {
                res.add(root.val);
                stack.push(root);
                root = root.left;
            }else {
                TreeNode temp = stack.pop();
                root = temp.right;
            }
        }

        return res;
    }
}
//递归
class Solution {
    private List<Integer> res = new ArrayList<>();

    public List<Integer> preorderTraversal(TreeNode root) {
        dfs(root);
        return res;
    }

    private void dfs(TreeNode root) {
        if(root == null) return;

        res.add(root.val);
        dfs(root.left);
        dfs(root.right);
    }
}

145. 二叉树的后序遍历

//迭代
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        LinkedList<Integer> res = new LinkedList<>();
        if(root == null) return res;

        Stack<TreeNode> stack = new Stack<>();
        while(!stack.isEmpty() || root != null) {
            if(root != null) {
                res.addFirst(root.val);
                stack.push(root);
                root = root.right;
            }else {
                TreeNode temp = stack.pop();
                root = temp.left;
            }
        } 

        return res;
    }
}
//递归
class Solution {
    private List<Integer> res = new ArrayList<>();

    public List<Integer> postorderTraversal(TreeNode root) {
        dfs(root);
        return res;
    }

    private void dfs(TreeNode root) {
        if(root == null) return;

        dfs(root.left);
        dfs(root.right);
        res.add(root.val);
    }
}

二叉搜索树相关

98. 验证二叉搜索树

class Solution {
    long pre = Long.MIN_VALUE;

    public boolean isValidBST(TreeNode root) {
        if(root == null) return true;

        boolean left = isValidBST(root.left);
        if(root.val <= pre) return false;
        pre = root.val;
        boolean right = isValidBST(root.right);

        return left && right;
    }
}

450. 删除二叉搜索树中的节点

class Solution {
    public TreeNode deleteNode(TreeNode root, int key) {
        if(root == null) return root;

        if(root.val > key){
            root.left = deleteNode(root.left,key);
        }else if(root.val < key){
            root.right = deleteNode(root.right,key);
        }else{
            if(root.left != null && root.right != null){
                TreeNode temp = root.right;
                while(temp.left != null)
                    temp = temp.left;
                temp.left = root.left;
                return root.right;
            }
            if(root.left == null) return root.right;
            if(root.right == null) return root.left;
        }

        return root;
    }
}

701. 二叉搜索树中的插入操作

class Solution {
    public TreeNode insertIntoBST(TreeNode root, int val) {
        //找到空节点要么插左边要么插右边
        if(root == null) return new TreeNode(val);

        if(root.val < val)
            root.right = insertIntoBST(root.right,val);
        else if(root.val > val)
            root.left = insertIntoBST(root.left,val);
        
        return root;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

方圆想当图灵

嘿嘿,小赏就行,不赏俺也不争你

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值