LeetCode 344. Reverse String

입력한 문자열을 거꾸로 출력하기

#Algotithm #LeetCode


Description

Write a function that takes a string as input and returns the string reversed. Example: Given s = “hello”, return “olleh”.

public class Solution {
    public String reverseString(String s) {
        
    }
}

Reference link (Click!)



Solution

/**
 * 344. Reverse String
 *
 * @author kimtaeng
 * created on 2018. 1. 10.
 */
public class Solution {
    public String reverseString(String s) {
        char[] arr = s.toCharArray();
        int inputLength = s.length();
        for (int currentIndex = 0; currentIndex < inputLength / 2; currentIndex++) {
            char temp = arr[currentIndex];
            arr[currentIndex] = arr[inputLength - 1 - currentIndex];
            arr[inputLength - 1 - currentIndex] = temp;
        }
        return String.valueOf(arr);
    }
}