Old Wei wins the offer to take you to learn --- Brush title series (left rotation string 43.)

43. The left rotary string

problem:

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!

solve:

thought:

Using the queue, and then loop n times, each time speaking popleft () value re append () behind the queue.

python code:

# -*- coding:utf-8 -*-
from collections import deque
class Solution:
    def LeftRotateString(self, s, n):
        # write code here
        if(len(s)<n):
            return ''
        dq=deque()
        for x in s:
            dq.append(x)
        for _ in range(n):
            dq.append(dq.popleft())
        return ''.join([i for i in dq])
        
Published 160 original articles · won praise 30 · views 70000 +

Guess you like

Origin blog.csdn.net/yixieling4397/article/details/105056607