describe

ENG

Given a string (in the form of an array of characters) and an offset, rotate the string in place (from left to right) based on the offset.

Offset >= 0 The length of STR >= 0

Have you ever come across this question in a real interview? is

The title error correction

instructions

Rotating in place means you have to modify in S itself. You don’t have to return anything.

The sample

Sample 1:

Input: STR ="abcdefg", offset = 3 Output: STR =" efgabcd"Copy the code

Example 2:

Input: STR ="abcdefg", offset = 0 Output: STR ="abcdefg" Example:Copy the code

Sample 3:

Input: STR ="abcdefg", offset = 1 Output: STR =" gabcdef"Copy the code

Example 4:

Input: STR ="abcdefg", offset =2 Output: STR =" fgabcde"Copy the code

The sample 5:

Input: STR ="abcdefg", offset = 10 Output: STR =" efgabcd"Copy the code

My code

class Solution:
    """
    @param str: An array of char
    @param offset: An integer
    @return: nothing
    """
    def rotateString(self, s, offset):
        # write your code here
        if not offset: return
        if not s: return
         
        n = len(s)
        offset = offset%n
         
        for i in range(offset):
            t = s.pop()
            s.insert(0,t)
Copy the code