LintCode:空格替换
来源:互联网 发布:mysql修改参数为自增 编辑:程序博客网 时间:2024/06/07 22:24
一.题目描述
设计一种方法,将一个字符串中的所有空格替换成 %20
。你可以假设该字符串有足够的空间来加入新的字符,且你得到的是“真实的”字符长度。
你的程序还需要返回被替换后的字符串的长度。
样例对于字符串"Mr John Smith"
, 长度为 13
替换空格之后,参数中的字符串需要变为"Mr%20John%20Smith"
,并且把新长度 17
作为结果返回。
二.解题思路
先算出总共有多少个空格,算出总的字符串长度,然后从尾部开始填充(从头部开始的话会覆盖掉原有的)。
public class Solution { /* * @param string: An array of Char * @param length: The true length of the string * @return: The true length of new string */ public int replaceBlank(char[] string, int length) {// write your code hereif (string == null || length <= 0)return 0;int count = 0;for (int i = 0; i < length; i++) {if (string[i] == ' ')count++;}int j = 1;int newLen = length + count * 2;string[newLen] = 0;for (int i = length - 1; i >= 0; i--) {if (string[i] != ' ') {string[newLen - j] = string[i];j++;} else {string[newLen - j] = '0';j++;string[newLen - j] = '2';j++;string[newLen - j] = '%';j++;}}return newLen;}}
阅读全文
0 0
- Lintcode空格替换
- lintcode-空格替换-212
- 空格替换(LintCode)
- lintcode 空格替换
- LintCode:空格替换
- lintcode ----空格替换
- LintCode 空格替换
- 【LintCode】 空格替换
- lintcode,空格替换
- Lintcode 空格替换
- lintcode--空格替换
- LintCode 212 空格替换
- [LintCode]212.空格替换
- LintCode:空格替换
- 空格替换-LintCode
- LintCode-剑指Offer-空格替换
- 空格替换
- 替换空格
- 用I/O完成端口+socket编写客户端demo实现Http下载
- JQuery简述
- 【hpu oj 1015 985的方格难题 [DP、数学] 】
- 合并两个顺序表,并删除重复元素
- SQL中的case when then else end详细用法
- LintCode:空格替换
- DRY原则
- 用boost::lexical_cast用法解析
- python操作表格、txt文件、字典进行参数化(数据驱动)
- 公众号SEO:如何做好公众号和文章的搜索优化?
- vmware workstation 12 pro 安装Mac OS 10.12.4系统
- 查找单链表中的某几个相同的数,并把它们组成新的链表,原链表不变
- leetcode 20. Valid Parentheses
- Java线程池