【剑指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 ...
随机推荐
- Druid 配置_StatFilter
Druid内置提供一个StatFilter,用于统计监控信息. 1. 别名配置 StatFilter的别名是stat,这个别名映射配置信息保存在druid-xxx.jar!/META-INF/drui ...
- JavaScript如何获取/计算页面元素的offset?
问题 通过点击一控件,在控件的下面显示一个浮动层,通常的做法是:获取此控件的offset值,再计算出浮动层的top,left等css属性的值,赋值即可. 那么下面就看一下如何获取控件的offset值 ...
- Delegates and Events
People often find it difficult to see the difference between events and delegates. C# doesn't help m ...
- jdbc preparestatement和preparestatement区别
1.preparestatement预编译,预编译指的是DB的编译器,会对此sql语句提前编译.然后将预编译的结果缓存到数据库中,下次执行时替换参数直接执行编译过的语句. 记住:数据库也是有编译器的, ...
- 基于Memcached的tomcat集群session共享所用的jar及多个tomcat各种序列化策略配置
原文:http://www.cnblogs.com/interdrp/p/4096466.html 多个tomcat各种序列化策略配置如下:一.java默认序列化tomcat配置conf/contex ...
- 水平ListView
/* * HorizontalListView.java v1.5 * * * The MIT License * Copyright (c) 2011 Paul Soucy (paul@dev-sm ...
- client怎样调用IBinder接口对象
代码: public void funclick(View view){ Intent _intent = new Intent(MainActivity.this,MyService.class); ...
- memcache在大型网站的应用策略
[部署策略] 基于memcached的 slab 和dump的内存管理方式,它产生的内存碎片比较少,不需要OS去做繁杂的内存回收,所以它对CPU的占用率那是相当的低.所以建议将它跟占用CPU较高 的W ...
- web.xml加载顺序 [转载]
一 1.启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<listener>和<context-param>两个结点. 2.紧急着,容创建一个Ser ...
- Java多线程学习(吐血超具体总结)
林炳文Evankaka原创作品. 转载请注明出处http://blog.csdn.net/evankaka 写在前面的话:此文仅仅能说是java多线程的一个入门.事实上Java里头线程全然能够写一本书 ...