Posts

Pascal's Triangle II

Given an index  k , return the  k th  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? Solution: This one is easy. If we modify the previous array instead of creating a new array, we only need O(k) extra space. The following code passes the LeetCode Online Large Judge.

Pascal's Triangle

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] ] Solution: This one is easy. The following code passes LeetCode Online Large Judge. Redo it at Aug. 24, 2013. BUG free.

Valid Palindrome

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan, a canal: Panama"  is a palindrome. "race a car"  is  not  a palindrome. Note: Have you consider that the string might be empty? This is a good question to ask during an interview. For the purpose of this problem, we define empty string as valid palindrome. Solution: This one is easy. The following code passes LeetCode Online Large Judge. Redo it at Sep. 8, bug free JAVA version:

Gray Code

Image
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 10 - 2 Note: For a given  n , a gray code sequence is not uniquely defined. For example,  [0,2,3,1]  is also a valid gray code sequence according to the above definition. Solution: There is a good instruction on  Wiki  about how to generate a gray code recursively. The following code passes LeetCode Online Large Judge. Update: Thank Yao for providing a much neater version Update: my own solution when I redo this problem (2013.05.17). NO RECURSION AND BUG FREE!

Integer to Roman

Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Solution: This problem is simple. Just need to check  wiki  to understand how to represent a roman number... Still get bug-free after almost one month break, hooray :) The following code passes LeetCode Online Large Judge.

Spiral Matrix II

Given an integer n, generate a square matrix filled with elements from 1 to n^2 in spiral order. Solution: Similar to Spiral Matrix . Actually it is easier since we always generate square matrix, so the number of bound conditions are much less. Not sure how to directly use vectors. Here we use 2d array as first step. The following code passes LeetCode Online Large Judge.

Spiral Matrix

Given a matrix of  m  x  n  elements ( m  rows,  n  columns), return all elements of the matrix in spiral order. For example, Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return  [1,2,3,6,9,8,7,4,5] . Solution: Recursion. From the outer loop to the inner loop.  The following code passes LeetCode Online Large Judge.