美文网首页
LeetCode #344. Reverse String

LeetCode #344. Reverse String

作者: Nemo_WangCN | 来源:发表于2016-04-25 18:29 被阅读29次

Reverse String

题目要求:

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

Example:

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

题目大意:

输入一个字符串,将其按倒序输出。

例子:输入s='hello', 则输出'olleh'。

解题思路:

利用string字符串数组找到中间字符,将前后字符依次交换位置,遍历输出。

C++代码:

class Solution {

public:

   string reverseString(string s) {

       int i, n;

       char temp;

       n=s.size();

       for(i=0; i<n/2; i++)

       {

           temp=s[i];

           s[i] = s[n-i-1];

           str[n-i-1] = temp;

       }

       return s;

   }

};

因为还没学会使用Markdown,所以现在的代码只能这样丑丑的放在这里了。

相关文章

网友评论

      本文标题:LeetCode #344. Reverse String

      本文链接:https://www.haomeiwen.com/subject/fbhqrttx.html