1. 程式人生 > >python 左旋轉字符串

python 左旋轉字符串

循環 log span def odi ret pos 現在 ota

比較簡單的一道題

匯編語言中有一種移位指令叫做循環左移(ROL),現在有個簡單的任務,就是用字符串模擬這個指令的運算結果。對於一個給定的字符序列S,請你把其循環左移K位後的序列輸出。例如,字符序列S=”abcXYZdef”,要求輸出循環左移3位後的結果,即“XYZdefabc”。是不是很簡單?OK,搞定它!

思路:

不斷地將 第一個 字符 拿到 字符串後 ,拿n 次就行

 1 # -*- coding:utf-8 -*-
 2 class Solution:
 3     def LeftRotateString(self, s, n):
 4         # write code here
5 if s == ‘‘: 6 return s 7 l = list(s) 8 l1 = [] 9 10 for i in range(n): 11 j = 0 12 l1.append(l[j]) 13 del l[j] 14 l.append(l1[j]) 15 del l1[j] 16 17 s1 = ‘‘.join(l)
18 return s1 19

python 左旋轉字符串