?LeetCode刷題實戰(zhàn)344:反轉(zhuǎn)字符串
Write a function that reverses a string. The input string is given as an array of characters s.
示例
示例 1:
輸入:["h","e","l","l","o"]
輸出:["o","l","l","e","h"]
示例 2:
輸入:["H","a","n","n","a","h"]
輸出:["h","a","n","n","a","H"]
解題
class Solution {
public:
void reverseString(vector<char>& s) {
//不加這一句也能通過,加上可以避免一些問題。
if(s.size()==0) return ;
int left = 0, right = s.size() - 1;
while (left < right) {
swap(s[left++], s[right--]);
}
}
};
評論
圖片
表情
