LeetCode 344. Reverse String

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


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);
    }
}

댓글을 남기시려면 Github 로그인을 해주세요 :D


Hi, there!

Thanks for visiting my blog.
Please let me know if there are any mistakes in my post.