[LeetCode] Longest Palindromic Substring (Java)

Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. Analysis This is a very basic dynamic programming problem. We use a 2-D boolean array P to save whether a substring of s is palindromic. We start from substring of length … Read more [LeetCode] Longest Palindromic Substring (Java)

[LeetCode] Longest Substring Without Repeating Characters (Java)

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for “abcabcbb” is “abc”, which the length is 3. For “bbbbb” the longest substring is “b”, with the length of 1. Analysis We can use to pointers indicating the substring we are processing. And … Read more [LeetCode] Longest Substring Without Repeating Characters (Java)

[LeetCode] Median of Two Sorted Arrays (Java)

There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Updated 09/22/2016: http://www.lifeincode.net/programming/leetcode-median-of-two-sorted-arrays-more-elegant-solution/ Analysis We can solve this problem with the algorithm: Finding the Kth element in two sorted arrays. It’s quite straight forward. For … Read more [LeetCode] Median of Two Sorted Arrays (Java)

[Hadoop] Problems when running simple examples

Environment: Cloudera Cent OS 6.2 (CDH 5.0). When I run a FileSystemCat example, I found there is something that needed to be configured before you compile the .class file and run it on Hadoop. 1. Source file should be compiled to “.class” file to be run on Hadoop. My source file is FileSystemCat.java. But when … Read more [Hadoop] Problems when running simple examples

[Algorithm] Level Order Binary Tree Traversal (Iterative and Recursive)

Traverse a binary tree in level order is intuitive. We can initialize a queue and add elements. Every cycle you need to print out the nodes in the queue, and add their children to queue, until queue is empty. However, since the recursive solution for pre-order, in-order, and post-order traverse is quite easy, I wonder … Read more [Algorithm] Level Order Binary Tree Traversal (Iterative and Recursive)

[LeetCode] Longest Valid Parentheses

Got stuck in this problem for one hour. I was trying to use a variable left to count the left parentheses. However, we should not only count the number of them, but also the position of them. Then we need to use stack. There is a very elegant solution here (In Java): http://rleetcode.blogspot.com/2014/01/longest-valid-parentheses.html This problem really cost … Read more [LeetCode] Longest Valid Parentheses