Reverse String
Write a function that reverses a string. The input string is given as an array of characters
char[]
.Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
You may assume all the characters consist of printable ascii characters.
Example 1:
1 | Input: ["h","e","l","l","o"] |
Example 2:
1 | Input: ["H","a","n","n","a","h"] |
思路:
设置两个指针,一个指针指向首元素l,另一个指针指向末尾元素r,开始进行while循环,循环推出条件为l<r。 通过移动l,r两个指针交换元素。
代码:
1 | class Solution { |