[Leetcode 344]Reverse String
Description:
Write a function that reverses a string. The input string is given as an array of characters s
.
Example 1:
Input: s = ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]
Example 2:
Input: s = ["H","a","n","n","a","h"]
Output: ["h","a","n","n","a","H"]
Constraints:
1 <= s.length <= 105
s[i]
is a printable ascii character.
C code solution:
void reverseString(char* s, int sSize){
for(int i=0; i<sSize/2; i++){
int tmp = s[i];
s[i] = s[sSize-1-i];
s[sSize-1-i] = tmp;
}
}
Explanation:
前後交換。第一個跟倒數第一個,第二個跟倒數第二個,以此類推直到中間。