forked from yingl/LintCodeInPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrotate_string.py
More file actions
25 lines (23 loc) · 731 Bytes
/
rotate_string.py
File metadata and controls
25 lines (23 loc) · 731 Bytes
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
# -*- coding: utf-8 -*-
class Solution:
# @param s: a list of char
# @param offset: an integer
# @return: nothing
def rotateString(self, s, offset):
# write you code here
if s:
shift = offset % len(s)
if shift > 0:
'''
abcdefg, 2 => fgabcde
ab => ba, cdefg => gfedc
bagfedc => cdefgab
'''
self.reverse(s, 0, len(s) - shift - 1)
self.reverse(s, len(s) - shift, len(s) - 1)
s.reverse()
def reverse(self, s, left, right):
while left < right:
s[left], s[right] = s[right], s[left]
left += 1
right -= 1