본문 바로가기

Algorithm

[LeetCode] Reverse String

문제 : 

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.

 

- 주어진 문자형 배열 s를 수정하여 return (in-place) 

- O(1)의 시간복잡도를 가진 추가 메모리만을 활용하여 풀기 

 

*O(1) - 일정한 복잡도(constant complexity)

: 입력값이 증가하더라도 시간이 늘어나지 않는다.

 

 

풀이 : 

/**
 * @param {character[]} s
 * @return {void} Do not return anything, modify s in-place instead.
 */
var reverseString = function(s) {
    return s.reverse();
};