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

Leetcode 344. Reverse String

时间:2016-07-22 07:43:08      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

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

 

Leetcode 344. Reverse String

标签:

原文地址:http://www.cnblogs.com/Deribs4/p/5693772.html

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