题目
给定一个字符数组,对数组进行倒序。只是用O(1)的额外内存空间。
Input: ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]
Input: ["H","a","n","n","a","h"]
Output: ["h","a","n","n","a","H"]
思路
直接倒序。
void reverseString(vector<char>& s) {
for (int i = 0; i < s.size() / 2; i++) {
char temp = s[s.size()-1-i];
s[s.size()-1-i] = s[i];
s[i] = temp;
}
}
总结
这个题是乱入的吧。
网友评论