Skip to content

Commit 9a539e3

Browse files
committed
Create reverse-string.cpp
1 parent ef2b5c7 commit 9a539e3

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

C++/reverse-string.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
string reverseString(string s) {
7+
for (int i = 0, j = s.length() - 1; i < j; ++i, --j) {
8+
swap(s[i], s[j]);
9+
}
10+
return s;
11+
}
12+
};
13+
14+
// Time: O(n)
15+
// Space: O(1)
16+
class Solution2 {
17+
public:
18+
string reverseString(string s) {
19+
reverse(s.begin(), s.end());
20+
return s;
21+
}
22+
};

0 commit comments

Comments
 (0)