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

    1790. 旋转字符串II

    中文English

    给出一个字符串(以字符数组形式给出),一个右偏移和一个左偏移,根据给出的偏移量循环移动字符串。(left offest表示字符串向左的偏移量,right offest表示字符串向右的偏移量,左偏移量和右偏移量计算得到总偏移量,在总偏移量处分成两段字符串并交换位置)。

    样例

    Example 1:

    Input:str ="abcdefg", left = 3, right = 1
    Output:"cdefgab"
    Explanation:The left offset is 3, the right offset is 1, and the total offset is left 2. Therefore, the original string moves to the left and becomes "cdefg"+ "ab".
    

    Example 2:

    Input:str="abcdefg", left = 0, right = 0
    Output:"abcdefg"
    Explanation:The left offset is 0, the right offset is 0, and the total offset is 0. So the string remains unchanged.
    

    Example 3:

    Input:str = "abcdefg",left = 1, right = 2
    Output:"gabcdef"
    Explanation:The left offset is 1, the right offset is 2, and the total offset is right 1. Therefore, the original string moves to the left and becomes "g" + "abcdef".

    class Solution:
        """
        @param str: A String
        @param left: a left offset
        @param right: a right offset
        @return: return a rotate string
        """
        def RotateString2(self, str, left, right):
            # write your code here
            length = len(str)
            
            if left == right:
                return str
            elif left > right:
                left_offset = (left - right) % length
                return str[left_offset: ] + str[: left_offset]
            else:
                right_offset = (right - left ) % length
                return str[length - right_offset: ] + str[: length - right_offset]
  • 相关阅读:
    第二阶段个人博客八
    第二阶段个人博客七
    第二阶段个人博客六
    第二阶段个人博客五
    第二阶段个人博客四
    第十五周学习进度表
    如何选择合适的图表类型
    参加技术会议的一些小窍门
    提高Scrum站会效率的一个小工具
    用6个字符写出任意的Javascript代码
  • 原文地址:https://www.cnblogs.com/yunxintryyoubest/p/14131330.html
Copyright © 2011-2022 走看看