【剑指Offer面试题】 九度OJ1510:替换空格
来源:互联网 发布:淘宝自动抢单软件 编辑:程序博客网 时间:2024/06/06 16:45
c/c++ 中的字符串以“\0”作为结尾符,这样每个字符串都有一个额外字符的开销。下面代码将造成内存越界。 char str[10];
strcpy(str, “0123456789”);
为了节省内存,c/c++ 会把常量字符串放到单独的一个内存区域。当几个指针赋予相同的常量字符串时,它们实际上会指向相同的内存地址。
题目链接地址:
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. 如果先计算出空格数目,从后往前,就可以实现一个字符只移动一次。学会转换思路。
/********************************* ----------------------------------- 【剑指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 <iostream>#include <string>using namespace std;int main(){ string str1="Hello"; char *str2=const_cast<char*>(str1.c_str()); cout<<str2; return 0;}
- 如果完成这道题,很简单方法是遇到非空格就输出,遇到空格就输出“%20”。
#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****************************************************************/
1 1
- 【剑指Offer面试题】 九度OJ1510:替换空格
- 剑指offer-->面试题4 替换空格
- 《剑指offer》面试题4 替换空格
- 【剑指offer】面试题4:替换空格
- 【剑指offer】面试题4替换空格
- 剑指offer 面试题4 替换空格
- 剑指Offer:面试题4 替换空格
- 剑指offer-面试题4-替换空格
- 《剑指Offer》面试题4:替换空格
- 【剑指offer】 面试题4: 替换空格
- 剑指offer 面试题4---替换空格
- 剑指offer面试题4-- 替换空格
- 《剑指Offer》面试题:替换空格
- 剑指Offer面试题2:替换空格
- 【剑指offer】面试题4:替换空格
- 剑指Offer:面试题4 替换空格
- 剑指offer-面试题4:替换空格
- 剑指offer面试题之替换空格
- Java IO学习【2】一个关于换行、续写的的小问题
- c.p.p第四章编程训练
- hdu5293
- ffmpeg常用命令
- 事件和路由事件概述
- 【剑指Offer面试题】 九度OJ1510:替换空格
- 最简单的基于FFmpeg的移动端例子:Android 视频解码器-单个库版
- 黑马程序员------C语言基础 字符串
- runtime-01
- .NET简谈路由事件
- java学习笔记一
- Red hat安装与使用
- Java初涉之8--main()函数入门
- html随笔