Stay button --- 2020.3.14

300. The rise longest sequence

//binarySearch()方法,可以使用二分搜索法来搜索指定的数组,以获得指定对象。该方法返回要搜索元素的索引值。
class Solution {
    public int lengthOfLIS(int[] nums) {
        int[] res = new int[nums.length];
        int len = 0;
        for (int num: nums) {
            int idx = Arrays.binarySearch(res, 0, len, num);
            idx = idx < 0? -idx - 1: idx;
            res[idx] = num;
            if (idx == len) {
                len++;
            }
        }
        return len;
    }
}
class Solution {
    public int lengthOfLIS(int[] nums) {
        if (nums.length == 0) {
            return 0;
        }
        int[] dp = new int[nums.length];
        dp[0] = 1;
        int maxans = 1;
        for (int i = 1; i < dp.length; i++) {
            int maxval = 0;
            for (int j = 0; j < i; j++) {
                if (nums[i] > nums[j]) {
                    maxval = Math.max(maxval, dp[j]);
                }
            }
            dp[i] = maxval + 1;
            maxans = Math.max(maxans, dp[i]);
        }
        return maxans;
    }
}
class Solution {
    public int lengthOfLIS(int[] nums) {
        int[] tails = new int[nums.length];
        int res = 0;
        for(int num : nums) {
            int i = 0, j = res;
            while(i < j) {
                int m = (i + j) / 2;
                if(tails[m] < num) i = m + 1;
                else j = m;
            }
            tails[i] = num;
            if(res == j) res++;
        }
        return res;
    }
}

938. The binary search tree scope and

//递归实现深度优先搜索
class Solution {
    int ans;
    public int rangeSumBST(TreeNode root, int L, int R) {
        ans = 0;
        dfs(root, L, R);
        return ans;
    }

    public void dfs(TreeNode node, int L, int R) {
        if (node != null) {
            if (L <= node.val && node.val <= R)
                ans += node.val;
            if (L < node.val)
                dfs(node.left, L, R);
            if (node.val < R)
                dfs(node.right, L, R);
        }
    }
}
//迭代实现深度优先搜索
class Solution {
    public int rangeSumBST(TreeNode root, int L, int R) {
        int ans = 0;
        Stack<TreeNode> stack = new Stack();
        stack.push(root);
        while (!stack.isEmpty()) {
            TreeNode node = stack.pop();
            if (node != null) {
                if (L <= node.val && node.val <= R)
                    ans += node.val;
                if (L < node.val)
                    stack.push(node.left);
                if (node.val < R)
                    stack.push(node.right);
            }
        }
        return ans;
    }
class Solution {
    public int rangeSumBST(TreeNode root, int L, int R) {
        if (root == null) return 0;
        
        if(root.val >= L && root.val <= R) {
            //当前节点再两数之间,把自身值加到结果里取,并往左右子节点递归
            return root.val + rangeSumBST(root.left, L ,R) + rangeSumBST(root.right, L , R);
        } else if(root.val < L){
            //当前节点小于L,往右子节点寻找
            return rangeSumBST(root.right, L, R);
        } else {
            //当前节点大于R,往左子节点寻找
            return rangeSumBST(root.left, L, R);
        }
    }
}

1021. deleted outermost parentheses

class Solution {
    public String removeOuterParentheses(String S) {
        
        int left = 0;
        StringBuilder res = new StringBuilder();
        for (int i = 0; i < S.length(); i++) {
            if (S.charAt(i) == '(' && left++ > 0)
                res.append('(');
            if (S.charAt(i) == ')' && --left > 0)
                res.append(')');         
        }    
        return res.toString();
    }
}
class Solution {
    public String removeOuterParentheses(String S) {
        String ans = "";
        Stack<Character> stack = new Stack<Character>();
        char[] s = S.toCharArray();
        int begin = 1;
        for(int i = 0; i < s.length; i++)
        {
            if(s[i] == '(')    stack.push(s[i]);
            else
            {
                stack.pop();
                if(stack.isEmpty())
                {
                    ans += S.substring(begin, i);
                    begin = i + 2;
                }
            }
        }
        return ans;
    }
}

The more you know, the more you do not know.
Proper way without surgery, patients can still seek, there is no way to surgery, ending surgery.
If you have other questions, welcome message, we can discuss, learn together and progress together

He published 193 original articles · won praise 116 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_40722827/article/details/104872926