劍指offer 2替換空格為指定字串

2021-09-26 22:28:06 字數 411 閱讀 3094

該題是劍指offer的基礎題,輸入乙個字串,裡面有一些空格,將空格填寫「%20」這三個字元。然後輸出。

下面是簡單**。

public class test0401

}int targetlength = usedlength + blanknum*2;

while (usedlength > 0 && usedlength< targetlength)else

usedlength--;

}system.out.println(string);

}public static void main(string args)

}

拓展:本題可以有很多拓展的地方,比如,輸入資料校驗,如果長度為0,返回-1;沒有空格,則不處理;可以再進行優化。

劍指offer(2) 替換空格

假設面試官讓我們在原來的字串上做替換。時間複雜度為o n 的解法。我們可以先遍歷一遍字串,這樣就能統計出字串中空格的總數,並可以由此計算出替換之後的字串的總長度。每替換乙個空格,長度增加2,因此替換以後字串的長度等於原來的長度加上2乘以空格數目。length為牛客系統規定字串輸出的最大長度,固定為乙...

劍指offer 2 替換空格

請實現乙個函式,將乙個字串中的空格替換成 20 usr bin env python coding utf 8 created by xuehz on 2017 8 7 class solution s 源字串 defreplacespace self,s write code here if ty...

劍指Offer 2 替換空格

題目描述 python 實現 coding utf 8 class solution s 源字串 def replacespace self,s write code here count 0 for val in s if val count 1 s new len s count 2 index...