2000. 反转单词前缀

题目链接:https://leetcode-cn.com/problems/reverse-prefix-of-word/

示例 1:

输入:word = “abcdefd”, ch = “d”
输出:”dcbaefd”
解释:”d” 第一次出现在下标 3 。
反转从下标 0 到下标 3(含下标 3)的这段字符,结果字符串是 “dcbaefd” 。

执行用时:0 ms, 在所有 Java 提交中击败了100.00% 的用户
内存消耗:39.4 MB, 在所有 Java 提交中击败了8.40% 的用户

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public String reversePrefix(String word, char ch) {
int right = word.indexOf(ch);
int left = 0;
char temp = 0;
char[] chars = word.toCharArray();
while (left < right) {
temp = chars[left];
chars[left] = chars[right];
chars[right] = temp;
right--;
left++;
}
return new String(chars);
}
}

2000. 反转单词前缀
https://pisces34.github.io/2022/02/02/leetcode/2000/
发布于
2022年2月2日
许可协议