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. wc 统计文件的行数,字数,字符

    格式:wc 参数 文件  默认统计文件的行数,字数,字符. -l   统计有多少行数 -c   统计有多少个字节 -m  统计有多少个字符 -w 统计有多少个字数

  2. Will Georgia Tech's $7K online M.S. in computer science program make the grade?

    https://newatlas.com/georgia-tech--graduate-computer-science-degree-mooc/28763/ Georgia Tech to offe ...

  3. Qt: QSqlRecord字段值为null时注意事项

    QSqlRecord在对应字段值为null时,QSqlRecord::value返回的QVariant是有效但为null(相当于使用QVariant(Type type)构造的),所以此时做对应类型的 ...

  4. Oracle Spatial中SDO_Geometry说明

    Oracle Spatial中SDO_Geometry说明 在ArcGIS中通过SDE存储空间数据到Oracle中有多种存储方式,分别有:二进制Long Raw .ESRI的ST_Geometry以及 ...

  5. html5式程序员表白

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/whqet/article/details/26394493 前端开发whqet,csdn,王海庆,w ...

  6. BZOJ2580:[USACO]Video Game(AC自动机,DP)

    Description Bessie is playing a video game! In the game, the three letters 'A', 'B', and 'C' are the ...

  7. linux 的常用命令---------第十阶段

    虚拟机三种网络模式 相同模式下的各个虚拟机之间都可以通信----两台虚拟机若都是 nat模式 或 桥接模式 或 仅主机模式,则这两台虚拟机之间是可以通信的. 桥接模式: (配置桥接模式的虚拟机可作为独 ...

  8. PAT B1030 完美数列 (25 分)

    给定一个正整数数列,和正整数 p,设这个数列中的最大值是 M,最小值是 m,如果 M≤mp,则称这个数列是完美数列. 现在给定参数 p 和一些正整数,请你从中选择尽可能多的数构成一个完美数列. 输入格 ...

  9. Python2.7-gzip

    gzip模块,提供了简单的压缩和解压缩文件的接口,和 GNU 程序的 gzip 和 gunzip 类似,数据压缩是通过 zlib 模块实现的 1.模块的类: gzip.GzipFile([filena ...

  10. jsp二(指令)

    一.jsp动作标签: 1)<jsp:forward> 请求转发 相当于之前的request.getRequestDispatcher(..).forward(..); <!--jsp ...