leetcode [#22] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (11)
目录1. 题目2. 解决方案3. 注意事项
题目Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
[  “((()))”,  “(()())”,  “(())()”,  “()(())”,  “()()()”]
解决方案123456789101112131415public class Solution {    public List<String> generateParenthesis(int n) {        List<String> res = new ArrayList<>();        recursivelyGetParenthesis(res, "", 0, 0, n);        return res;    }    private static void recursivelyGetParenth...阅读全文

leetcode [#226] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (14)
目录1. 题目2. 解决方案3. 注意事项
题目
解决方案1234567891011121314151617181920212223/** * Definition for a binary tree node. * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */public class Solution {    public TreeNode invertTree(TreeNode root) {        if(root != null) {            TreeNode tmp = root.left;            root.left = root.right;            root.right = tmp;            invertTree(root.left);            invertTree(root.right);...阅读全文

leetcode [#226] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (9)
目录1. 题目2. 解决方案3. 注意事项
题目
解决方案12345678910111213public class Solution {    public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {        int s1 = (C - A) * (D - B);        int s2 = (G - E) * (H - F);        int left = Math.max(A, E);        int right = Math.min(G, C);        int top = Math.min(D, H);        int bottom = Math.max(B, F);        int common = 0;        if(right > left && top > bottom) common = (right - left) * (top - bottom);        return  s1 + s2 - common...阅读全文

leetcode [#231] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (19)
目录1. 题目2. 解决方案3. 注意事项
题目Given an integer, write a function to determine if it is a power of two.
解决方案1234567891011public class Solution {    public boolean isPowerOfTwo(int n) {        if(n < 0) return false;        if(n == 1) return true;        double n1 = (double) n;        while (n1 / 2.0 >= 1.0) {            n1 = n1 / 2.0;        }        return n1 == 1.0 ? true : false;    }}注意事项先处理特殊情况。将n转换为double型,不断除以2,直到结果小于1.0.此时根据n1的结果即可确定。...阅读全文

leetcode [#232] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (9)
目录1. 题目2. 解决方案3. 注意事项
题目Implement the following operations of a queue using stacks.
push(x) – Push element x to the back of queue.pop() – Removes the element from in front of queue.peek() – Get the front element.empty() – Return whether the queue is empty.Note
You must use only standard operations of a stack – which means only push to top, peek/pop from top, size, and is empty operations are valid.Depending on your language, stack may not be supported natively. You may simulate a stack by usi...阅读全文