牛客网-《剑指offer》-替换空格
题目:http://www.nowcoder.com/practice/4060ac7e3e404ad1a894ef3e17650423
C++
1 class Solution { 2 public: 3 void replaceSpace(char *str,int len) { 4 int cnt = 0; 5 for (int i = 0; i < len; i++) { 6 if (str[i] == ' ') cnt++; 7 } 8 int idx = len + cnt * 2 - 1; 9 for (int i = len - 1; i >= 0; i--) { 10 if (str[i] == ' ') { 11 str[idx--] = '0'; 12 str[idx--] = '2'; 13 str[idx--] = '%'; 14 } else { 15 str[idx--] = str[i]; 16 } 17 } 18 } 19 };
找我内推: 字节跳动各种岗位
作者:
ZH奶酪(张贺)
邮箱:
cheesezh@qq.com
出处:
http://www.cnblogs.com/CheeseZH/
*
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。