Skip to content

Instantly share code, notes, and snippets.

@cixuuz
cixuuz / 545_0902.java
Last active September 2, 2017 19:44
[545. Boundary of Binary Tree] #leetcode
class Solution {
// O(n) O(n)
public List<Integer> boundaryOfBinaryTree(TreeNode root) {
List<Integer> res = new ArrayList<Integer>();
if (root == null) return res;
if (root.left == null && root.right == null) {
res.add(root.val);
return res;
}
@cixuuz
cixuuz / 553_0902.java
Created September 2, 2017 17:25
[553. Optimal Division] #leetcode
class Solution {
// O(n) O(n)
public String optimalDivision(int[] nums) {
StringBuilder sb = new StringBuilder();
sb.append(nums[0]);
for (int i = 1; i < nums.length; i++) {
sb.append("/");
if (i == 1 && nums.length>2) {
sb.append("(");
}
@cixuuz
cixuuz / 579_0902.sql
Created September 2, 2017 16:31
[579. Find Cumulative Salary of an Employee] #leetcode
SELECT
E1.id,
E1.month,
(IFNULL(E1.salary, 0) + IFNULL(E2.salary, 0) + IFNULL(E3.salary, 0)) AS Salary
FROM
(SELECT
id, MAX(month) AS month
FROM
Employee
GROUP BY id
@cixuuz
cixuuz / 606_0902.java
Created September 2, 2017 16:17
[606. Construct String from Binary Tree] #leetcode
class Solution {
public String tree2str(TreeNode t) {
if (t == null) return "";
String left = tree2str(t.left);
String right = tree2str(t.right);
StringBuilder sb = new StringBuilder();
sb.append(t.val);
if (right.equals("")) {
if (!left.equals("")) sb.append("(" + left + ")");
@cixuuz
cixuuz / 617_0902.java
Last active September 2, 2017 15:05
[617. Merge Two Binary Trees] #leetcode
class Solution {
public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
if (t1 == null && t2 == null) return null;
TreeNode node = new TreeNode(t1 == null? t2.val : t2 == null? t1.val : t1.val + t2.val);
node.left = mergeTrees(t1 == null? null : t1.left, t2 == null? null : t2.left);
node.right = mergeTrees(t1 == null? null : t1.right, t2 == null? null : t2.right);
return node;
}
@cixuuz
cixuuz / 640_0901.java
Created September 1, 2017 17:33
[640. Solve the Equation] #leetcode
class Solution {
// O(n) O(1)
public String solveEquation(String equation) {
int x = 0, c = 0, num = -1, sign = 1;
boolean flag = false; // if at right of equal sign
for (int i = 0; i < equation.length(); i++) {
Character s = equation.charAt(i);
if (Character.isDigit(s)) {
if (num == -1) {
@cixuuz
cixuuz / 662_0901.java
Last active September 1, 2017 15:45
[662. Maximum Width of Binary Tree] #leetcode
class Solution {
// O(n) O(n)
public int widthOfBinaryTree(TreeNode root) {
if ( root == null ) return 0;
Deque<TreeNode> queue = new LinkedList<TreeNode>();
List<Integer> idx = new ArrayList<Integer>();
queue.addLast(root);
idx.add(0);
@cixuuz
cixuuz / 129_0829.java
Created August 30, 2017 02:05
[129. Sum Root to Leaf Numbers] #leetcode
class Solution {
private int res = 0;
public int sumNumbers(TreeNode root) {
if ( root == null ) return 0;
dfs(root, 0);
return res;
}
public void dfs(TreeNode node, int sum) {
@cixuuz
cixuuz / 661_0829.java
Created August 29, 2017 21:42
[661. Image Smoother] #leetcode
class Solution {
public int[][] imageSmoother(int[][] M) {
if (M == null) return null;
int rows = M.length;
if (rows == 0) return new int[0][];
int cols = M[0].length;
int result[][] = new int[rows][cols];
@cixuuz
cixuuz / 117_0829.java
Last active August 29, 2017 20:54
[117. Populating Next Right Pointers in Each Node II] #leetcode
public class Solution {
// O(n) O(n)
public void connect(TreeLinkNode root) {
if ( root == null ) return;
Deque<TreeLinkNode> deque = new LinkedList<TreeLinkNode>();
deque.addLast(root);
while ( !deque.isEmpty() ) {
int n = deque.size();