Reverse String
Description
Given a string s, reverse it in place. Return the reversed string.
Example 1:
Input: s = "hello"
Output: "olleh"
Example 2:
Input: s = "Hannah"
Output: "hannaH"
Example 3:
Input: s = "a"
Output: "a"
Explanation: Single character remains the same when reversed.
Constraints
1 <= s.length <= 10^5s consists of printable ASCII characters
Signature
...Complexity
- Time:
O(n) - Space:
O(1)
Hints
- Pattern
- Use two pointers: one at start, one at end
- Approach
- Swap characters at left and right pointers, then move them toward center
- Complexity
- Swap elements in place to achieve O(1) extra space
Solutions
char *
return
=
= 0
= - 1
, = ,
+= 1
-= 1
return