翻轉字串

2021-10-02 03:25:33 字數 4106 閱讀 3335

給定字元陣列chars,在單詞間逆序,對空格位置沒有特別要求。

例如:

『dog loves pig』

-> 『gip sevol god』

-> 『pig loves dog』

先整體逆序,再按單詞逆序。

def

reverse

(chars, beg, end)

:while beg < end:

chars[beg]

, chars[end-1]

= chars[end-1]

, chars[beg]

beg +=

1 end -=

1def

rotate_by_word

(s):

if s is

none

:return s

chars =

list

(s) reverse(chars,0,

len(chars)

) begin, end =-1

,-1for i in

range

(len

(chars)):

c = chars[i]

if c !=

' ':

if begin ==-1

: begin = i

else

: end = i

if i ==

len(chars)-1

: end =

len(chars)

if begin !=-1

and end !=-1

: reverse(chars, begin, end)

begin, end =-1

,-1return

''.join(c for c in chars)

def

rotate_by_word2

(s):

s = s[::

-1]return

' '.join(a[::

-1]for a in s.split(

))

def

test_reverse()

: s =

'abcdef'

chars =

list

(s) reverse(chars,0,

len(chars)

) r =

''.join(chars)

r1 = s[::

-1] r2 =

''.join(

reversed

(s))

assert

(r == r1)

assert

(r == r2)

print

('test reverse done'

)def

test_rotate_by_word()

:assert

(rotate_by_word(

'abc def')==

'def abc'

)assert

(rotate_by_word(

'dog loves pig')==

'pig loves dog'

)assert

(rotate_by_word(

"i'm a student.")==

"student. a i'm"

)assert

(rotate_by_word2(

'abc def')==

'def abc'

)assert

(rotate_by_word2(

'dog loves pig')==

'pig loves dog'

)assert

(rotate_by_word2(

"i'm a student.")==

"student. a i'm"

)print

('test rotate by word done'

)

牛客網

牛客網

給定字元陣列chars,和乙個整數size,把大小為size的左半區整體移到右半區,右半區移到左半區。

例如:『abcde』 size = 3

-> 『deabc』

『abcde』

-> 『cbaed』

-> 『deabc』

先按size分成兩部分,兩部分分別逆序,然後整體逆序

def

rotate_k

(s, k)

:if s is

none

orlen

(s)< k:

return s

chars =

list

(s) reverse(chars,

0, k)

reverse(chars, k,

len(chars)

) reverse(chars,0,

len(chars)

)return

''.join(c for c in chars)

def

rotate_k2

(s, k)

:return s[k:

]+ s[

:k]

書上提供六另一種思路,過程如下

『1234567abcd』 size = 7

def

rotate_k3

(s, k)

:def

exchange

(chars, begin, end, size)

: i = end - size

for _ in

range

(size)

: chars[begin]

, chars[i]

= chars[i]

, chars[begin]

begin +=

1 i +=

1if s is

none

orlen

(s)< k:

return s

chars =

list

(s) begin, end =0,

len(s)

left, right = k,

len(s)

-k while

true

: d = left - right

s =min(left, right)

exchange(chars, begin, end, s)

if d ==0:

break

elif d <0:

end -= s

right =

-d elif d >0:

begin += s

left = d

return

''.join(chars)

def

test_rotate_k()

:assert

(rotate_k(

'abcde',3

)=='deabc'

)assert

(rotate_k2(

'abcde',3

)=='deabc'

)assert

(rotate_k3(

'1234567abcd',7

)=='abcd1234567'

)print

('test rotate k done'

)if __name__ ==

'__main__'

: test_reverse(

) test_rotate_k(

) test_rotate_by_word(

)

牛客網

翻轉字串 翻轉單詞字串

將一句話裡面的單詞進行倒置,標點符號不倒換。比如一句話 i come from china.倒換後變成 china.from come i 解析 解決該問題可以分為兩步,第一步全盤置換為 anihc morf emoc i 第二部對每個單詞進行逐步翻轉,如果不是空格,則開始翻轉單詞。具體 如下 in...

字串翻轉

遞迴入門 字串翻 將字串 test 翻轉,變為 tset 解法 遞迴 此題的遞迴跟判斷回文字串的解法原理一樣。只是不是比較兩端字元,而是直接交換。include using namespace std int str turn int low,int high,char p,int length t...

字串翻轉

字串翻轉是常見筆試面試題,記錄下來 include include void reverse const char src char dest intstrlen strlen src while strlen void strrev ms char input char output int le...