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 numbers that might contain duplicates, return all possible unique permutations. | |
For example, | |
[1,1,2] have the following unique permutations: | |
[1,1,2], [1,2,1], and [2,1,1]. | |
Algorithm: | |
Basically it is similar to Permutation, but it needs | |
to check the condition that if the current num is the |
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 numbers, return all possible permutations. | |
For example, | |
[1,2,3] have the following permutations: | |
[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. | |
Algorithm: | |
1. check if the list is equal to the size of the num array | |
2. loop through all the possibility |
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
/* | |
You are given an n x n 2D matrix representing an image. | |
Rotate the image by 90 degrees (clockwise). | |
Follow up: | |
Could you do this in-place? | |
Basic approach has 2 steps: | |
1. rotate based on the right most to left bottom diagonal |
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
/* | |
Cases need to be taken care of: | |
1. sign | |
2. extra space in the front | |
3. half valid number | |
4. overflow for the large number | |
5. invalid number | |
*/ | |
public class Solution { | |
public int atoi(String str) { |
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 preorder and inorder 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
/* | |
The gray code is a binary numeral system where two successive values differ in only one bit. | |
Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0. | |
For example, given n = 2, return [0,1,3,2]. Its gray code sequence is: | |
00 - 0 | |
01 - 1 | |
11 - 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
/* | |
A message containing letters from A-Z is being encoded to numbers using the following mapping: | |
'A' -> 1 | |
'B' -> 2 | |
... | |
'Z' -> 26 | |
Given an encoded message containing digits, determine the total number of ways to decode it. | |
For example, |
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 an index k, return the kth row of the Pascal's triangle. | |
For example, given k = 3, | |
Return [1,3,3,1]. | |
Note: | |
Could you optimize your algorithm to use only O(k) extra space? | |
*/ | |
public class Solution { |
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 numRows, generate the first numRows of Pascal's triangle. | |
For example, given numRows = 5, | |
Return | |
[ | |
[1], | |
[1,1], | |
[1,2,1], |
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 integers n and k, return all possible combinations of k numbers out of 1 ... n. | |
For example, | |
If n = 4 and k = 2, a solution is: | |
[ | |
[2,4], | |
[3,4], | |
[2,3], |