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 length of the string after replacement.

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.

Challenge

Do it in-place.

解题:给一个字符数组,原地将空格换成  %20 。因为是原地转换,不能申请额外的空间,那么只能一步一步往后移动了。因为是把空格转换成“%20”,空格原本占用一个单位,现在需要占用三个单位,只要把原来空格的后面所有的数都向后移动两格即可。在移动的过程中,length也随之变化。代码如下:

 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 here
for(int i = 0; i < length; ){
//如果发现空格
if(string[i] == ' '){
for(int j = length-1; j >= i+1; j--){
string[j+2] = string[j];
}
string[i++] = '%';
string[i++] = '2';
string[i++] = '0';
length = length+2;
}else{
i++;
}
}
return length;
}
}

212. Space Replacement【LintCode by java】的更多相关文章

  1. 156. Merge Intervals【LintCode by java】

    Description Given a collection of intervals, merge all overlapping intervals. Example Given interval ...

  2. 158. Valid Anagram【LintCode by java】

    Description Write a method anagram(s,t) to decide if two strings are anagrams or not. Clarification ...

  3. 165. Merge Two Sorted Lists【LintCode by java】

    Description Merge two sorted (ascending) linked lists and return it as a new sorted list. The new so ...

  4. 177. Convert Sorted Array to Binary Search Tree With Minimal Height【LintCode by java】

    Description Given a sorted (increasing order) array, Convert it to create a binary tree with minimal ...

  5. 173. Insertion Sort List【LintCode by java】

    Description Sort a linked list using insertion sort. Example Given 1->3->2->0->null, ret ...

  6. 172. Remove Element【LintCode by java】

    Description Given an array and a value, remove all occurrences of that value in place and return the ...

  7. 30. Insert Interval【LintCode by java】

    Description Given a non-overlapping interval list which is sorted by start point. Insert a new inter ...

  8. 155. Minimum Depth of Binary Tree【LintCode by java】

    Description Given a binary tree, find its minimum depth. The minimum depth is the number of nodes al ...

  9. 211. String Permutation【LintCode by java】

    Description Given two strings, write a method to decide if one is a permutation of the other. Exampl ...

随机推荐

  1. 【排序算法】选择排序(Selection sort)

    0. 说明 选择排序(Selection sort)是一种简单直观的排序算法. 它的工作原理如下. 首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余未排序元素中继续寻找最 ...

  2. jquery实现根据所选时间生成页面元素

    最近做项目,碰见这样的一个需求 根据所选的时间动态生成值班安排,日期格式需要带星期,如: 代码如下: 1.首先放两个文本框,时间插件用的是My97DataPicker,再放一个table,简单设置一下 ...

  3. T4学习- 4、Kalman Studio-T4代码生成器

    开源代码生成器-Kalman Studio https://github.com/loamen/Kalman.Studio 软件主要功能如下: 1.基于T4的代码生成工具,根据数据库元数据信息生成代码 ...

  4. JAVA基础|从Class.forName初始化数据库到SPI破坏双亲委托机制

    代码托管在:https://github.com/fabe2ry/classloaderDemo 初始化数据库 如果你写过操作数据库的程序的话,可能会注意,有的代码会在程序的开头,有Class.for ...

  5. 【Alpha 冲刺】 6/12

    今日任务总结 人员 今日原定任务 完成情况 遇到问题 贡献值 胡武成 建立数据库 未完成 表结构文档已设计好,服务器mysql刚配置完成,但是,SpringMVC框架还没有熟络,不清楚如何使用该框架去 ...

  6. 洛谷P1208

    #include <iostream>#include <algorithm>#include <cstdio>using namespace std; struc ...

  7. JS强制刷新页面、清除缓存刷新

    清理网站缓存的几种方法 meta方法 <meta http-equiv="pragma" content="no-cache"> <meta ...

  8. shiro实战系列(四)之配置

    Shiro之配置 Shiro 被设计成能够在任何环境下工作,从最简单的命令行应用程序到最大的的企业群集应用.由于环境的多样性,使得许多配置机制适用于它的配置. 一. 许多配置选项 Shiro的Secu ...

  9. django表格form无法保存评论排查步骤

    初学django项目,在网上找了个blog教程,还是很不错的,这里感谢一下博主https://www.zmrenwu.com/post/2/ 这个项目适合django初学者,是一个完整的blog项目 ...

  10. Pyspider抓取静态页面

    近期,我想爬一批新闻资讯的内容.新闻类型的网址很多,我想看看有没有一个网页上能包罗尽可能多的新闻网站呢,于是就发现了下面这个网页 http://news.hao123.com/wangzhi 这个页面 ...