【LeetCode】344. Reverse String

题目:

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) {
        string str = s;
        if (str == "") return str;
        if (str.size() == 1) return str;
        for (int i = 0, j = str.size()-1; i < j;){
            char tmp = str[i];
            str[i] = str[j];
            str[j] = tmp;
            i++;
            j--;
        }
        return str;
    }
};

 

posted on 2016-09-12 23:08  医生工程师  阅读(142)  评论(0编辑  收藏  举报

导航