[Leetcode] 344. Reverse String 解题报告

来源:互联网 发布:linux合并两个文件夹 编辑:程序博客网 时间:2024/06/10 18:51

题目

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) {        if (s.length() <= 1) {            return s;        }        for (int i = 0; i < s.length() / 2; ++i) {            swap(s[i], s[s.length() - 1 - i]);        }        return s;    }};

原创粉丝点击