首页 > 学院 > 开发设计 > 正文

344. Reverse String

2019-11-06 06:37:11
字体:
来源:转载
供稿:网友

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

Example: Given s = “hello”, return “olleh”.

Subscribe to see which companies asked this question.

Show Tags Show Similar PRoblems 解法1:

class Solution {public: string reverseString(string s) { int left = 0, right = s.size()-1; while (left<right) { swap(s[left++] , s[right--]);//运用交换函数,逐一交换位置 } return s; }};

解法2:

class Solution {public: string reverseString(string s) { int left = 0, right = s.size() - 1; while (left < right) {//与上面的一个道理,只是是自己写的swap函数 char t = s[left]; s[left++] = s[right]; s[right--] = t; } return s; }};
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表