2022.02.23 - 250.仅仅反转字母
作者:互联网
文章目录
1. 题目
2. 思路
(1) 双指针法
- 利用双指针法反转字母位置即可。
3. 代码
public class Test {
public static void main(String[] args) {
}
}
class Solution {
public String reverseOnlyLetters(String s) {
char[] chars = s.toCharArray();
int left = 0;
int right = chars.length - 1;
while (left < right) {
while (left < right && !Character.isLetter(chars[left])) {
left++;
}
while (left < right && !Character.isLetter(chars[right])) {
right--;
}
char c = chars[left];
chars[left] = chars[right];
chars[right] = c;
left++;
right--;
}
return String.valueOf(chars);
}
}
标签:right,String,23,chars,public,while,250,2022.02,left 来源: https://blog.csdn.net/qq_44021223/article/details/123082516