leetcode --344. Reverse String

Posted LaplaceAkuir

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 };

 

以上是关于leetcode --344. Reverse String的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 344. 反转字符串 Reverse String

leetcode 344. Reverse String

LeetCode之344. Reverse String

leetcode --344. Reverse String

LeetCode #344. Reverse String

leetcode344 Reverse String Java