[CareerCup] 1.4 Replace Spaces 替换空格
1.4 Write a method to replace all spaces in a string with '%20'. You may assume that the string has sufficient space at the end of the string to hold the additional characters, and that you are given the "true" length of the string. (Note: if implementing in Java, please use a character array so that you can perform this operation in place.)
这道题让我们将一个字符串里所有的空格都替换为'%20',而且说明最好用in place的方法,就是说不要建新的字符串。如果建个新的字符,那这题解法就显得略low,因为那样我们只需要遍历原字符串s,遇到字符直接加进来,遇到空格,直接加个'%20'就行了。这道题要使用高大上的in place的方法,那就是我们先便利一遍给定字符串s,统计空格的个数,然后再resize一下字符串,增大字符串s的容量,以便我们来替换。然后我们再从原来s的末尾位置向开头遍历,遇到空格,在末尾三个空添加'%20',然后标示新位置,遇到非空格字符,则添加该字符,然后再标示新位置即可。代码如下:
class Solution {
public:
void replaceSpaces(string &s) {
int count = , len = s.size(), newLen = ;
for (int i = ; i < len; ++i) {
if (s[i] == ' ') ++count;
}
newLen = len + * count;
s.resize(newLen);
for (int i = len - ; i >= ; --i) {
if (s[i] == ' ') {
s[newLen - ] = '';
s[newLen - ] = '';
s[newLen - ] = '%';
newLen -= ;
} else {
s[newLen - ] = s[i];
newLen -= ;
}
}
}
};
[CareerCup] 1.4 Replace Spaces 替换空格的更多相关文章
- 【C语言】字符串替换空格:实现一个函数,把字符串里的空格替换成“%20”
//字符串替换空格:实现一个函数,把字符串里的空格替换成"%20" #include <stdio.h> #include <assert.h> void ...
- 剑指offer编程题Java实现——替换空格
题目描述 请实现一个函数,将一个字符串中的空格替换成"%20".例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. package ...
- 替换空格[by Python]
题目: 请实现一个函数,将一个字符串中的空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 1.使用python自带的repla ...
- 剑指offer(2)替换空格
题目描述 请实现一个函数,将一个字符串中的空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 题目分析 我们如果要替换空格,两步 ...
- 剑指offer编程题Java实现——面试题4替换空格
题目描述 请实现一个函数,将一个字符串中的空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. package Solution; ...
- JS替换空格回车换行符
JS替换空格回车换行符 str=str.replace(/\r/g," ") str=str.replace(/\n/g,"<br />") 或 ...
- 《剑指offer》— JavaScript(2)替换空格
替换空格 题目描述 请实现一个函数,将一个字符串中的空格替换成"%20".例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 实现 ...
- 替换空格(C++和Python 实现)
(说明:本博客中的题目.题目详细说明及参考代码均摘自 “何海涛<剑指Offer:名企面试官精讲典型编程题>2012年”) 题目 请实现一个函数,把字符串中的每个空格替换为 "%2 ...
- sql server中字符串无法替换空格的问题
直接上代码: select case when 'workReport'=LTRIM(RTRIM(' workReport ')) then 'trim去空格成功' when 'workReport' ...
随机推荐
- ReSharper 8.XXX 注册机
今天给电脑重装系统,发现Rsharper已经更新到8.0.14.856了,于是下载新版本的,但像咱搞开发的,肯定不能用付费软件(关键是你也付不起啊,499$,499刀啊).于是在网上找相关的激活软件. ...
- 利用eclipse抽取 代码片段为方法
选取要被抽取成方法的代码片段,右键->Refactor--->Extract Method 填写方法名称 抽取后成了这个样子:
- centos性能监控系列三:监控工具atop详解
引言 Linux以其稳定性,越来越多地被用作服务器的操作系统(当然,有人会较真地说一句:Linux只是操作系统内核:).但使用了Linux作为底层的操作系统,是否我们就能保证我们的服务做到7*24地稳 ...
- .Net 三款工作流引擎比较:WWF、netBPM 和 ccflow
下面将对目前比较主流的三款工作流进行介绍和比较,然后通过三款流程引擎分别设计一个较典型的流程来给大家分别演示这三款创建流程的过程.这三款工作流程引擎分别是 Windows Workflow Found ...
- JavaScript Patterns 3.8 Error Objects
The error objects created by constructors(Error(), SyntaxError(), TypeError(), and others) have the ...
- MongoDB学习笔记——数据库操作
使用use数据库名称来创建数据库,如果该数据库已经存在则返回这个数据库 语句格式:use DATABASE_NAME >use mynewdb switched to db mynewdb 使用 ...
- Linked List Cycle
Given a linked list, determine if it has a cycle in it. /** * Definition for singly-linked list. * s ...
- gnuplot Python API
源文件 #!/usr/bin/env python from os import popen class gnuplot_leon: # Author : Leon Email: yangli0534 ...
- windows下如何安装jira
---恢复内容开始--- 准备工作: 1.安装jdk,详细不在介绍 2.建立jira帐号:https://id.atlassian.com/login?application=mac&cont ...
- 2014 Super Training #9 E Destroy --树的直径+树形DP
原题: ZOJ 3684 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3684 题意: 给你一棵树,树的根是树的中心(到其 ...