Write a function that reverses a string. The input string is given as an array of characters
char[]
.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
You may assume all the characters consist of printable ascii characters.
寫一個函式能夠反轉一個字串。輸入字串會使用字元陣列
char[]
。
不要配置任何額外的空間在其他陣列上,你應該在額外記憶體O(1)的原地演算法條件下,改變輸入字串完成它。
你可以假設所有字串都包含在可顯示字元中。
char[]
,但實際上是vector<char>
,應該是有改過題目的型別但忘了改敘述。
vector
速度較快。i
個和第n - i
個交換即可。LeetCode
C++