rotate string
- Problem Description
Given a string (in the form of a character array) and an offset, rotate the string in place from left to right according to the offset.
2. Problem example
Input str = "abcdefg", offset=3, output "efgabcd".
Input str = "abcdefg", offset=0, output "abcdefg".
Input str = "abcdefg", offset=1, output "gabcdef".
3. Code implementation
class Solution:
#参数s:字符列表
#参数offset:整数
#返回值:无
def rotateString(self,s,offset):
if len(s)>0:
offset = offset % len(s)
temp = (s + s)[len(s) - offset:2 * len(s) - offset]
for i in range(len(temp)):
s[i] = temp[i]
#主函数
if __name__ == '__main__':
s = ["a","b","c","d","e","f","g"]
offset = 3
solution = Solution()
solution.rotateString(s,offset)
print("输入:s=",["a","b","c","d","e","f","g"],"\n\t","offset = ",offset)
print("输出:s=",s)
Adjust the main function:
if __name__ =='__main__':
solution = Solution()
s=list(input("输入:s="))
offset = int(input("offset="))
solution.rotateString(s,offset)
print("输出:s=", s)
Example: