leetcode 125. 验证回文串
public boolean isPalindrome(String s) {
if (s == null || s.length() < 2) {
return true;
}
char[] chs = s.toCharArray();
int l = 0, r = s.length() - 1;
while (l < r) {
while (l < r && !Character.isLetterOrDigit(chs[l])) l++;
while (l < r && !Character.isLetterOrDigit(chs[r])) r--;
if (l < r) {
if (Character.toLowerCase(chs[l]) != Character.toLowerCase(chs[r])) {
return false;
}
l++;
r--;
}
}
return true;
}
leetcode 344. 反转字符串
public void reverseString(char[] s) {
if(s == null || s.length < 2)
return;
int l = 0, r = s.length - 1;
while(l < r) {
s[l] ^= s[r];
s[r] ^= s[l];
s[l++] ^= s[r--];
}
}
leetcode 345. 反转字符串中的元音字母
public String reverseVowels(String s) {
if (s == null || s.length() < 2)
return s;
char[] chs = s.toCharArray();
int l = 0, r = chs.length - 1;
while (l < r) {
while (l < r && !isVowel(chs[l])) l++;
while (l < r && !isVowel(chs[r])) r--;
if (l < r) {
chs[l] ^= chs[r];
chs[r] ^= chs[l];
chs[l] ^= chs[r];
l++;
r--;
}
}
return String.valueOf(chs);
}
public boolean isVowel(char letter) {
letter = Character.toLowerCase(letter);
if (letter == 'a' ||
letter == 'e' ||
letter == 'i' ||
letter == 'o' ||
letter == 'u') {
return true;
}
return false;
}
leetcode 11. 盛最多水的容器
public int maxArea(int[] H) {
if(H == null || H.length < 2) {
return 0;
}
int maxCap = 0;
int i = 0, j = H.length - 1;
while(i < j) {
int cap = Math.min(H[i],H[j]) * (j - i);
maxCap = Math.max(maxCap, cap);
if(H[i] < H[j]) {
i++;
} else if(H[i] > H[j]) {
j--;
} else {
i++;
j--;
}
}
return maxCap;
}