题目: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 };