剑指offer——面试题5:替换空格

Posted acm-jing

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了剑指offer——面试题5:替换空格相关的知识,希望对你有一定的参考价值。

利用STL:

技术图片
 1 #include"iostream"
 2 #include"stdio.h"
 3 #include"algorithm"
 4 using namespace std;
 5 
 6 string ReplaceBlank(string src)
 7 {
 8     if(src.find(" ")>src.length())
 9         return src;
10     while(src.find(" ")<src.length())
11     {
12         int pos=src.find(" ");
13         src=src.replace(pos,1,"%20");
14     }
15     return src;
16 }
17 
18 void Test(char *testName,string testStr,string resStr)
19 {
20     if(testName!=nullptr)
21         printf("the %s begin:",testName);
22     if(testStr=="")
23     {
24         printf("the source string is null!
");
25         return;
26     }
27     string res=ReplaceBlank(testStr);
28     if(res==resStr)
29         printf("passed.
");
30     else
31         printf("failed
");
32 }
33 
34 //包含空格的字符串
35 void Test1()
36 {
37     Test("Test1","We are happy enough !","We%20are%20happy%20enough%20!");
38 }
39 
40 //不包含空格的字符串
41 void Test2()
42 {
43     Test("Test2","WeAreHappy","WeAreHappy");
44 }
45 
46 //空字符串
47 void Test3()
48 {
49     Test("Test3","","");
50 }
51 
52 int main()
53 {
54     Test1();
55     Test2();
56     Test3();
57     return 0;
58 }
View Code

官方给出的O(n)复杂度的算法:

技术图片
  1 // 面试题5:替换空格
  2 // 题目:请实现一个函数,把字符串中的每个空格替换成"%20"。例如输入“We are happy.”,
  3 // 则输出“We%20are%20happy.”。
  4 
  5 #include <cstdio>
  6 #include <cstring>
  7 
  8 /*length 为字符数组str的总容量,大于或等于字符串str的实际长度*/
  9 void ReplaceBlank(char str[], int length)
 10 {
 11     if(str == nullptr && length <= 0)
 12         return;
 13 
 14     /*originalLength 为字符串str的实际长度*/
 15     int originalLength = 0;
 16     int numberOfBlank = 0;
 17     int i = 0;
 18     while(str[i] != )
 19     {
 20         ++ originalLength;
 21 
 22         if(str[i] ==  )
 23             ++ numberOfBlank;
 24 
 25         ++ i;
 26     }
 27 
 28     /*newLength 为把空格替换成‘%20‘之后的长度*/
 29     int newLength = originalLength + numberOfBlank * 2;
 30     if(newLength > length)
 31         return;
 32 
 33     int indexOfOriginal = originalLength;
 34     int indexOfNew = newLength;
 35     while(indexOfOriginal >= 0 && indexOfNew > indexOfOriginal)
 36     {
 37         if(str[indexOfOriginal] ==  )
 38         {
 39             str[indexOfNew --] = 0;
 40             str[indexOfNew --] = 2;
 41             str[indexOfNew --] = %;
 42         }
 43         else
 44         {
 45             str[indexOfNew --] = str[indexOfOriginal];
 46         }
 47 
 48         -- indexOfOriginal;
 49     }
 50 }
 51 
 52 // ====================测试代码====================
 53 void Test(char* testName, char str[], int length, char expected[])
 54 {
 55     if(testName != nullptr)
 56         printf("%s begins: ", testName);
 57 
 58     ReplaceBlank(str, length);
 59 
 60     if(expected == nullptr && str == nullptr)
 61         printf("passed.
");
 62     else if(expected == nullptr && str != nullptr)
 63         printf("failed.
");
 64     else if(strcmp(str, expected) == 0)
 65         printf("passed.
");
 66     else
 67         printf("failed.
");
 68 }
 69 
 70 // 空格在句子中间
 71 void Test1()
 72 {
 73     const int length = 100;
 74 
 75     char str[length] = "hello world";
 76     Test("Test1", str, length, "hello%20world");
 77 }
 78 
 79 // 空格在句子开头
 80 void Test2()
 81 {
 82     const int length = 100;
 83 
 84     char str[length] = " helloworld";
 85     Test("Test2", str, length, "%20helloworld");
 86 }
 87 
 88 // 空格在句子末尾
 89 void Test3()
 90 {
 91     const int length = 100;
 92 
 93     char str[length] = "helloworld ";
 94     Test("Test3", str, length, "helloworld%20");
 95 }
 96 
 97 // 连续有两个空格
 98 void Test4()
 99 {
100     const int length = 100;
101 
102     char str[length] = "hello  world";
103     Test("Test4", str, length, "hello%20%20world");
104 }
105 
106 // 传入nullptr
107 void Test5()
108 {
109     Test("Test5", nullptr, 0, nullptr);
110 }
111 
112 // 传入内容为空的字符串
113 void Test6()
114 {
115     const int length = 100;
116 
117     char str[length] = "";
118     Test("Test6", str, length, "");
119 }
120 
121 //传入内容为一个空格的字符串
122 void Test7()
123 {
124     const int length = 100;
125 
126     char str[length] = " ";
127     Test("Test7", str, length, "%20");
128 }
129 
130 // 传入的字符串没有空格
131 void Test8()
132 {
133     const int length = 100;
134 
135     char str[length] = "helloworld";
136     Test("Test8", str, length, "helloworld");
137 }
138 
139 // 传入的字符串全是空格
140 void Test9()
141 {
142     const int length = 100;
143 
144     char str[length] = "   ";
145     Test("Test9", str, length, "%20%20%20");
146 }
147 
148 int main(int argc, char* argv[])
149 {
150     Test1();
151     Test2();
152     Test3();
153     Test4();
154     Test5();
155     Test6();
156     Test7();
157     Test8();
158     Test9();
159 
160     return 0;
161 }
View Code

 

以上是关于剑指offer——面试题5:替换空格的主要内容,如果未能解决你的问题,请参考以下文章

剑指offer字符串面试题 替换空格

剑指offer字符串面试题 替换空格

剑指OFFER----面试题05.替换空格

面试题5:替换空格

面试题5:替换空格

[剑指offer]面试题4:替换空格