zoukankan      html  css  js  c++  java
  • 8. 旋转字符串

    8. 旋转字符串

    中文English

    给定一个字符串(以字符数组的形式给出)和一个偏移量,根据偏移量原地旋转字符串(从左向右旋转)。

    样例

    样例 1:

    输入:  str="abcdefg", offset = 3
    输出:  str = "efgabcd"	
    样例解释:  注意是原地旋转,即str旋转后为"efgabcd"
    

    样例 2:

    输入: str="abcdefg", offset = 0
    输出: str = "abcdefg"	
    样例解释: 注意是原地旋转,即str旋转后为"abcdefg"
    

    样例 3:

    输入: str="abcdefg", offset = 1
    输出: str = "gabcdef"	
    样例解释: 注意是原地旋转,即str旋转后为"gabcdef"
    

    样例 4:

    输入: str="abcdefg", offset =2
    输出: str = "fgabcde"	
    样例解释: 注意是原地旋转,即str旋转后为"fgabcde"
    

    样例 5:

    输入: str="abcdefg", offset = 10
    输出: str = "efgabcd"	
    样例解释: 注意是原地旋转,即str旋转后为"efgabcd"
    

    挑战

    在数组上原地旋转,使用O(1)的额外空间

    说明

    原地旋转意味着你要在s本身进行修改。你不需要返回任何东西。

    注意事项

    offset >= 0
    the length of str >= 0
    Make changes on the original input data

    输入测试数据 (每行一个参数)如何理解测试数据?
    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 s:
                return ''
            offset = offset%len(s)
            a =  s[-offset:] + s[:-offset] 
            for i in range(len(s)):
                s[i] = a[i]
            return s
  • 相关阅读:
    田忌赛马 题解
    亚历山大的丢番图方程 题解
    zhx's contest题解
    芝麻OI比赛T7edges题解
    CSP-J2020游记
    Linux shell 学习笔记(五)
    Linux shell 学习笔记(四)
    Linux shell 学习笔记(三)
    Linux shell 学习笔记(二)
    Oracle并发
  • 原文地址:https://www.cnblogs.com/yunxintryyoubest/p/12942567.html
Copyright © 2011-2022 走看看