面试题:替换空格
来源:互联网 发布:如何消除身体疲劳知乎 编辑:程序博客网 时间:2024/06/04 22:35
题目:替换字符串中的空格为$$$。要求时间复杂度为O(N)
例如:将”talk is cheap show me the code”替换。
基本思想:
栗子:we are family
参考代码:
void ReplaceBlank(char string[], int length){ //length为字符串的总容量 if (string == NULL || length <= 0) return; //字符串实际长度 int actuallen = 0; //空格个数 int blanknum = 0; int i = 0; while (string[i]!='\0') { if (string[i] == ' ') blanknum++; actuallen++; i++; } //替换后的总长度 int newlen = actuallen + blanknum * 2; if (newlen > length) return; int actualidx = actuallen; int newidx = newlen; while (actualidx>=0 && newidx>actualidx) { if (string[actualidx] == ' ') { string[newidx--] = '$'; string[newidx--] = '$'; string[newidx--] = '$'; } else { string[newidx--] = string[actualidx]; } actualidx --; } int j = 0; while (j<newlen) { cout << string[j] << " "; j++; }}
阅读全文
0 0
- 面试题:替换空格
- 面试题之替换空格
- 面试题4:替换空格
- 面试题 -- 字符串替换空格
- 面试题4 替换空格
- 面试题4:空格替换
- 面试题4-替换空格
- 面试题4 替换空格
- 面试题四:替换空格
- 面试题4:替换空格
- 面试题4 替换空格
- 面试题4:替换空格
- 面试题2:替换空格
- 面试题4:替换空格
- 面试题4:替换空格
- 面试题5:替换空格
- 面试题4:替换空格
- 面试题4-替换空格
- leetcode:path-sum
- Play 2.0 中文资料
- ssm中Invalid bound statement (not found)错误的处理(IDEA中)
- Codeforces Round #426 (Div. 2) C. The Meaningless Game
- 调试代码时免重复输入数据
- 面试题:替换空格
- Zookeeper client
- FileChannel 传输(zero-copy 的基础)
- word-break:break-all和word-wrap:break-word的区别
- C# windows服务没有RunInstallerAttribute.Yes的公共安装程序
- Spring Security 与 Oauth2 整合 步骤
- 传颜色生成图片
- ThinkPHP函数详解:U方法
- jQuery layer弹出层