lintcode 空格替换
来源:互联网 发布:mac 路由器ip 编辑:程序博客网 时间:2024/06/08 10:13
设计一种方法,将一个字符串中的所有空格替换成 %20 。你可以假设该字符串有足够的空间来加入新的字符,且你得到的是“真实的”字符长度。
你的程序还需要返回被替换后的字符串的长度。
对于字符串”Mr John Smith”, 长度为 13
替换空格之后的结果为”Mr%20John%20Smith”
class Solution {public: /** * @param string: An array of Char * @param length: The true length of the string * @return: The true length of new string */ int replaceBlank(char *string, int length) { // Write your code here if(string == nullptr || length <= 0){ return 0; } int i = 0; int numOfBlank = 0, OriginalLength = 0; while(*(string+i)!= '\0'){ ++ OriginalLength; if(*(string+i) == ' '){ ++ numOfBlank; } ++i; } int newlength = OriginalLength + numOfBlank*2; int ptr1 = OriginalLength,ptr2 = newlength; while(ptr1 >= 0 && ptr1 <= ptr2 ){ while(*(string+ptr1) != ' '){ *(string+ptr1) = *(string+ptr2); ptr1--; ptr2--; } if(string[ptr1] == ' '){ string[ptr2--] = '0'; string[ptr2--] = '2'; string[ptr2--] = '%'; } ptr1 --; } }};
同样的方法用python写并没有成功。
0 0
- Lintcode空格替换
- lintcode-空格替换-212
- 空格替换(LintCode)
- lintcode 空格替换
- LintCode:空格替换
- lintcode ----空格替换
- LintCode 空格替换
- 【LintCode】 空格替换
- lintcode,空格替换
- Lintcode 空格替换
- lintcode--空格替换
- LintCode 212 空格替换
- [LintCode]212.空格替换
- LintCode:空格替换
- 空格替换-LintCode
- LintCode-剑指Offer-空格替换
- 空格替换
- 替换空格
- ireport 中文乱码无法显示
- perl 模块下载安装
- 区域生长和区域分离与合并的图像分割方法
- 【BZOJ1009】[HNOI2008]GT考试【KMP】
- pop,push,shift,unshift
- lintcode 空格替换
- STLpair
- python之import机制详解
- 实习半个月的感受
- chomp 跟 chop
- 自定义Android标题栏修改TitleBar的布局
- 实验室中搭建Spark集群和PyCUDA开发环境
- perl 高级操作符 <=>
- 反向代理的理解