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
Input: ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]
example 2
Input: ["H","a","n","n","a","h"]
Output: ["h","a","n","n","a","H"]
추가로 다른 배열을 할당하지 않고 들어온 input 배열의 index 순서를 바꾸는 문제이다.
그렇기 때문에 unshift 같은 메소드로 배열에 새로 넣을 수 없다.
var reverseString = function(s) {
let left = 0;
let right = s.length - 1;
while(left < right){
let temp = s[left];
s[left] = s[right];
s[right] = temp;
++left;
--right;
}
return s;
};
인덱스를 활용해서 간단하게 풀 수 있는 문제였다!
Life is short...
var reverseString = function(s) {
return s.reverse();
};
끝😀