【剑指Offer面试题】 九度OJ1510:替换空格
c/c++ 中的字符串以“\0”作为结尾符。这样每一个字符串都有一个额外字符的开销。
以下代码将造成内存越界。
char str[10];
strcpy(str, “0123456789”);
为了节省内存。c/c++ 会把常量字符串放到单独的一个内存区域。当几个指针赋予同样的常量字符串时,它们实际上会指向同样的内存地址。
题目链接地址: pid=1510">http://ac.jobdu.com/problem.php?pid=1510
替换空格
时间限制:1 秒内存限制:128 兆特殊判题:否提交:9648解决:2483
题目描写叙述:
请实现一个函数。将一个字符串中的空格替换成“%20”。比如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
输入:
每一个输入文件仅包括一组測试例子。
对于每组測试案例,输入一行代表要处理的字符串。
输出:
相应每一个測试案例,出经过处理后的字符串。
例子输入:
We Are Happy
例子输出:
We%20Are%20Happy
《剑指offer》中分析的非常具体:
1. 假设从前往后找空格。遇到空格时将后面的字符后移,这样后面的字符会多次移动。
2. 假设先计算出空格数目,从后往前,就能够实现一个字符仅仅移动一次。学会转换思路。
#include
#include
#include
using namespace std;
void ReplaceBlank(char *str)
{
if(str == NULL)
return;
int len = strlen(str); //碰到第一个字符串结束符'\0'为止。然后返回计数器值(长度不包括“\0”)
if(len == 0)
return;
int i,count = 0;
for(i=0;i=0&&indexOfnew_len>indexOflen)
{
if (str[indexOflen]==' ')
{
str[indexOfnew_len--]='0';
str[indexOfnew_len--]='2';
str[indexOfnew_len--]='%';
}
else
{
str[indexOfnew_len--]=str[indexOflen];
}
--indexOflen;
}
} int main()
{
static char str[10000002];
gets(str);
ReplaceBlank(str);
puts(str);
return 0;
}
/**************************************************************
Problem: 1510
Language: C++
Result: Accepted
Time:20 ms
Memory:11284 kb
****************************************************************/" data-snippet-id="ext.7ca15b8f8c8d30bf0e0c99007bdad244" data-snippet-saved="false" data-csrftoken="SV45t05l-bal0EA4_VBXU_a8ZNUyWv1nEsjk" data-codota-status="done">/*********************************
-----------------------------------
【剑指Offer面试题】替换空格
-----------------------------------
Author:牧之丶 Date:2015年
Email:bzhou84@163.com
**********************************/
#include <stdio.h>
#include <cstring>
#include <string>
#include <iostream>
using namespace std;
void ReplaceBlank(char *str)
{
if(str == NULL)
return;
int len = strlen(str); //碰到第一个字符串结束符'\0'为止,然后返回计数器值(长度不包括“\0”)
if(len == 0)
return;
int i,count = 0;
for(i=0;i<len;i++)
if(str[i] == ' ')
count++;
//没有空格,就直接返回str
if(count == 0)
return;
int new_len = 2*count+len;
int indexOflen=len;
int indexOfnew_len=new_len;
while(indexOflen>=0&&indexOfnew_len>indexOflen)
{
if (str[indexOflen]==' ')
{
str[indexOfnew_len--]='0';
str[indexOfnew_len--]='2';
str[indexOfnew_len--]='%';
}
else
{
str[indexOfnew_len--]=str[indexOflen];
}
--indexOflen;
}
} int main()
{
static char str[10000002];
gets(str);
ReplaceBlank(str);
puts(str);
return 0;
}
/**************************************************************
Problem: 1510
Language: C++
Result: Accepted
Time:20 ms
Memory:11284 kb
****************************************************************/
注意点:
- string转char *
#include
using namespace std; int main()
{
string str1="Hello";
char *str2=const_cast(str1.c_str());
cout#include <iostream>
#include <string>
using namespace std; int main()
{
string str1="Hello";
char *str2=const_cast<char*>(str1.c_str());
cout<<str2;
return 0;
}
- 假设完毕这道题,非常easy方法是遇到非空格就输出。遇到空格就输出“%20”。
#include
#include using namespace std; int main()
{
char str[10000002];
while(gets(str))
{
int i=0;
while(str[i]!='\0')
{
if(str[i]==' ') cout#include <cstdio>
#include <iostream>
#include <string> using namespace std; int main()
{
char str[10000002];
while(gets(str))
{
int i=0;
while(str[i]!='\0')
{
if(str[i]==' ') cout<<"%20";
else cout<<str[i];
i++;
}
cout<<endl;
}
return 0;
}
/**************************************************************
Problem: 1510
Language: C++
Result: Accepted
Time:20 ms
Memory:11212 kb
****************************************************************/
【剑指Offer面试题】 九度OJ1510:替换空格的更多相关文章
- 《剑指offer》第五题(替换空格)
// 替换空格 // 题目:请实现一个函数,把字符串中的每个空格替换成"%20".例如输入“We are happy.”, // 则输出“We%20are%20happy.”. # ...
- 《剑指offer》— JavaScript(2)替换空格
替换空格 题目描述 请实现一个函数,将一个字符串中的空格替换成"%20".例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 实现 ...
- 【剑指Offer面试题】 九度OJ1389:变态跳楼梯
转自:http://www.myexception.cn/program/1973966.html 时间限制:1 秒内存限制:32 兆特殊判题:否提交:2331解决:1332 题目描述: 一只青蛙一次 ...
- 【剑指Offer面试题】 九度OJ1518:反转链表
与其非常快写出一段漏洞百出的代码,倒不如细致分析再写出鲁棒的代码. 提前想好測试用例(输入非空等等)进行測试改动代码. 题目链接地址: http://ac.jobdu.com/problem.php? ...
- 【剑指Offer面试题】 九度OJ1368:二叉树中和为某一值的路径
题目链接地址: http://ac.jobdu.com/problem.php? pid=1368 题目1368:二叉树中和为某一值的路径 时间限制:1 秒内存限制:32 兆特殊判题:否提交:2252 ...
- 【剑指Offer面试题】 九度OJ1517:链表中倒数第k个结点
鲁棒性是指程序可以推断输入是否符合规范要求,并对不和要求的输入予以 合理的处理. 题目链接地址: http://ac.jobdu.com/problem.php?pid=1517 题目1517:链表中 ...
- 【剑指Offer面试题】 九度OJ1385:重建二叉树
题目链接地址: pid=1385">http://ac.jobdu.com/problem.php?pid=1385 题目1385:重建二叉树 时间限制:1 秒内存限制:32 兆特殊判 ...
- 【剑指Offer面试题】 九度OJ1371:最小的K个数
题目链接地址: http://ac.jobdu.com/problem.php?pid=1371 题目1371:最小的K个数 时间限制:1 秒内存限制:32 兆特殊判题:否提交:5938解决:1265 ...
- 【剑指Offer面试题】 九度OJ1516:调整数组顺序使奇数位于偶数前面
题目链接地址: http://ac.jobdu.com/problem.php?pid=1516 题目1516:调整数组顺序使奇数位于偶数前面 时间限制:1 秒内存限制:128 兆特殊判题:否提交:2 ...
随机推荐
- 解决因为google cdn无法访问导致无法打开stackoverflow等网站的方法
许多网站使用了Google的CDN来加速Jquery之类的库来加速网站访问,但由于方校长发福利的原因,导致这些网站很容易出现无法加载"ajax.googleapis.com"而出现 ...
- Spring 如何读取properties文件内容
http://hi.baidu.com/alizv/blog/item/d8cb2af4094662dbf3d38539.html 在现实工作中,我们常常需要保存一些系统配置信息,大家一般都会选择配置 ...
- 【java】【mysql】存储微信表情emoji表情
java.sql.SQLException: Incorrect string value: '\xF0\x9F\x92\x94' for colum n 'name' at row 1 at com ...
- POJ 2046 Gap 搜索- 状态压缩
题目地址: http://poj.org/problem?id=2046 一道搜索状态压缩的题目,关键是怎样hash. AC代码: #include <iostream> #include ...
- 开源 免费 java CMS - FreeCMS1.5-数据对象-info
下载地址:http://code.google.com/p/freecms/ info 在信息页静态化时,系统会自动向模板文件传递currInfo对象,此对象存储当前信息.在使用信息相关标签时,标签会 ...
- [转载][概念]Storage Pool, Private RAID Group, Private LUN
Storage Pool的起源 ========================== Some time ago, EMC introduced the concept of Virtual Prov ...
- C#的四个基本技巧
1.如果可能尽量使用接口来编程 .NET框架包括类和接口,在编写程序的时候,你可能知道正在用.NET的哪个类.然而,在这种情况下如果你用.NET支持的接口而不是它的类来编程时,代码会变得更加稳定.可用 ...
- ubuntu14.04如何卸载mysql
1. 删除mysql的数据文件 sudo rm /var/lib/mysql/ -R 2. 删除mqsql的配置文件 sudo rm /etc/mysql/ -R 3. 自动卸载mysql的程序 su ...
- 数字证书相关技术 : Versign信任签章
资料网址: 淘宝网站可信服务 http://www.ert7.com/case/eb/1391.html Versign信任签章 http://www.ert7.com/verisign/ssl/29 ...
- ActiveMQ的环境搭建及使用
一:环境搭建 ActiveMQ官网下载mq在windows上的安装包:http://activemq.apache.org/,解压到某个磁盘下. 运行要环境条件:jdk安装1.8,(本人这里安装版本) ...