Skip to content

Instantly share code, notes, and snippets.

/*
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;
/*
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].
*/
"""
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].
"""
"""
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 ],
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++){
# 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
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
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
/*
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
@Ray1988
Ray1988 / PathSum.py
Created May 28, 2014 03:58
Python Version
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
/ \ \