[LintCode] 空格替换
2015-06-29 23:59
218 查看
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 (!length) return 0; int spaces = 0; for (int i = 0; string[i]; i++) if (string[i] == ' ') spaces++; if (!spaces) return length; int newlen = length + spaces * 2; int pn = newlen - 1, p = length - 1; while (p >= 0 && pn >= 0) { if (string[p] == ' ') { string[pn--] = '0'; string[pn--] = '2'; string[pn--] = '%'; p--; } else string[pn--] = string[p--]; } return newlen; } };
相关文章推荐
- 常用排序算法之快速排序算法
- JavaMail发送邮件(实例)
- C++之踏入多态1
- LeetCode_Linked List_Swap Nodes in Pairs
- CSS和文档
- WPF数据绑定
- php首页定向到内页代码
- JavaMail接、收邮件
- MySQL5.6 GTID Replication
- Lightoj 1071 - Baker Vai (双线程DP)
- Android之Handler与AsyncTask的区别
- Android Http请求方法汇总
- 2015年5月信息系统项目管理师综合知识真题详解 21-30题
- YTUOJ-世界杯小组赛
- css position用法
- java实现堆排序
- 其他学习
- 分享一些前端chm文档
- BZOJ 2152 聪聪可可 (树上点分治)
- NSScanner类的基本用法