[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 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] 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

[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

Binary Search: Stop point

There are a lot of problems related to binary search. For example, Search for a Range, Search Insert Position on Leetcode. In these problems, sometimes we are not required to find a exact number, but to find the position of a number that is just larger than it, or find the first instance of the target, … Read more

[LeetCode] Gas Station (Java)

There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations. Return the starting gas station’s index … Read more

[LeetCode] Sort Colors (Java)

Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively. Follow up: A rather straight forward … Read more

[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