Sunday, October 7, 2018
917. Reverse Only Letters
Version #1 Two Pointers
class Solution {
public String reverseOnlyLetters(String S) {
if (S == null || S.length() == 0) {
return S;
}
char[] chars = S.toCharArray();
int left = 0;
int right = S.length() - 1;
while (left < right) {
while (left < right && !Character.isLetter(chars[left])) {
left++;
}
while (left < right && !Character.isLetter(chars[right])) {
right--;
}
char temp = chars[left];
chars[left] = chars[right];
chars[right] = temp;
left++;
right--;
}
return new String(chars);
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment