LeetCode #1427. Perform String Shifts
作者:互联网
题目
解题方法
先根据shift数组找到向右转的次数,对其取len(s)的模并再取绝对值,然后初始化一个长度为len(s)的列表rat,遍历s将字符填到对应位置,最后把rat合并成字符串返回即可。
时间复杂度:O(n)
空间复杂度:O(n)
代码
class Solution:
def stringShift(self, s: str, shift: List[List[int]]) -> str:
rightshifts = 0
for i in shift:
if not i[0]:
rightshifts -= i[1]
else:
rightshifts += i[1]
rightshifts = abs(rightshifts % len(s))
rat = ["" for _ in range(len(s))]
for i in range(len(s)):
rat[(i + rightshifts) % len(s)] = s[i]
return "".join(rat)
标签:Perform,shift,复杂度,rat,len,Shifts,rightshifts,LeetCode 来源: https://www.cnblogs.com/RatsCommander/p/14069051.html