首页 > 其他 > 详细

字符串逆序

时间:2016-06-04 16:08:39      阅读:160      评论:0      收藏:0      [点我收藏+]

题目:

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

思路:首尾字符交换

c++代码:

 1 class Solution {
 2 public:
 3     string reverseString(string s) {
 4         int len = s.size();
 5         for (int i = 0; i < len/2; i++) {
 6             char t = s[i];
 7             s[i] = s[len-i-1];
 8             s[len-i-1] = t;
 9         }
10         return s;
11     }
12 };

 

字符串逆序

原文:http://www.cnblogs.com/haileyzhang/p/5558827.html

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