首页 > 其他 > 详细

LeetCode #344. Reverse String

时间:2017-03-11 12:51:23      阅读:139      评论:0      收藏:0      [点我收藏+]

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++代码:

 1 class Solution {
 2 public:
 3    string reverseString(string s) {
 4        int i, n;
 5        char temp;
 6        n=s.size();
 7 
 8        for(i=0; i<n/2; i++)
 9        {
10            temp=s[i];
11            s[i] = s[n-i-1];
12            str[n-i-1] = temp;
13        }
14 
15        return s;
16    }
17 };

 

LeetCode #344. Reverse String

原文:http://www.cnblogs.com/nemowang1996/p/6534516.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!