码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode --344. Reverse String

时间:2016-10-01 22:10:16      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:

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

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

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

 

 1 class Solution{
 2 public:
 3     string reverseString(string s){
 4         int i , j , n;
 5         n = strlen(s);
 6         char* tmp = (char*)malloc(sizeof(char)*(n+1));
 8         for(i=n,j=0;i>0;i--,j++)
 9            tmp[j] = s[i-1];
11         tmp[n] = \0;
13         return tmp ;
14     }
15 };

 

leetcode --344. Reverse String

标签:

原文地址:http://www.cnblogs.com/LaplaceAkuir/p/5926293.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!