---
title: 'LeetCode 344. Reverse String'
disqus: hackmd
---
# LeetCode 344. Reverse String
## Description
Write a function that reverses a string. The input string is given as an array of characters s.
You must do this by modifying the input array in-place with O(1) extra memory.
## Example
Input: s = ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]
## Constraints
1 <= s.length <= 10^5^
s[i] is a printable ascii character.
## Answer
此題為普通的字串反轉,抓頭尾交換,然後loop一增一減,當頭和尾交錯即可得解。
```Cin=
//2021_11_16
void reverseString(char* s, int sSize) {
char *head = s, *tail = s + sSize - 1, temp = '\0';
while(head < tail){
temp = *head;
*head = *tail;
*tail = temp;
head++;
tail--;
}
}
```
## Link
https://leetcode.com/problems/reverse-string/
###### tags: `Leetcode`