Space Replacement
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.
Notice
If you are using Java or Python,please use characters array instead of string.
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.
分析:
先过一遍,找出string里的空格个数,这样就可以确定新string的总长度,我们可以从length -1 那个地方倒着插入字符。
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[] str, int length) {
if (str == null || str.length == ) return ;
int count = ;
for (int i = ; i < str.length; i++) {
if (str[i] == ' ') count++;
}
int index = length + * count - ;
for (int i = length -; i >= ; i--) {
if (str[i] == ' ') {
str[index--] = '';
str[index--] = '';
str[index--] = '%';
} else {
str[index--] = str[i];
}
}
return length + * count;
}
}
Space Replacement的更多相关文章
- Lintcode212 Space Replacement solution 题解
[题目描述] Write a method to replace all spaces in a string with%20. The string is given in a characters ...
- 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 char ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- Java Algorithm Problems
Java Algorithm Problems 程序员的一天 从开始这个Github已经有将近两年时间, 很高兴这个repo可以帮到有需要的人. 我一直认为, 知识本身是无价的, 因此每逢闲暇, 我就 ...
- jQuery表单 Ajax向PHP服务端发送文件请求并返回数据
ImageAjaxUpLoad.htm <!DOCTYPE html> <head> <meta charset='utf-8'> <title>< ...
- 怎么利用jquery.form 提交form
说明:开发环境 vs2012 asp.net mvc c# 利用jQuery.form.js提交form 1.HTML前端代码 <%@ Page Language="C#" ...
- jquery 通过ajax 提交表单
1.需要引入以下两个js文件 <script src="Easyui/jquery-1.7.2.min.js"></script> <scrip ...
- jquery.form.min.js
/*! * jQuery Form Plugin * version: 3.51.0-2014.06.20 * Requires jQuery v1.5 or later * Copyright (c ...
- HDFS面试题
hadoop节点动态上线下线怎么操作? )节点上线操作: 当要新上线数据节点的时候,需要把数据节点的名字追加在 dfs.hosts 文件中 ()关闭新增节点的防火墙 ()在 NameNode 节点的 ...
随机推荐
- Day Four
站立式会议 站立式会议内容总结 442 今天:整合主页两个部分的逻辑代码,主页及其跳转基本完成 遇到的问题:无 明天:阅读图书界面逻辑部分完成 331 今天:学习java反射添加类数据到数据库 遇到问 ...
- 使用Jekyll在Github上搭建个人博客 - 环境搭建
本地安装Jekyll 首先安装Ruby及gem Ruby的安装 Ruby官网进行下载 从RubyInstaller下载ruby [新手推荐] 我采用的是RubyInstaller,无脑简单 勾选时我配 ...
- 软工实践练习一 git使用心得
使用git进行代码管理的心得 小组 1.结对的同学创建了小组,我属于被邀请的.附上图片一张. 2.已将代码库https://github.com/sefzu2015/AutoCS fork到了小组or ...
- 解决eclipse中mybatis的xml配置文件无代码提示问题
https://blog.csdn.net/IRainReally/article/details/81743506
- 老李的blog使用日记(3)
匆匆忙忙.碌碌无为,这是下一个作业,VS,多么神圣高大上,即使这样,有多少人喜欢你就有多少人烦你,依然逃不了被推销的命运,这抑或是它喜欢接受的,但是作为被迫接受者,能做的的也只有接受,而已. 既来之则 ...
- Centos7 安装netcat
1.下载 下载地址:https://sourceforge.net/projects/netcat/files/netcat/0.7.1/ 下载的是netcat-0.7.1.tar.gz版本 2.安装 ...
- html+css照片墙
html文件 <!DOCTYPE html><html lang="en"><head> <meta charset="UTF- ...
- 【刷题】LOJ 6002 「网络流 24 题」最小路径覆盖
题目描述 给定有向图 \(G = (V, E)\) .设 \(P\) 是 \(G\) 的一个简单路(顶点不相交)的集合.如果 \(V\) 中每个顶点恰好在 \(P\) 的一条路上,则称 \(P\) 是 ...
- 【bzoj3994】 SDOI2015—约数个数和
http://www.lydsy.com/JudgeOnline/problem.php?id=3994 (题目链接) 题意 多组询问,给出${n,m}$,求${\sum_{i=1}^n\sum_{j ...
- 使用Metasploit绕过UAC的多种方法
一.用户帐户控制(UAC)简介 在本文中,我们将简要介绍一下用户帐户控制,即UAC.我们还将研究它如何潜在地保护免受恶意软件的攻击并忽略UAC提示可能给系统带来的一些问题. 1.什么是用户帐户控制 ...