生活随笔
收集整理的這篇文章主要介紹了
剑指offer面试题:替换空格
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
https://blog.csdn.net/yanxiaolx/article/details/52235212
?題目:請實現一個函數,把字符串中的每個空格替換成“%20”。例如輸入“We are happy.”,則輸出“We%20are%20happy.”。
解析:時間復雜度為O(n)的解法。
完整代碼及測試用例實現:
[cpp]?view plaincopy
#include<iostream>??using?namespace?std;??#include?<cstring>??????void?ReplaceBlank(char?string[],?int?length)??{??????if?(string?==?NULL&&length?<=?0)??????{??????????return;??????}????????????int?reallyLength?=?0,?numberOfBlank?=?0,i=0;????????????while?(string[i]!='\0')??????{??????????++reallyLength;????????????if?(string[i]?==?'?')??????????{??????????????++numberOfBlank;??????????}??????????++i;??????}??????????????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()??{????????????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面试题:替换空格的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。