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.
public boolean isPalindrome(String s) {
int i = 0, j = s.length() - 1;
while (i < j) {
char a = s.charAt(i), b = s.charAt(j);
if (!Character.isLetterOrDigit(a)) {
++i; continue;
}
if (!Character.isLetterOrDigit(b)) {
--j; continue;
}
if (Character.toLowerCase(a) != Character.toLowerCase(b)) return false;
++i; --j;
}
return true;
}
Container With Most Water
★★☆☆☆
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.
public int maxArea(int[] height) {
int left = 0, right = height.length - 1;
int maxArea = Math.min(height[left], height[right])*(right - left);
while (left < right) {
if (height[left] <= height[right]) {
int orileft = height[left];
while (left < height.length - 1 && height[left] <= orileft) left++;
}
else {
int oriright = height[right];
while (right > 0 && height[right] <= oriright) right--;
}
int area = Math.min(height[left], height[right])*(right - left);
maxArea = Math.max(maxArea, area);
}
return maxArea;
}