0
点赞
收藏
分享

微信扫一扫

LeetCode 1427. 字符串的左右移

代码小姐 2022-04-21 阅读 80

LeetCode 1427. 字符串的左右移

文章目录

题目描述

给定一个包含小写英文字母的字符串 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"

LeetCode 1427. 字符串的左右移
提示:

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

一、解题关键词


二、解题报告

1.思路分析

2.时间复杂度

3.代码示例

class Solution {
    public String stringShift(String s, int[][] shift) {
        int moves = 0;
        for (var arr : shift) {
            if (arr[0] == 0) {
                moves -= arr[1];
            }
            else {
                moves += arr[1];
            }
        }
        moves %= s.length();
        if (moves <= 0) {
            return s.substring(-moves) + s.substring(0, -moves);
        }
        else {
            return s.substring(s.length()-moves) + s.substring(0, s.length()-moves);
        }
    }
}

2.知识点

抽象问题的本质  不用单步操作 本质就是移动字符位置 
1、统计
2、进行截取位移

总结

相同题目

xxx

举报

相关推荐

0 条评论