[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] Container With Most Water (Java)

[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] ZigZag Conversion (Java)

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