344. Reverse String

题目大意:输入一个字符串,返回字符串的反转,例如输入“hello”,返回“olleh”

344. Reverse String

Write a function that takes a string as input and returns the string reversed.

Example:

Given s = "hello", return "olleh".

public String reverseString(String s) {
    char[] c = s.toCharArray();
    char temp;
    int startIndex = 0;
    int endIndex = c.length - 1;
    while (endIndex > startIndex) {
        temp = c[startIndex];
        c[startIndex] = c[endIndex];
        c[endIndex] = temp;
        startIndex ++;
        endIndex --;
    }
    return new String(c);
}

Last updated