lintcode : 空格替换
题目:
设计一种方法,将一个字符串中的所有空格替换成 %20 。你可以假设该字符串有足够的空间来加入新的字符,且你得到的是“真实的”字符长度。
对于字符串"Mr John Smith", 长度为 13
替换空格之后的结果为"Mr%20John%20Smith"
如果使用 Java 或 Python, 程序中请用字符数组表示字符串。
解题:
表示做到的通过率最低的一道题。。。。
用java的字符串链接起来,然后toCharArray,然后出去接个和之前的一样,原来%20 表示的是一个空格。。。。。。。参考程序,直接在原char字符数组上操作,先求新的字符串数组的长度,再从后插入%20,这样就解决了,对于从后插入求解的碰到过,题目都会给这样的提示:假设该字符串有足够的空间来加入新的字符,要根据题目找答案,找思路。
Java程序:
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
int reallen = length;
for(int i = 0;i<length;i++){
if(string[i] == ' ')
reallen += 2;
}
int index = reallen;
for(int i = length - 1;i>= 0 ;i-- ){
if(string[i] == ' '){
string[--index] = '0';
string[--index] = '2';
string[--index] = '%';
}else{
string[--index] = string[i];
}
}
return reallen;
}
}
总耗时: 1137 ms
Python程序:
class Solution:
# @param {char[]} string: An array of Char
# @param {int} length: The true length of the string
# @return {int} The true length of new string
def replaceBlank(self, string, length):
# Write your code here
if string==None:
return None
reallen = length
for si in string:
if si==' ':
reallen += 2
index = reallen
for i in range(length-1,-1,-1):
if string[i] == ' ':
index -= 1
string[ index ] = ''
index -= 1
string[ index ] = ''
index -= 1
string[ index ] = '%'
else:
index -= 1
string[ index ] = string[i]
return reallen
总耗时: 393 ms
lintcode : 空格替换的更多相关文章
- C# 中将多个空格替换成一个空格
2013-04-17 15:36 C# 中如何将多个空格替换成一个空格? 1 myString = Regex.Replace(myString, @"\s+", " & ...
- 【C语言】字符串替换空格:实现一个函数,把字符串里的空格替换成“%20”
//字符串替换空格:实现一个函数,把字符串里的空格替换成"%20" #include <stdio.h> #include <assert.h> void ...
- 剑指offer 1,输入一个字符串,将字符串的空格替换成%20
剑指offer 1,输入一个字符串,将字符串的空格替换成%20 function replaceSpace(str){ return str.replace(/\s/g,"% ...
- 将一个字符串中的空格替换成“%20”(C、Python)
将一个字符串中的空格替换成“%20” C语言: /* ----------------------------------- 通过函数调用,传地址来操作字符串 1.先计算出替换后的字符串的长度 2.从 ...
- shell中空格的使用;空格替换;通配符
测试: test $? -eq && echo "yes" || echo "no" 通配符: 通配符 ()*:0个或多个连续的字符 ()?:任 ...
- linux删除指定行&删除行首空格&替换字符
打印并删除2~1000行 nl /etc/passwd | sed '2,1000d' |more 删除行首空格 sed -i 's/^[][ ]*//g' file 替换分隔符 说明:文件中数据是由 ...
- c# 字符串去掉两端空格,并且将字符串中多个空格替换成一个空格
字符串去掉两端空格,并且将字符串中多个空格替换成一个空格: 主要还是考察使用字符串的方法: trim(); 去掉字符串两端空格 split(); 切割 string.join(); 连接 class ...
- LintCode-212.空格替换
空格替换 设计一种方法,将一个字符串中的所有空格替换成 %20 .你可以假设该字符串有足够的空间来加入新的字符,且你得到的是"真实的"字符长度. 你的程序还需要返回被替换后的字符串 ...
- 请实现一个函数,把字符串中的每一个空格替换成“%20”,比如输入 “We are Happly。” 则输出“we%20are%20happy。”
请实现一个函数,把字符串中的每一个空格替换成"%20",比如输入 "We are Happly." 则输出"we%20are%20happy. &q ...
随机推荐
- Gulp vs Grunt 前端构建工具对比
Gulp vs Grunt 前端工程的构建工具对比 1. Grunt -> Gulp 早些年提到构建工具,难免会让人联想到历史比较悠久的Make,Ant,以及后来为了更方便的构建结构类似的Jav ...
- hadoop架构
HADOOP中可以分为两个大的模块,存储模块和计算模块.HDFS作为存储模块,JobTracker,TaskTracker构成计算模块. 1.HADOOP的文件是以HDFS格式存储的 HDFS ...
- curl 报错记录,mark
今天在做接口开发的时候,使用curl post ,请求返回数据为 null ,很纳闷,然后使用 curl_errno 打印出来的错误代码为 28 ,curl_error($ch) 打印出来的是Oper ...
- php中fopen函数用法详解(打开文件)
介绍下php中的fopen函数. 1.resource fopen(string $filename, string $mode [,bool $use_include_path [, resou ...
- ORACLE 11G 配置DG 报ORA-10458、ORA-01152、ORA-01110
操作系统: Oracle Linux Server release 5.7 数据库版本: Oracle Database 11g Enterprise Edition Release 11.2.0.3 ...
- I/O空间映射
转自:http://www.cnblogs.com/hydah/archive/2012/04/10/2232117.html 注:部分资料和图片来源于网络,本文在学习过程中对网络资源进行再整理. I ...
- Most People Aren’t.
Most people want to be fit, most people aren't. Most people want to build a successful business, mos ...
- 文字沟通工具使用SignalR,跨域例子源代码
其他网站已经有很多关于SignalR的介绍了.这里不多介绍. 安装:Install-Package Microsoft.AspNet.SignalR -Version 1.1.4 参考自:http:/ ...
- 想成为真正的代码gg,目标
转眼已而大二了,可是在这上了一个星期的课,感觉生活非常的茫然.当然这与我处在的环境有一定的关系. 处在这样的学校,想努力可是让我心凉的是没有一个老师肯真心带学生,, 学校办的各种事情都很坑,,我不怕自 ...
- Ioc 比较
public interface IC { } public class A { IC ic_; public A(IC ic) { ic_ = ic; } } public class B : IC ...