日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

剑指offer面试题:替换空格

發布時間:2023/11/30 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 剑指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>??
  • ??
  • //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面试题:替换空格的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。