43. The left rotation string (Python)

Title Description

There is a shift in the assembly language instruction called a rotate left (the ROL), and now there is a simple task, this instruction is simulated by the string operation result. For a given character sequence S, you put its left circle after K-bit serial output. For example, the character sequence S = "abcXYZdef", rotated left required output result after three, i.e. "XYZdefabc". Is not it simple? OK, get it!
method one:
1 class Solution:
2     def LeftRotateString(self, s, n):
3         # write code here
4         return s[n:]+s[:n]

Method Two:

 1 class Solution:
 2     def LeftRotateString(self, s, n):
 3         # write code here
 4         if s == "":
 5             return ""
 6         s=list(s)
 7         self.reverse(s,0,n-1)
 8         self.reverse(s,n,len(s)-1)
 9         self.reverse(s,0,len(s)-1)
10         return ''.join(s)
11     def reverse(self,s,low,high):
12         while low < high:
13             s[low],s[high]=s[high],s[low]
14             low+=1
15             high-=1

2019-12-25 19:25:09

Guess you like

Origin www.cnblogs.com/NPC-assange/p/12098513.html