[LeetCode] Longest Common Prefix (Java)

Write a function to find the longest common prefix string amongst an array of strings. Analysis This is a simple problem. We can just check for each position of every string in the string array. But we need to take care of some strings that is shorter than others. Since there is two loops, we … Read more

[LeetCode] Container With Most Water (Java)

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. Analysis It could take … Read more

[LeetCode] Reverse Integer (Java)

Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Spoilers: Have you thought about this?Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!If the integer’s last digit is 0, what should the output be? ie, cases such as … Read more

[LeetCode] ZigZag Conversion (Java)

The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)

And then read line by line: “PAHNAPLSIIGYIR” Write the code that will take a string and make this conversion given a number of rows:

convert(“PAYPALISHIRING”, … Read more

[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