lintcode:旋转字符串

xiaoxiao2021-02-28  5

class Solution: # @param s: a list of char # @param offset: an integer # @return: nothing def rotateString(self, s, offset): # write you code here # 198ms if not s:return s n = len(s) offset = offset % n if not s:return s for i in range(offset): t = s.pop() s.insert(0,t) # 189ms 分别赋值 if not s:return s n = len(s) offset = offset % n if not offset:return s temp_pre = s[-offset:] temp_las = s[0:n-offset] m = len(temp_pre) for i in range(len(temp_pre)): s[i] = temp_pre[i] for i in range(len(temp_pre), n): s[i] = temp_las[i - m] # 206ms 字符串 * 2 if not s:return s n = len(s) offset = offset%n if not offset:return s temp = s*2 for i in range(n): s[i] = temp[n-offset+i] # 198ms 互换位置 if not s: return s n = len(s) offset = offset % n if not offset:return s s[0:n-offset], s[-offset:] = s[-offset:], s[0:n-offset] # 226ms 三次反转 if not s:return s n = len(s) offset = offset % n if not offset:return s s[0:n-offset] = reversed(s[0:n-offset]) s[-offset:] = reversed(s[-offset:]) s[::] = reversed(s)
转载请注明原文地址: https://www.6miu.com/read-200275.html

最新回复(0)