-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy path58_翻转字符串.py
57 lines (43 loc) · 1.29 KB
/
58_翻转字符串.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""
题目一: 翻转单词顺序
"""
def reverse_sentence(string):
if not isinstance(string, str):
return
list_of_char = list(string)
p_begin = 0
p_end = len(list_of_char) - 1
reverse(list_of_char, p_begin, p_end)
p_begin = 0
p_end = 0
while p_begin < len(list_of_char) - 1:
if list_of_char[p_begin] == " ":
p_begin += 1
p_end += 1
elif list_of_char[p_end] == " " or p_end == len(list_of_char) - 1:
p_end -= 1
reverse(list_of_char, p_begin, p_end)
p_end += 1
p_begin = p_end
else:
p_end += 1
return "".join(list_of_char)
def reverse(array, begin, end):
while begin < end:
array[begin], array[end] = array[end], array[begin]
begin += 1
end -= 1
"""
题目二: 左旋转字符串
"""
def rotate_string(string, n):
if not isinstance(string, str) or len(string) == 0 or n < 1 or n >= len(string):
return string
list_of_char = list(string)
reverse(list_of_char, 0, n - 1)
reverse(list_of_char, n, len(string) - 1)
reverse(list_of_char, 0, len(string) - 1)
return "".join(list_of_char)
if __name__ == "__main__":
print(reverse_sentence("I am a student."))
print(rotate_string("abcdefg", 2))