剑指offer面试题:替换空格
https://blog.csdn.net/yanxiaolx/article/details/52235212
题目:请实现一个函数,把字符串中的每个空格替换成“%20”。例如输入“We are happy.”,则输出“We%20are%20happy.”。
解析:时间复杂度为O(n)的解法。
完整代码及测试用例实现:
- #include<iostream>
- using namespace std;
- #include <cstring>
- //length 为字符数组string的总容量
- void ReplaceBlank(char string[], int length)
- {
- if (string == NULL&&length <= 0)
- {
- return;
- }
- //reallyLength 为字符串string的实际长度
- int reallyLength = 0, numberOfBlank = 0,i=0;
- while (string[i]!='\0')
- {
- ++reallyLength;
- if (string[i] == ' ')
- {
- ++numberOfBlank;
- }
- ++i;
- }
- //newLength 为把空格替换成'%20'之后的长度
- int newLength = reallyLength + numberOfBlank * 2;
- if (newLength > length)
- {
- return;
- }
- int indexOfReally = reallyLength;
- int indexOfNew = newLength;
- while (indexOfReally >= 0 && indexOfNew >indexOfReally)
- {
- if (string[indexOfReally] == ' ')
- {
- string[indexOfNew--] = '0';
- string[indexOfNew--] = '2';
- string[indexOfNew--] = '%';
- }
- else
- {
- string[indexOfNew--] = string[indexOfReally];
- }
- --indexOfReally;
- }
- }
- // ====================测试代码====================
- void Test(char* testName, char string[], int length, char expected[])
- {
- if (testName != NULL)
- {
- cout << testName << " begins: ";
- }
- ReplaceBlank(string, length);
- if (expected == NULL && string == NULL)
- {
- cout << "passed." << endl;
- }
- else if (expected == NULL && string != NULL)
- {
- cout << "failed." << endl;
- }
- else if (strcmp(string, expected) == 0)
- {
- cout << "passed." << endl;
- }
- else
- {
- cout << "failed." << endl;
- }
- }
- void Test1()
- {
- // 空格在句子中间
- const int length = 100;
- char string[length] = "we are happy.";
- Test("Test1", string, length, "we%20are%20happy.");
- }
- void Test2()
- {
- // 空格在句子开头
- const int length = 100;
- char string[length] = " wearehappy.";
- Test("Test2", string, length, "%20wearehappy.");
- }
- void Test3()
- {
- // 空格在句子末尾
- const int length = 100;
- char string[length] = "wearehappy. ";
- Test("Test3", string, length, "wearehappy.%20");
- }
- void Test4()
- {
- // 连续有两个空格
- const int length = 100;
- char string[length] = "we are happy.";
- Test("Test4", string, length, "we%20%20are%20happy.");
- }
- void Test5()
- {
- // 传入NULL
- Test("Test5", NULL, 0, NULL);
- }
- void Test6()
- {
- // 传入内容为空的字符串
- const int length = 100;
- char string[length] = "";
- Test("Test6", string, length, "");
- }
- void Test7()
- {
- //传入内容为一个空格的字符串
- const int length = 100;
- char string[length] = " ";
- Test("Test7", string, length, "%20");
- }
- void Test8()
- {
- // 传入的字符串没有空格
- const int length = 100;
- char string[length] = "wearehappy.";
- Test("Test8", string, length, "wearehappy.");
- }
- void Test9()
- {
- // 传入的字符串全是空格
- const int length = 100;
- char string[length] = " ";
- Test("Test9", string, length, "%20%20%20");
- }
- int main()
- {
- Test1();
- Test2();
- Test3();
- Test4();
- Test5();
- Test6();
- Test7();
- Test8();
- Test9();
- system("pause");
- return 0;
- }
运行结果:
Test1 begins: passed.
Test2 begins: passed.
Test3 begins: passed.
Test4 begins: passed.
Test5 begins: passed.
Test6 begins: passed.
Test7 begins: passed.
Test8 begins: passed.
Test9 begins: passed.
请按任意键继续. . .
剑指offer面试题:替换空格相关推荐
- 剑指Offer(48)-[ArrayString]替换空格
点击查看剑指Offer全解[Java & Golang]实现 题目描述 请实现一个函数,将一个字符串中的每个空格替换成"%20".例如,当字符串为We Are Happy. ...
- 剑指Offer(二):替换空格
参考链接: https://cuijiahua.com/blog/2017/11/basis_2.html https://blog.csdn.net/wang454592297/article/de ...
- Java之《剑指Offer》:字符串替换空格,请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happ
1. 题目描述 请实现一个函数,将一个字符串中的每个空格替换成"%20".例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. ① ...
- 剑指offer面试题[4]-空格替换
题目描述 请实现一个函数,将一个字符串中的空格替换成"%20".例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 以输入hell ...
- 剑指offer二之替换空格
一.题目: 请实现一个函数,将一个字符串中的空格替换成"%20".例如,当字符串为I love you.则经过替换之后的字符串为I%20love%20You. 二.解题方法: 方法 ...
- 【剑指offer】_02替换空格
题目描述 请实现一个函数,将一个字符串中的每个空格替换成"%20".例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 解题思路 ...
- python剑指offer替换空格_《剑指Offer》字符串 替换空格
//str.replace(char oldChar,char newChar);用字符newChar替换oldChar;返回一个新的字符串 public class Solution { publi ...
- 剑指offer二:替换空格
题目描述: 请实现一个函数,将一个字符串中的空格替换成"%20".例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 解题思路: ...
- 【剑指Offer面试题】 九度OJ1510:替换空格
c/c++ 中的字符串以"\0"作为结尾符.这样每一个字符串都有一个额外字符的开销. 以下代码将造成内存越界. char str[10]; strcpy(str, "01 ...
- 剑指offer——面试题4:替换空格
#剑指offer--面试题4:替换空格 此题的关键思路在于字符串的从后向前复制!!! class Solution { public:void replaceSpace(char *str,int l ...
最新文章
- 【Android UI】图片 + 文字展示by SpannableStringBuilder
- 出块过程 (1)close发送消息
- 构造数列中的常见变形总结【中阶和高阶辅导】
- java枚举怎么编译不行的_java枚举类型
- video-js RTMP直播
- 【kafka】kerberos client is being asked for a password not available to garner authentication informa
- AndroidStuido连接不上手机的解决方法
- java中正则表达式截取字符串
- css 多行文本的溢出显示省略号(移动端)
- 2016年总结与2017展望
- 软考信息系统监理师:2016年4月22日作业
- Excel打印时完整显示合并的单元格区域
- linux下用户名怎么修改密码,LINUX用户名密码忘记怎么修改用户密码
- 数据之美:迄今 10 佳数据可视化示例
- 微信公众号开发----生成带参数的临时二维码
- python 爬取知网url
- 中国大学MOOC大学生心理健康试题及答案
- 全球首家BAYC NFT主题餐厅BoredHungry开业,可使用APE和ETH支付
- Ubuntu安装开源终端工具Tabby
- 线上服务器CPU负载过高的问题解决过程
热门文章
- 最全Pycharm教程(10)——Pycharm调试器总篇
- JDBC连接(MySql)数据库步骤,以及查询、插入、删除、更新等十一个处理数据库信息的功能。...
- poj 1862 Stripies/优先队列
- commons-lang的FastDateFormat性能测试
- 三菱st语言编程实例_LD、FBD、IL、ST、SFC、CFC六种编程语言的特点
- 大学物理质点动力学思维导图_生理学 | 思维导图
- java对hashmap迭代_Java:通过HashMap迭代,这样更有效率?
- php copy 文件夹,php删除与复制文件夹及其文件夹下所有文件的实现代码
- php 删除单个文件大小,php删除指定大小的jpg文件
- cass展点不在原位置,cass中打开一副图后,通过绘图处理-——展高程点,怎么之前的图形就不显示了,,只剩下高程点!!...