leetcode --344. Reverse String

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 };

 

posted @ 2016-10-01 20:06  圆滚滚的小峰峰  阅读(212)  评论(0编辑  收藏  举报