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 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】的更多相关文章
- 156. Merge Intervals【LintCode by java】
Description Given a collection of intervals, merge all overlapping intervals. Example Given interval ...
- 158. Valid Anagram【LintCode by java】
Description Write a method anagram(s,t) to decide if two strings are anagrams or not. Clarification ...
- 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 ...
- 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 ...
- 173. Insertion Sort List【LintCode by java】
Description Sort a linked list using insertion sort. Example Given 1->3->2->0->null, ret ...
- 172. Remove Element【LintCode by java】
Description Given an array and a value, remove all occurrences of that value in place and return the ...
- 30. Insert Interval【LintCode by java】
Description Given a non-overlapping interval list which is sorted by start point. Insert a new inter ...
- 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 ...
- 211. String Permutation【LintCode by java】
Description Given two strings, write a method to decide if one is a permutation of the other. Exampl ...
随机推荐
- Go语言学习笔记(四)结构体struct & 接口Interface & 反射reflect
加 Golang学习 QQ群共同学习进步成家立业工作 ^-^ 群号:96933959 结构体struct struct 用来自定义复杂数据结构,可以包含多个字段(属性),可以嵌套: go中的struc ...
- windows最常用的快捷键(windows10 )
windows最常用的快捷键(windows10 ) [单指点击] 单击/双击,相当于鼠标左键. [单指滑动] 控制光标移动. [单指拖动] 相当于按下鼠标左键移动鼠标. [双指点击] 菜单键,相当于 ...
- DevExpress05、TileControl、AlertControl
TileControl控件 该控件是根据Windows 8的用户界面设计的,可以轻松地把各个控制块集成到窗体上. 1. IndertBetweenGroups属性 控制两个Group之间的间距: ...
- 第二次SDN上机作业
SDN第二次作业 1.安装floodlight fatter树在floodlight上的连接显示 2.生成拓扑并连接控制器floodlight,利用控制器floodlight查看图形拓扑 floodl ...
- Java 回调函数例子
首先定义一个类Caller,按照上面的定义就是程序员A写的程序a,这个类里面保存一个接口引用. public class Caller { private MyCallInterface callIn ...
- 2.2.3 TableLayout(表格布局)
3.如何确定行数与列数 ①如果我们直接往TableLayout中添加组件的话,那么这个组件将占满一行!!! ②如果我们想一行上有多个组件的话,就要添加一个TableRow的容器,把组件都丢到里面! ③ ...
- ethereumjs/ethereumjs-wallet
Utilities for handling Ethereum keys ethereumjs-wallet A lightweight wallet implementation. At the m ...
- resnet模型详细结构
resnet有5个stage,每个stage缩小一倍(即stride2).第1个stage是7*7个卷积大的缩小1倍,第2个stage是通过max-pooling缩小1倍,后面3个stage都是在各自 ...
- python+jenkins 构建节点环境编译器配置问题
python 编译器默认添加环境变量路径
- HDU 2767 Proving Equivalences(至少增加多少条边使得有向图变成强连通图)
Proving Equivalences Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...