leetcode_1427 字符串的左右移

163 阅读1分钟

要求

给定一个包含小写英文字母的字符串 s 以及一个矩阵 shift,其中 shift[i] = [direction, amount]:

  • direction 可以为 0 (表示左移)或 1 (表示右移)。
  • amount 表示 s 左右移的位数。
  • 左移 1 位表示移除 s 的第一个字符,并将该字符插入到 s 的结尾。
  • 类似地,右移 1 位表示移除 s 的最后一个字符,并将该字符插入到 s 的开头。 对这个字符串进行所有操作后,返回最终结果。 

示例 1:

输入:s = "abc", shift = [[0,1],[1,2]]
输出:"cab"
解释:
[0,1] 表示左移 1 位。 "abc" -> "bca"
[1,2] 表示右移 2 位。 "bca" -> "cab"

示例 2:

输入:s = "abcdefg", shift = [[1,1],[1,1],[0,2],[1,3]]
输出:"efgabcd"
解释: 
[1,1] 表示右移 1 位。 "abcdefg" -> "gabcdef"
[1,1] 表示右移 1 位。 "gabcdef" -> "fgabcde"
[0,2] 表示左移 2 位。 "fgabcde" -> "abcdefg"
[1,3] 表示右移 3 位。 "abcdefg" -> "efgabcd"

提示:

  • 1 <= s.length <= 100
  • s 只包含小写英文字母
  • 1 <= shift.length <= 100
  • shift[i].length == 2
  • 0 <= shift[i][0] <= 1
  • 0 <= shift[i][1] <= 100

核心代码

class Solution:
    def stringShift(self, s: str, shift: List[List[int]]) -> str:
        n = len(s)
        left_shift = 0
        for direction,dist in shift:
            if direction == 0:
                left_shift += dist
            else:
                left_shift -= dist
        left_shift %= n
        if left_shift == 0:
            return s
        return s[left_shift:] + s[:left_shift]

image.png

解题思路:其实这道题就是对字符串的切片的考量,我们先使用循环将向左的偏移量计算出来,进行切片拼接即可。