This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Suppose a sorted array is rotated at some pivot unknown to you beforehand. | |
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). | |
You are given a target value to search. If found in the array return its index, otherwise return -1. | |
You may assume no duplicate exists in the array. | |
Algorithm: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Suppose a sorted array is rotated at some pivot unknown to you beforehand. | |
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). | |
You are given a target value to search. If found in the array return its index, otherwise return -1. | |
You may assume no duplicate exists in the array. | |
Algorithm: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Given a number represented as an array of digits, plus one to the number. | |
*/ | |
public class Solution { | |
public int[] plusOne(int[] digits) { | |
// Start typing your Java solution below | |
// DO NOT write main() function | |
int len = digits.length - 1; | |
digits[len] = digits[len] + 1; | |
int carry = digits[len] >= 10 ? 1 : 0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Merged two sorted array. | |
merged B into A | |
*/ | |
public class Solution { | |
public void merge(int A[], int m, int B[], int n) { | |
// Start typing your Java solution below | |
// DO NOT write main() function |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Given two binary trees, write a function to check if they are equal or not. | |
Two binary trees are considered equal if they are structurally identical and the nodes have the same value. | |
Algorithm: | |
1. It is a recursive function that check p.left and q.left, | |
and p.right and p.right, and p.val and q.val | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Given inorder and postorder traversal of a tree, construct the binary tree. | |
Note: | |
You may assume that duplicates do not exist in the tree. | |
*/ | |
/** | |
* Definition for binary tree | |
* public class TreeNode { | |
* int val; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). | |
For example, this binary tree is symmetric: | |
1 | |
/ \ | |
2 2 | |
/ \ / \ | |
3 4 4 3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). | |
For example: | |
Given binary tree {3,9,20,#,#,15,7}, | |
3 | |
/ \ | |
9 20 | |
/ \ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Given a collection of integers that might contain duplicates, S, return all possible subsets. | |
Note: | |
Elements in a subset must be in non-descending order. | |
The solution set must not contain duplicate subsets. | |
For example, | |
If S = [1,2,2], a solution is: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static class Wrapper { | |
TreeNode node; | |
int level; | |
Wrapper(TreeNode node, int level) { | |
this.node = node; | |
this.level = level; | |
} | |
} | |
static int skip; |