Leetcode 344. Reverse String

344. Reverse String 

  • Total Accepted: 56722
  • Total Submissions: 96534
  • Difficulty: Easy

 

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

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

 

思路:字符串反转,交换s[i]和s[s.size()-1-i]

 

 

代码:

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

 

posted @ 2016-07-22 06:23  Deribs4  阅读(150)  评论(0编辑  收藏  举报