Space Replacement

Write a method to replace all spaces in a string with %20. The string is given in a characters array, you can assume it has enough space for replacement and you are given the true length of the string.

You code should also return the new length of the string after replacement.

Notice

If you are using Java or Python,please use characters array instead of string.

Example

Given "Mr John Smith", length = 13.

The string after replacement should be "Mr%20John%20Smith", you need to change the string in-place and return the new length 17.

分析:

先过一遍,找出string里的空格个数,这样就可以确定新string的总长度,我们可以从length -1 那个地方倒着插入字符。

 1 public class Solution {
 2     /**
 3      * @param string: An array of Char
 4      * @param length: The true length of the string
 5      * @return: The true length of new string
 6      */
 7     public int replaceBlank(char[] str, int length) {
 8         if (str == null || str.length == 0) return 0;
 9
10         int count = 0;
11         for (int i = 0; i < str.length; i++) {
12             if (str[i] == ‘ ‘) count++;
13         }
14
15         int index = length + 2 * count - 1;
16
17         for (int i = length -1; i >= 0; i--) {
18             if (str[i] == ‘ ‘) {
19                 str[index--] = ‘0‘;
20                 str[index--] = ‘2‘;
21                 str[index--] = ‘%‘;
22             } else {
23                 str[index--] = str[i];
24             }
25         }
26
27
28         return length + 2 * count;
29
30     }
31 }
时间: 2024-08-18 03:23:44

Space Replacement的相关文章

212. Space Replacement【LintCode by java】

Description Write a method to replace all spaces in a string with %20. The string is given in a characters array, you can assume it has enough space for replacement and you are given the true length of the string. You code should also return the new

lintcode 容易题:Space Replacement 空格替换

题目: 空格替换 设计一种方法,将一个字符串中的所有空格替换成 %20 .你可以假设该字符串有足够的空间来加入新的字符,且你得到的是“真实的”字符长度. 样例 对于字符串"Mr John Smith", 长度为 13 替换空格之后的结果为"Mr%20John%20Smith" 注意 如果使用 Java 或 Python, 程序中请用字符数组表示字符串. 解题: 表示做到的通过率最低的一道题.... 用java的字符串链接起来,然后toCharArray,然后出去接个

LintCode:剑指Offer

第1章: 9.Fizz Buzz :http://www.lintcode.com/zh-cn/problem/fizz-buzz/ 解法1:(%  String.valueOf) (1.rst;  2.for(1,n),15,5,3,else;  3.return) 1 public class Solution { 2 public List<String> fizzBuzz(int n) { 3 List<String> rst = new ArrayList<>

jQuery表单 Ajax向PHP服务端发送文件请求并返回数据

ImageAjaxUpLoad.htm <!DOCTYPE html> <head> <meta charset='utf-8'> <title></title> <script src="jquery-1.6.1.min.js" type="text/javascript"></script> <script src="jquery.form.js" ty

JQuery:$(...).ajaxSubmit is not a function

用jquery写表单回调的时候报的bug 正如stackover上说的 I'm guessing you don't have a jquery form plugin included. ajaxSubmit isn't a core jquery function, I believe. 意思就是说表单提交是单独的一个插件,没集成在jquery-版本号.min.js这个文件里,需要另外引入jquery-form.js,文件名自取.现在找到的官方最新的form插件代码: /*! * jQuer

Coding Interviews 2 Array

Data Structure is the most important aspect in interviews. Most questions are on array, string, linked list, tree, stack, queue. Array and string are two basic data structure. They are both continuous memory. Linked list and tree have high frequency

jquery 通过ajax 提交表单

1.需要引入以下两个js文件 <script src="Easyui/jquery-1.7.2.min.js"></script>    <script src="JS/jquery.form.js"></script> 说明:jquery-1.7.2.min.js文件可以从网上下载,当然jquery.form.js文件也可以方便的从网上下载,这里给出代码 jquery.form.js代码如下: /*! * jQuer

jquery文件上传版 插件

/*! * jQuery Form Plugin * version: 4.2.2 * Requires jQuery v1.7.2 or later * Project repository: https://github.com/jquery-form/form * Copyright 2017 Kevin Morris * Copyright 2006 M. Alsup * Dual licensed under the LGPL-2.1+ or MIT licenses * https:

Python爬虫|深入请求(四)常见的反爬机制以及应对方法

作者:David Qian 链接:https://zhuanlan.zhihu.com/p/21558661 来源:知乎 著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 大家好!我是厦门大学王亚南经济研究院的大一学生,今天将由我来为大家介绍一下常见的反爬机制以及应对方法. 注:非商业转载注明作者即可,商业转载请联系作者授权并支付稿费.本人已授权"维权骑士"网站(http://rightknights.com)对我在知乎发布文章的版权侵权行为进行追究与维权. ---