mirror of
https://github.com/ShusenTang/LeetCode.git
synced 2024-09-02 14:20:01 +00:00
19 lines
459 B
Markdown
19 lines
459 B
Markdown
![]() |
# [344. Reverse String](https://leetcode.com/problems/reverse-string/description/)
|
|||
|
# 思路
|
|||
|
翻转字符串。
|
|||
|
注意:交换元素时,最好用标准库里的swap,快一些。
|
|||
|
# C++
|
|||
|
```
|
|||
|
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;
|
|||
|
|
|||
|
}
|
|||
|
};
|
|||
|
```
|