跳到主要内容

Reverse String

描述

Write a function to reverse a string. The input string is given as an array of characters.

You must do this by modifying the input array in-place with O(1) extra memory.

分析

无。

代码

class Solution {
public void reverseString(char[] s) {
int left = 0, right = s.length - 1;
while (left < right) {
char temp = s[left];
s[left] = s[right];
s[right] = temp;
left++;
right--;
}
}
}

相关题目