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 ...
随机推荐
- You have new mail in /var/spool/mail/root 解决烦琐提示的方法
今天写定时任务时,出现奇怪的提示,有的时候每敲一下回车,也出现奇怪的提示 You have new mail in /var/spool/mail/root 阿西吧........表示很烦...究竟是 ...
- idea 断点上面有x
背景:确定你的java代码没有问题,并且编译通过 问题:debug 启动项目的时候没有问题,idea打断点的时候左边红色断点上面有x 原因:java文件和class文件不一致, 解决方法:ant cl ...
- time模块案例演示
案例01: 2008年8月8日20:08:08 往后88,888,888秒是哪天?星期几? 日期->时间戳(浮点数)->可以做数学运算 演示: import time # 构造日期的元组, ...
- pig:group by之后的其它统计方法一
--测试Top N后的其它统计 A = LOAD '/TraceParser/blackcore/' USING PigStorage() as (lk_id:chararray,host:chara ...
- 移动web前端开发时注意事项
在智能手机横行的时代,作为一个web前端,不会编写移动web界面,的确是件悲催的事情.当公司准备做一个微信的微网站时,作为一个多年经验的web前端码农,我迷茫了,真心不知道从何下手. 接下来就是搜一堆 ...
- PyQt5---firstwindow
# -*- coding:utf-8 -*- ''' Created on Sep 13, 2018 @author: SaShuangYiBing ''' import sys from PyQt5 ...
- 利用 Settings Sync 同步vs code配置
vs code上有各种各样不同的插件,如果要在不同的电脑上使用 vs code 配置是件比较麻烦的事情,使用 Settings Sync 将 vs code 配置备份起来,当需要在其他电脑使用 vs ...
- docker swarm英文文档学习-10-使用Docker密钥管理敏感数据
Manage sensitive data with Docker secrets使用Docker secrets管理敏感数据 About secrets 对于Docker Swarm服务来说,sec ...
- Jenkins与Github集成
Jenkins目前是手动进行项目构建的,如何才能做到Github并持续集成呢? 配置前要求: 1.Jenkins已经安装Github插件 2.Jenkins服务器已经拥有一个公网IP地址 第一步:配置 ...
- UART, SPI, IIC的详解及三者的区别和联系
UART.SPI.IIC是经常用到的几个数据传输标准,下面分别总结一下: UART(Universal Asynchronous Receive Transmitter):也就是我们经常所说的串口,基 ...