剑指 Offer 05. 替换空格
文章目录
- 剑指 Offer 05. 替换空格
- 一.题目描述
- 二.代码(快慢指针-中间指针和末尾指针)
一.题目描述
二.代码(快慢指针-中间指针和末尾指针)
class Solution {public:string replaceSpace(string s) {char ch = ' ';int count = std::count(s.begin(), s.end(), ch);int old_size = s.size();int new_size = s.size() + count * 2;s.resize(new_size);for (int i = old_size - 1, int j = new_size - 1; i < j; i--, j--) {if (s[i] != ' ') {s[j] = s[i];} else {s[j] = '0';s[j - 1] = '2';s[j - 2] = '%';j -= 2;}}return s;}
};