• leetcode Perform String Shifts


    You are given a string s containing lowercase English letters, and a matrix shift, where shift[i] = [direction, amount]:

    • direction can be 0 (for left shift) or 1 (for right shift). 
    • amount is the amount by which string s is to be shifted.
    • A left shift by 1 means remove the first character of s and append it to the end.
    • Similarly, a right shift by 1 means remove the last character of s and add it to the beginning.

    Return the final string after all operations.

    Example 1:

    Input: s = "abc", shift = [[0,1],[1,2]]
    Output: "cab"
    Explanation: 
    [0,1] means shift to left by 1. "abc" -> "bca"
    [1,2] means shift to right by 2. "bca" -> "cab"

    Example 2:

    Input: s = "abcdefg", shift = [[1,1],[1,1],[0,2],[1,3]]
    Output: "efgabcd"
    Explanation:  
    [1,1] means shift to right by 1. "abcdefg" -> "gabcdef"
    [1,1] means shift to right by 1. "gabcdef" -> "fgabcde"
    [0,2] means shift to left by 2. "fgabcde" -> "abcdefg"
    [1,3] means shift to right by 3. "abcdefg" -> "efgabcd"

    Constraints:

    • 1 <= s.length <= 100
    • s only contains lower case English letters.
    • 1 <= shift.length <= 100
    • shift[i].length == 2
    • 0 <= shift[i][0] <= 1
    • 0 <= shift[i][1] <= 100

    题目大意:给定一个只包含小写字母的字符串s和一个转移(shift)矩阵,其中shift[i] = [direction, amount]:

    direction只能是0或者1,0表示字符串左移,1表示右移,

    amount表示字符串s的移动的数量

    左移1次表示将s的第一个字符移除,并将其插入(append)到字符串尾

    同样的,右移1次表示将s的最后一个字符移除,并将其插入(append)到字符串首

    最后返回经过所有操作后的串。

    思路:假设字符串长度为len,首先明确几个规律:

    1)右移或者左移len位相当于不移动

    2)左移num位相当于右移len-num位

    3)左移num位再右移num位相当于原字符串不移动,所以本题可以一次性统计,算出最后右移的位数(左移的位数用右移来抵消)

    4)右移num位,常规做法就是模拟,一位一位右移,计算复杂度为O(num * len), 一种做法是翻转字符串前len-num个字符,翻转最后num个字符,最后再翻转整个字符,复杂度为O(len):reverse(s[:len-num]), reverse(s[len-num:], reverse(s)

    C++代码:

     1 class Solution {
     2 public:
     3     string stringShift(string s, vector<vector<int>>& shift) {
     4         int shift_right = 0; //shift_right记录总的右移位数
     5         string ans(s);
     6         for (auto shift_i : shift) {
     7             if (shift_i[0] == 0) //左移,shift_right来抵消左移,用减法
     8                 shift_right -= shift_i[1]; 
     9             else
    10                 shift_right += shift_i[1];
    11         }
    12         
    13         int len = ans.length();
    14         //shift_right = (shift_right - shift_right / len * len + len) % len; //此语句同时考虑shift_right正负两种情况,相当于下面两句
    15         shift_right = shift_right % len; //先消除len的整数倍的右移次数
    16         shift_right = shift_right < 0 ? shift_right + len : shift_right; //如果右移次数为负,转换成真正的右移
    17         int mid = len - shift_right;
    18         
    19         for (int i = 0, j = mid - 1; i < j; ++i, --j) //翻转后半部分字符串
    20             swap(ans[i], ans[j]);
    21         for (int i = mid, j = len - 1; i < j; ++i, --j) //翻转前半部分字符串
    22             swap(ans[i], ans[j]);
    23         for (int i = 0, j = len - 1; i < j; ++i, --j) //翻转整个字符串
    24             swap(ans[i], ans[j]);
    25         return ans;
    26     }
    27 };

    python3代码

     1 class Solution:
     2     def stringShift(self, s: str, shift: List[List[int]]) -> str:
     3         shift_right = 0
     4         for x, y in shift:
     5             if x == 0:
     6                 shift_right -= y
     7             else:
     8                 shift_right += y
     9         shift_right %= len(s)
    10         return s[-shift_right:] + s[:-shift_right]
     
  • 相关阅读:
    NET中的类型和装箱/拆箱原理
    转 C# 装箱和拆箱[整理]
    理解线程同步
    IsBackground的理解
    赛马会面试题
    FTP上传类
    FTPS加密上传
    转载WPF SDK研究 之 AppModel
    SQL Server查看错误日志存档编号及其详情
    Hive基础编程入门(一)
  • 原文地址:https://www.cnblogs.com/qinduanyinghua/p/12724971.html
Copyright © 2020-2023  润新知