Sunday, March 31, 2013

Pascal's Triangle II


Pascal's Triangle IIOct 29 '12
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?
» Solve this problem

Pascal's Triangle


Pascal's TriangleOct 28 '12
Given numRows, generate the first numRows of Pascal's triangle.
For example, given numRows = 5,
Return
[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]

Regular Expression Matching


Regular Expression MatchingJan 8 '12
Implement regular expression matching with support for '.' and '*'.
'.' Matches any single character.
'*' Matches zero or more of the preceding element.

The matching should cover the entire input string (not partial).

The function prototype should be:
bool isMatch(const char *s, const char *p)

Some examples:
isMatch("aa","a") ? false
isMatch("aa","aa") ? true
isMatch("aaa","aa") ? false
isMatch("aa", "a*") ? true
isMatch("aa", ".*") ? true
isMatch("ab", ".*") ? true
isMatch("aab", "c*a*b") ? true
Thanks to  for the solution.
The basic idea is to use recursion for matching * as we dont know how many times we have to match a character preceding a *.

Saturday, March 30, 2013

Triangle


TriangleOct 30 '12
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.
For example, given the following triangle
[
     [2],
    [3,4],
   [6,5,7],
  [4,1,8,3]
]
The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11).
Note:
Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.

Wednesday, March 27, 2013

Palindrome Number


Palindrome NumberJan 4 '12
Determine whether an integer is a palindrome. Do this without extra space.

Reverse Integer


Reverse IntegerDec 26 '11
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321

Friday, March 15, 2013

Permutations II


Permutations IIMar 17 '12
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].