天天看點

字元串反轉的面試題,你會嗎?

不用申請記憶體空間,把一個字元串做反正操作。

比如說:

str=”abcdefg”

res=”gfedcba”

這個比較簡單,隻要做前後字元交換就可以了

func reverse(str []byte){

i := 0

j := len(str) - 1

}

第二階段

不用申請記憶體,如何把每個單詞做反轉,假設單詞中間隻有一個空格

str = “php is the best programing language in the world”

res = “php si eht tseb gnimargorp egaugnal ni eht dlrow”

func reverse(str string) {

k := 0

第三階段

不用申請記憶體,如何把一組單詞做反轉。

res = “world the in language programing best the is php”

這個略有難度,但是隻需要在第二階段的接觸上加一行代碼就可以做到了。

繼續閱讀