zoukankan      html  css  js  c++  java
  • 848. 字母移位

    有一个由小写字母组成的字符串 S,和一个整数数组 shifts。
    
    我们将字母表中的下一个字母称为原字母的 移位(由于字母表是环绕的, 'z' 将会变成 'a')。
    
    例如·,shift('a') = 'b', shift('t') = 'u',, 以及 shift('z') = 'a'。
    
    对于每个 shifts[i] = x , 我们会将 S 中的前 i+1 个字母移位 x 次。
    
    返回将所有这些移位都应用到 S 后最终得到的字符串。
    
    示例:
    
    输入:S = "abc", shifts = [3,5,9]
    输出:"rpl"
    解释: 
    我们以 "abc" 开始。
    将 S 中的第 1 个字母移位 3 次后,我们得到 "dbc"。
    再将 S 中的前 2 个字母移位 5 次后,我们得到 "igc"。
    最后将 S 中的这 3 个字母移位 9 次后,我们得到答案 "rpl"。
    提示:
    
    1 <= S.length = shifts.length <= 20000
    0 <= shifts[i] <= 10 ^ 9

    解题:

    class Solution {
    public:
        string shiftingLetters(string S, vector<int>& shifts) {
            int n = shifts.size();
            vector<int> sum;
            int cnt = 0;
            
            for(int i = n-1; i>= 0; --i){
                cnt = (cnt + (shifts[i]%26))%26;
                sum.push_back(cnt);
            }
            
            reverse(sum.begin(),sum.end());
            
            for(int i = 0;i < n; ++i){
                //cout<<sum[i]<<endl;
                S[i] = (S[i] + sum[i] - 'a')%26 + 'a';
            }
            
            
            return S;
        }
    };

    有一个由小写字母组成的字符串 S,和一个整数数组 shifts

    我们将字母表中的下一个字母称为原字母的 移位(由于字母表是环绕的, 'z' 将会变成 'a')。

    例如·,shift('a') = 'b', shift('t') = 'u',, 以及 shift('z') = 'a'

    对于每个 shifts[i] = x , 我们会将 S 中的前 i+1 个字母移位 x 次。

    返回将所有这些移位都应用到 S 后最终得到的字符串。

    示例:

    输入:S = "abc", shifts = [3,5,9]
    输出:"rpl"
    解释: 
    我们以 "abc" 开始。
    将 S 中的第 1 个字母移位 3 次后,我们得到 "dbc"。
    再将 S 中的前 2 个字母移位 5 次后,我们得到 "igc"。
    最后将 S 中的这 3 个字母移位 9 次后,我们得到答案 "rpl"。
    

    提示:

    1. 1 <= S.length = shifts.length <= 20000
    2. 0 <= shifts[i] <= 10 ^ 9
  • 相关阅读:
    OnMeasureItem和OnDrawItem的区别和联系
    DockPanel 类
    C# 源码 AForge.NET
    ystem.Windows.Forms.SplitContainer : ContainerControl, ISupportInitialize
    System.Windows.Forms.Control : Component, IOleControl, IOleObject, IOleInPlaceObject, IOleInPlaceActiveObject....
    System.ComponentModel.Component : MarshalByRefObject, IComponent, IDisposable
    System.Windows.Forms.ListView : Control
    vs2013 密钥_
    系统封装 EasyBoot如何将WIN7安装版提取到光盘
    系统封装 ES3使用方法
  • 原文地址:https://www.cnblogs.com/mikemeng/p/9185499.html
Copyright © 2011-2022 走看看