博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
344. Reverse String
阅读量:4949 次
发布时间:2019-06-11

本文共 431 字,大约阅读时间需要 1 分钟。

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

Example:

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

class Solution {
    public String reverseString(String s) {
        char[] ch = s.toCharArray();
        int start = 0;
        int end = s.length() - 1;
        while (start < end) {
            char temp = ch[start];
            ch[start] = ch[end];
            ch[end] = temp;
            start++;
            end--;
        }
        return new String(ch);
    }
}

转载于:https://www.cnblogs.com/MarkLeeBYR/p/10678211.html

你可能感兴趣的文章