LeetCode/solutions/344. Reverse String.md

19 lines
462 B
Markdown
Raw Normal View History

2018-10-04 15:27:59 +00:00
# [344. Reverse String](https://leetcode.com/problems/reverse-string/description/)
# 思路
翻转字符串。
注意交换元素时最好用标准库里的swap快一些。
# C++
2019-09-13 15:08:41 +00:00
```C++
2018-10-04 15:27:59 +00:00
class Solution {
public:
string reverseString(string s) {
int low = 0, high = s.size() - 1;
while(low < high){
swap(s[low++], s[high--]); // 库里的swap比自己写的要快一些
}
return s;
}
};
```