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
/* | |
Implement int sqrt(int x). | |
Compute and return the square root of x. | |
Algorithm: | |
1. binary search | |
2. make sure the right < sqrt(Integer.MAX_VALUE) | |
*/ | |
public class Solution { | |
public int sqrt(int x) { | |
// Start typing your Java solution below |
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 count-and-say sequence is the sequence of integers beginning as follows: | |
1, 11, 21, 1211, 111221, ... | |
1 is read off as "one 1" or 11. | |
11 is read off as "two 1s" or 21. | |
21 is read off as "one 2, then one 1" or 1211. | |
Given an integer n, generate the nth sequence. |
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 sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. | |
For example, | |
Given 1->2->3->3->4->4->5, return 1->2->5. | |
Given 1->1->1->2->3, return 2->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 sorted linked list, delete all duplicates such that each element appear only once. | |
For example, | |
Given 1->1->2, return 1->2. | |
Given 1->1->2->3->3, return 1->2->3. | |
*/ | |
/** | |
* Definition for singly-linked list. |
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 m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. | |
Algorithm: | |
1. No need extra space and it has O(n*m) complexity in time | |
2. Go throught the matrix and if it meets 0, then set its row and col to ZERO | |
3. Once complete, then go throught the matrix again and change ZERO to 0; | |
*/ | |
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 s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. | |
For example, | |
Given: | |
s1 = "aabcc", | |
s2 = "dbbca", | |
When s3 = "aadbbcbcac", return true. | |
When s3 = "aadbbbaccc", return false. |
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 climbing a stair case. It takes n steps to reach to the top. | |
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? | |
Algorithm: | |
1. it is a DP problem and it is the same as the fibonacci series. | |
*/ | |
public class Solution { | |
public int climbStairs(int n) { |
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
/* | |
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: | |
Integers in each row are sorted from left to right. | |
The first integer of each row is greater than the last integer of the previous row. | |
For example, | |
Consider the following matrix: | |
[ |
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 strings, return their sum (also a binary string). | |
For example, | |
a = "11" | |
b = "1" | |
Return "100". | |
Algorithm: | |
Make sure take care of the edge case when the carry > 0 and aLen < 0, bLen < 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
/* | |
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). | |
For example, | |
S = "ADOBECODEBANC" | |
T = "ABC" | |
Minimum window is "BANC". | |
Note: |