본문 바로가기

LeetCode/String

[Easy] 344. Reverse String

Leetcode 344번 문제는 단순히 주어진 문자열을 뒤집는 문제이다. 단 Space complexity (공간 복잡도)  가 O(1) 이

되도록 해야 한다. 

 

class Solution {
public void reverseString(char[] s) {

    int i = 0, j = s.length - 1;
    while(j > i){
        char temp = s[j];
        s[j--] = s[i];
        s[i++] = temp;
    }
    }
}