LeetCode Reverse String(字符串反转)

题意:给出一个字符串,将其反转

代码如下:

public class Solution
{
    public String reverseString(String s)
    {
        StringBuilder sb = new StringBuilder(s);

        int left = 0, right = sb.length() - 1;
        while (left < right)
        {
            char ch = sb.charAt(left);
            sb.setCharAt(left, sb.charAt(right));
            sb.setCharAt(right, ch);

            left++;
            right--;
        }

        return sb.toString();
    }
}


你可能感兴趣的:(#)