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 ...
随机推荐
- Linux 下Shell的学习3-service编程
1. vim /etc/init.d/nginx 2. chmod 755 /etc/init.d/nginx 3. service nginx status #!/bin/bash # nginx ...
- <button>与<input type="button">
在做form表单,点击按钮随机生成两串密钥的时候 1.用第一种按钮的时候,会出现刷新form表单的现象.会把创建密钥前面的输入框中的字消失.虽然能生成密钥1和密钥2,但是会闪一下,随即消失.几个输入框 ...
- MySQL面试题36道
MySQL数据库是在免费的数据库中最受欢迎的一款,尤其是在一些小型项目以及项目资金有限的情况下,选择MySQL来作为数据存储的工具,那些不差钱并且数据吞吐量非常大的互联网公司一般都是会用付费的Orac ...
- js将时间戳转换成日期格式-陈远波
var timestamp =1539598555000;//时间戳 //时间戳转换成time格式function timestampToTime(timestamp) { var date = ne ...
- 解决Windows Server2008 R2中IE开网页时弹出阻止框
使用Windows Server2008,用IE打开网站时会弹出“Internet Explorer增强安全配置正在阻止来自下列网站的此应用程序中的内容”的对话框.如下图所示: 2011-10-14_ ...
- Android Environment 获取各种路径的方法
<pre name="code" class="java">package com.deepoon.beyond.environment; impo ...
- 第三方git pull免密码更新
方法一: git pull http://账号:密码@服务器地址/xxx/xxx.git master:master 方法二: 或者使用ssh免密码,生成的pub公钥内容拷贝的auth文件里面,同时添 ...
- P2962 [USACO09NOV]灯Lights
贝希和她的闺密们在她们的牛棚中玩游戏.但是天不从人愿,突然,牛棚的电源跳闸了,所有的灯都被关闭了.贝希是一个很胆小的女生,在伸手不见拇指的无尽的黑暗中,她感到惊恐,痛苦与绝望.她希望您能够帮帮她,把所 ...
- linux iostat 性能指标说明(转)
iostat属于sysstat软件包.可以用yum install sysstat 直接安装. 备注: 如果%iowait的值过高,表示硬盘存在I/O瓶颈, %idle值高,表示CPU较空闲, 如果% ...
- 20155217《网络对抗》Exp01 PC平台逆向破解(5)M
20155217<网络对抗>Exp01 PC平台逆向破解(5)M 实验要求 掌握NOP,JNE,JE,JMP,CMP汇编指令的机器码 掌握反汇编与十六进制编程器 能正确修改机器指令改变程序 ...