旋转字符串

旋转字符串

给定一个字符串(以字符数组的形式给出)和一个偏移量,根据偏移量原地旋转字符串(从左向右旋转)

样例

样例  1:
	输入:  str="abcdefg", offset = 3
	输出: "efgabcd"
	
	样例解释: 
	返回旋转后的字符串。

样例 2:
	输入: str="abcdefg", offset = 0
	输出: "abcdefg"
	
	样例解释: 
	返回旋转后的字符串

样例 3:
	输入: str="abcdefg", offset = 1
	输出: "gabcdef"
	
	样例解释: 
	返回旋转后的字符串

样例 4:
	输入: str="abcdefg", offset =2
	输出:"fgabcde"
	
	样例解释: 
	返回旋转后的字符串
 1 class Solution:
 2     """
 3     @param str: An array of char
 4     @param offset: An integer
 5     @return: nothing
 6     """
 7     def rotateString(self, str, offset):
 8         # write your code here
 9         if len(str) == 0:
10             return
11         n = offset % len(str)
12         nStr = str[-n:] + str[:-n]
13         str.clear()
14         str.extend(nStr)

 

 
posted @ 2019-03-16 17:50  漸行漸遠  阅读(279)  评论(0编辑  收藏  举报