Write a function that reverses a string. The input string is given as an array of characters s.
You must do this by modifying the input array in-place with O(1) extra memory.
Input: s = ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]
Input: s = ["H","a","n","n","a","h"]
Output: ["h","a","n","n","a","H"]
//https://leetcode.com/explore/interview/card/top-interview-questions-easy/127/strings/879/
class Solution {
public void reverseString(char[] s) {
this.s = s;
int n = s.length;
for (int i = 0; i < n / 2; i++) {
char temp = s[i];
s[i] = s[n - i - 1];
s[n - i - 1] = temp;
}
}
}
left, right(stard, end)에 포인트를 둔다
left는 ++, right는 --
class Solution {
public void reverseString(char[] s) {
this.s = s;
int left = 0;
int right = s.length - 1;
while (left <= right) {
char temp = s[left];
s[left] = s[right];
s[right] = temp;
left++;
right--;
}
}
}