We have a string S of lowercase letters, and an integer array shifts.

Call the shift of a letter, the next letter in the alphabet, (wrapping around so that 'z' becomes 'a').

For example, shift('a') = 'b'shift('t') = 'u', and shift('z') = 'a'.

Now for each shifts[i] = x, we want to shift the first i+1 letters of Sx times.

Return the final string after all such shifts to S are applied.

Example 1:

Input: S = "abc", shifts = [3,5,9]
Output: "rpl"
Explanation:
We start with "abc".
After shifting the first 1 letters of S by 3, we have "dbc".
After shifting the first 2 letters of S by 5, we have "igc".
After shifting the first 3 letters of S by 9, we have "rpl", the answer.

Note:

  1. 1 <= S.length = shifts.length <= 20000
  2. 0 <= shifts[i] <= 10 ^ 9

这道题让我们对字母进行漂移,给了一个 shifts 数组,里面是对应对需要漂移值,但是需要注意的是,shifts[i] 表示对于原字符串 [0, i] 范围内的所有的字符都进行 shifts[i] 的漂移,那么实际上第一个字母其实把 shifts 数组所有的数字都漂移了一遍,而第二个字母则是把 shifts 数组从第二个数字开始到最后的所有数字都漂移了,而最后一个字母就只漂移了最后一个数字。这不就是一个反向累加和数组么,只要建立了反向累加和数组,那么每个位子上的数字就是对应的字母的漂移值了。为了节省空间,我们就不另建数组了,直接在 shifts 数组上累加就行了,注意累加值要对 26 取余,因为累加和数组可能会整型溢出,取余后就不会有这个问题,而且由于字母漂移 2 6次后,都会回到原来的位置,所以对 26 取余并不会影响到最后的结果。

反向累加和数组建立好了之后,就要开始对字母进行漂移了,这里还有个需要注意的地方,不能直接用原字母加上漂移值,因为一旦超过了 'z' 的时候,是需要从 'a' 重新的开始的,为了处理所有的情况,可以使用一个很常用的 trick,就是先算出字母到 'a' 之间的距离,然后加上漂移值,再对 26 取余,这就是漂移后与 'a' 的距离了,再加上 'a' 变成字母即可,参见代码如下:

class Solution {
public:
string shiftingLetters(string S, vector<int>& shifts) {
for (int i = (int)shifts.size() - ; i >= ; --i) {
shifts[i] = (shifts[i] + shifts[i + ]) % ;
}
for (int i = ; i < shifts.size(); ++i) {
S[i] = (S[i] - 'a' + shifts[i]) % + 'a';
}
return S;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/848

参考资料:

https://leetcode.com/problems/shifting-letters/

https://leetcode.com/problems/shifting-letters/discuss/137906/C%2B%2BJavaPython-Easy-Understood

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Shifting Letters 漂移字母的更多相关文章

  1. 【LeetCode】848. Shifting Letters 解题报告(Python)

    [LeetCode]848. Shifting Letters 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  2. 848.Shifting Letters——weekly contest 87

    848. Shifting Letters 题目链接:https://leetcode.com/problems/shifting-letters/description/ 思路:O(N^2)复杂度过 ...

  3. 前端与算法 leetcode 242. 有效的字母异位词

    目录 # 前端与算法 leetcode 242. 有效的字母异位词 题目描述 概要 提示 解析 解法一:哈希表 解法二:数组判断字符出现次数 解法三:转换字符串 算法 传入测试用例的运行结果 执行结果 ...

  4. [Swift]LeetCode848. 字母移位 | Shifting Letters

    We have a string S of lowercase letters, and an integer array shifts. Call the shift of a letter, th ...

  5. LeetCode 848. Shifting Letters

    原题链接在这里:https://leetcode.com/problems/shifting-letters/ 题目: We have a string S of lowercase letters, ...

  6. 【leetcode】848. Shifting Letters

    题目如下: 解题思路:本题首先要很快速的计算出任意一个字符shift后会变成哪个字符,其实也很简单,让shift = shift % 26,接下来再做计算.第二部是求出每个字符要shift的次数.可以 ...

  7. [LeetCode] Letter Case Permutation 字母大小写全排列

    Given a string S, we can transform every letter individually to be lowercase or uppercase to create ...

  8. 【LeetCode】763-划分字母区间

    title: 763-划分字母区间 date: 2019-04-15 21:10:46 categories: LeetCode tags: 字符串 贪心思想 双指针 题目描述 字符串 S 由小写字母 ...

  9. 领扣(LeetCode)检测大写字母 个人题解

    给定一个单词,你需要判断单词的大写使用是否正确. 我们定义,在以下情况时,单词的大写用法是正确的: 全部字母都是大写,比如"USA". 单词中所有字母都不是大写,比如"l ...

随机推荐

  1. 移动端,input、textarea滚动至可视区域

    1.一般情况下 在移动端,点击input框之后,会弹出输入键盘.而内容input的内容也会自动滚动到可视区域内. 2.当父元素设置了overflow属性之后 在设置了overflow属性之后,点击in ...

  2. Java 多线程 - Synchronized关键字

    目录 1-Synchronized 关键字概述 2- Synchronized关键字作用域 3- Synchronized 原理(反编译指令解释) 正文 1-Synchronized 关键字概述 由于 ...

  3. 使用scrapy爬虫,爬取今日头条搜索吉林疫苗新闻(scrapy+selenium+PhantomJS)

    这一阵子吉林疫苗案,备受大家关注,索性使用爬虫来爬取今日头条搜索吉林疫苗的新闻 依然使用三件套(scrapy+selenium+PhantomJS)来爬取新闻 以下是搜索页面,得到吉林疫苗的搜索信息, ...

  4. HTTP协议详解(三)

    接着第二篇继续学习... 6 HTTP的几个重要概念 6.1连接:Connection 一个传输层的实际环流,它是建立在两个相互通讯的应用程序之间. 在http1,request和reponse头中都 ...

  5. Maven Install报错:Perhaps you are running on a JRE rather than a JDK?

    我用的是idea,解决办法是:安装jdk,配置环境变量

  6. Deep face recognition: a survey v4

    http://www.cnblogs.com/shouhuxianjian/p/9789243.html

  7. 关于PCB走线能不能走锐角的讨论

    (此文参考吴川斌的博客) 很多PCB工程师都知道Layout走线时忌走直角,那么锐角能走吗? 回答当然是否定的!为什么呢? 这里先不说锐角对高速信号走线会不会造成负面影响,单从PCB DFM(可制造性 ...

  8. Vuejs自定义select2指令

    在做select2插件的时候遇到一些坑,最终解决如下: Vue.directive('select2', { inserted: function (el, binding, vnode) { var ...

  9. About The Order of The Declarations And Definition When Making a Member Function a Friend.关于使类成员成为另一个类友元函数的声明顺序和定义。

    If only member function clear of WindowMgr is a friend of Screen, there are some points need to note ...

  10. Android进阶:四、RxJava2 源码解析 1

    本文适合使用过Rxjava2或者了解Rxjava2的基本用法的同学阅读 一.Rxjava是什么 Rxjava在GitHub 主页上的自我介绍是 "a library for composin ...