【剑指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 ...
随机推荐
- How to run WPF – XBAP as Full Trust Application
Recently I work on WPF-XBAP application that will run from intranet website: This application must h ...
- Setup SS5 Socks Proxy
Install and configure ss5 socks proxy with simple authentication SS5 is a high performance socks pro ...
- Unity3D面试题总结
一.什么是渲染管道? 是指在显示器上为了显示出图像而经过的一系列必要操作. 渲染管道中的很多步骤,都要将几何物体从一个坐标系中变换到另一个坐标系中去. 主要步骤有: 本地坐标->视图坐标-> ...
- 【mysql】二级索引----聚簇索引和非聚簇索引-----
参考地址: https://blog.csdn.net/bigtree_3721/article/details/51335479 https://blog.csdn.net/roxliu/artic ...
- DotNet和DotNet Core
EF 1.0 ---EF6.0 都是code firstmodel ,model model first model,database first model, EF7 是DOTNET CORE重框版 ...
- pytest文档1-环境准备与入门
前言 首先说下为什么要学pytest,在此之前相信大家已经掌握了python里面的unittest单元测试框架,那再学一个框架肯定是需要学习时间成本的. 刚开始我的内心是拒绝的,我想我用unittes ...
- [Android Studio] Android Studio中查看类的继承关系
转载自:http://blog.csdn.net/hyr83960944/article/details/38098091 查看类的继承关系的快捷键F4,在Android Studio常用快捷键这篇文 ...
- Webharvest网络爬虫应用总结,web-harvest 编写脚本 读取 百度 博客 实例
Webharvest网络爬虫应用总结 Web-Harvest是一个Java开源Web数据抽取工具.它能够收集指定的Web页面并从这些页面中提取有用的数据.其实现原理是,根据预先定义的配置文件用ht ...
- 在上已个Java Spring MVC项目基础上加MyBatis
代码目录: /Users/baidu/Documents/Data/Work/Code/Self/HelloSpringMVC 1. 首先在resource目录加上jdbc.properties: d ...
- .net使用自定义类属性
.net中可以使用Type.GetCustomAttributes获取类上的自定义属性,可以使用PropertyInfo.GetCustomAttributes获取属性信息上的自定义属性. 下面以定义 ...