-
Notifications
You must be signed in to change notification settings - Fork 1
/
rotate-string.py
64 lines (58 loc) · 1.41 KB
/
rotate-string.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
58
59
60
61
62
63
64
# coding=utf-8
"""
796. Rotate String
"""
class Solution(object):
def rotateString(self, A, B):
"""
:type A: str
:type B: str
:rtype: bool
"""
if len(A) != len(B):
return False
if not A:
return True
n = len(A)
for shift in range(n):
C = A[shift:] + A[:shift]
if C == B:
return True
return False
def rotateString1(self, A, B):
"""
:type A: str
:type B: str
:rtype: bool
下面这种方法先产生一个A+A的字符串,如果B 在这个字符串里, 那一定是A通过旋转得到的,
好巧妙啊
"""
if len(A) != len(B):
return False
C = A + A
if B in C:
return True
return False
def rotateString2(self, A, B):
return len(A) == len(B) and B in A+A
def rotateString2(self, A, B):
if A == B:
return True
for i in range(len(A)):
# 每次左移一位,这样会改变A的值
A = A[1:] + A[0]
if A == B:
return True
return False
def LeftRotateString(self, s, n):
"""
字符串左移n位
:param s:
:param n:
:return:
"""
if not s:
return s
N = len(s)
n = n%N
return s[n:] + s[:n]