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 n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. | |
Note: You may not slant the container. | |
*/ | |
public class Solution { | |
public int maxArea(int[] height) { | |
if (height==null || height.length<2){ | |
return 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
/* | |
Follow up for "Remove Duplicates": | |
What if duplicates are allowed at most twice? | |
For example, | |
Given sorted array A = [1,1,1,2,2,3], | |
Your function should return length = 5, and A is now [1,1,2,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
""" | |
Follow up for "Remove Duplicates": | |
What if duplicates are allowed at most twice? | |
For example, | |
Given sorted array A = [1,1,1,2,2,3], | |
Your function should return length = 5, and A is now [1,1,2,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 an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. | |
For example, | |
Given n = 3, | |
You should return the following matrix: | |
[ | |
[ 1, 2, 3 ], | |
[ 8, 9, 4 ], |
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
public class Solution { | |
public void setZeroes(int[][] matrix) { | |
if (matrix==null||matrix.length==0){ | |
return; | |
} | |
boolean[] rows=new boolean[matrix.length]; | |
boolean[] cols=new boolean[matrix[0].length]; | |
for (int row=0; row<matrix.length; row++){ |
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
# without extra space | |
class Solution: | |
# @param matrix, a list of lists of integers | |
# RETURN NOTHING, MODIFY matrix IN PLACE. | |
def setZeroes(self, matrix): | |
if matrix==None or len(matrix)==0: | |
return | |
# use first col and row as buffer to hold a flag to check if accordinate col or row should be 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
class Solution: | |
# @param A a list of integers | |
# @param target an integer | |
# @return a boolean | |
def search(self, A, target): | |
if A==None or len(A)==0: | |
return False | |
start=0; | |
end=len(A)-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
class Solution: | |
# @return a list of integers | |
def getRow(self, rowIndex): | |
if rowIndex<0: | |
return None | |
result=[0]*(rowIndex+1) | |
result[0]=1 | |
for i in range(1,len(result)): | |
result[i]=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 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? | |
*/ | |
//6/2014 |
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 and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. | |
For example: | |
Given the below binary tree and sum = 22, | |
5 | |
/ \ | |
4 8 | |
/ / \ | |
11 13 4 | |
/ \ \ |