917. 仅仅反转字母

https://leetcode-cn.com/problems/reverse-only-letters/

执行用时:0 ms, 在所有 Java 提交中击败了100.00% 的用户
内存消耗:39.4 MB, 在所有 Java 提交中击败了12.78% 的用户
通过测试用例:115 / 115

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public String reverseOnlyLetters(String s) {
int left = 0, right = s.length() -1;
char[] str = s.toCharArray();
while (left < right) {
char leftCh = s.charAt(left);
char rightCh = s.charAt(right);
if (Character.isLetter(leftCh) && Character.isLetter(rightCh)) {
char temp = str[left];
str[left] = str[right];
str[right] = temp;
left++;
right--;
}
if (!Character.isLetter(leftCh)) {
left++;
}
if (!Character.isLetter(rightCh)) {
right--;
}
}
return new String(str);
}
}

917. 仅仅反转字母
https://pisces34.github.io/2022/02/23/leetcode/917/
发布于
2022年2月23日
许可协议