344. Reverse String

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

Solution1:前后two pointers对换

Time Complexity: O(N) Space Complexity: O(N)

Solution Code:

class Solution {
    public String reverseString(String s) {
        char[] chars = s.toCharArray();
        int i = 0, j = s.length() - 1;
        while (i < j) {
            char tmp = chars[i];
            chars[i] = chars[j];
            chars[j] = tmp;
            i++;
            j--;
        }
        return new String(chars);
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 14,351评论 0 33
  • Reverse String 题目要求: Write a function that takes a string...
    Nemo_WangCN阅读 1,865评论 0 0
  • Write a function that takes a string as input and returns...
    腹黑君阅读 1,040评论 0 0
  • 我遇见谁,会有怎样的对白;我等的人他在多远的未来。------《遇见》孙燕姿 少年在等,经年累月地等,坚...
    艾叶姑娘阅读 1,462评论 0 1
  • 2017年6月19日 晴 今天晚上下班回家,崔政飞说他今天在学校还是感觉不舒服。我说今天晚饭给你做蛋炒饭吧。他说...
    巭Pro阅读 825评论 0 1