TreeviewCopyright © aleen42 all right reserved, powered by aleen42
917. 仅仅反转字母
Java
/*
* @Author: Goog Tech
* @Date: 2020-08-21 18:35:39
* @LastEditTime: 2020-08-21 18:36:02
* @Description: https://leetcode-cn.com/problems/reverse-only-letters/
* @FilePath: \leetcode-googtech\#917. Reverse Only Letters\Solution.java
* @WebSite: https://algorithm.show/
*/
class Solution {
// 双指针法
public String reverseOnlyLetters(String S) {
// 初始化头尾指针
int left = 0, right = S.length() - 1;
// 将字符串转换为字符数组
char[] chars = S.toCharArray();
while(left < right) {
// 若头元素不是字母则将头指针持续向后移动,同理后指针持续向前移动
while(!(Character.isLetter(chars[left])) && left < right) left++;
while(!(Character.isLetter(chars[right])) && left < right) right--;
// 直到头尾指针所指元素都是字母,然后交换其元素位置
char c = chars[left];
chars[left] = chars[right];
chars[right] = c;
// 继续移动头尾指针
left++;
right--;
}
// 将字符数组转换为字符串并返回
return String.valueOf(chars);
}
}
Python
'''
Author: Goog Tech
Date: 2020-08-21 18:35:45
LastEditTime: 2020-08-21 18:36:21
Description: https://leetcode-cn.com/problems/reverse-only-letters/
FilePath: \leetcode-googtech\#917. Reverse Only Letters\Solution.py
WebSite: https://algorithm.show/
'''
class Solution(object):
def reverseOnlyLetters(self, S):
"""
:type S: str
:rtype: str
"""
# 将字符串转换为列表数组
S = list(S)
# 初始化双指针
front, end = 0, len(S) - 1
# 通过移动双指针循环遍历数组元素
while front < end:
# 遇到非字母时,头指针向后移动,尾指针向前移动
if not S[front].isalpha():
front += 1
elif not S[end].isalpha():
end -= 1
# 当头尾指针所指元素都为字母时,交换元素位置,然后继续移动头尾指针
else:
S[front], S[end] = S[end], S[front]
front += 1
end -= 1
# 将列表数组转化为字符串并返回
return ''.join(S)