컴퓨터/LeetCode

LeetCode 344 Reverse String Easy [Java]

이상한 나그네 2023. 12. 25. 23:22

문제: https://leetcode.com/problems/reverse-string/

 

Reverse String - LeetCode

Can you solve this real interview question? 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 [https://en.wikipedia.org/wiki/In-place_algo

leetcode.com

1. 코드

class Solution {
    public void reverseString(char[] s) {
        for(int i = 0; i < s.length / 2; i++) {
            char c = s[i];
            s[i] = s[s.length - i - 1];
            s[s.length - i - 1] = c;
        }
    }
}

2. 설명

char 배열을 반으로 나누어 양끝부터 교환하여 완성합니다.