848. Shifting Letters**

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

题目描述

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 S, x 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 <= S.length = shifts.length <= 20000
  • 0 <= shifts[i] <= 10 ^ 9

C++ 实现 1

这题本身并不难, 关键在于要防止 overflow. 拿例子来说, 对于 c, 需要移动 9 次, 而对 b 来说, 需要移动 9 + 5 = 14 次, 而 a 需要移动 9 + 5 + 3 = 17 次. 对 26 (26 个小写字母) 求余. 但这是 shifts 中的值较小的情况. 如果 shifts 中的值很大, 从后向前累加, 可能会造成整数 overflow. 这时, 需要提前求余:

class Solution {private:char shift(const char &c, int n) {char res = (c - 'a' + n) % 26 + 'a';return res;}
public:string shiftingLetters(string S, vector<int>& shifts) {// 累加和的时候就应该求余for (int i = shifts.size() - 2; i >= 0; -- i)shifts[i] = (shifts[i + 1] + shifts[i]) % 26;for (int i = 0; i < S.size(); ++ i)S[i] = shift(S[i], shifts[i]);return S;}
};

原理在于公式: 可以参看 974. Subarray Sums Divisible by K**

(a + b) mod n = [(a mod n) + (b mod n)] mod n
(a mod n) mod n = a mod n

n - 3 这个位置的公式推导:

C++ 实现 2

也可以写成如下的形式:

class Solution {private:char shift(const char &c, int n) {char res = (c - 'a' + n) % 26 + 'a';return res;}
public:string shiftingLetters(string S, vector<int>& shifts) {int prev = 0;for (int i = S.size() - 1; i >= 0; -- i) {prev = (prev + shifts[i] % 26) % 26;// 注意和 C++ 实现 1 不同的是, 这里的第二个参数是 prevS[i] = shift(S[i], prev);}return S;}
};

848. Shifting Letters**相关推荐

  1. LeetCode 848. Shifting Letters

    原题目:https://leetcode-cn.com/problems/shifting-letters/ 思路: 逆序进行,sum记录逆序的和,该值就是S[i]要shift的值. 代码: clas ...

  2. Shifting Letters

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

  3. leetcode刷题规划

    LeetCode精华题目列表[刷题规划系列] – TuringPlanet 目录 算法题到底在考察什么? 题目列表 Array String Linked List Queue Stack Advan ...

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

    原文地址:https://www.cnblogs.com/grandyang/p/4606334.html 终于将LeetCode的大部分题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开 ...

  5. Codeforces Problem 708A Letters Cyclic Shift(implementation)

    此文章可以使用目录功能哟↑(点击上方[+]) 比赛链接→AIM Tech Round 3 (Div. 1)  Codeforces Problem 708A Letters Cyclic Shift ...

  6. 【LeetCode】字符串 string(共112题)

    [3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...

  7. LeetCode笔记:Biweekly Contest 85

    LeetCode笔记:Biweekly Contest 85 1. 题目一 1. 解题思路 2. 代码实现 2. 题目二 1. 解题思路 2. 代码实现 3. 题目三 1. 解题思路 2. 代码实现 ...

  8. IEEE signal processing letters 投稿经验

    转自:http://emuch.net/t.php?tid=6226942 前段时间比较幸运地中了一篇spl,把自己浅薄的经验写出来,直接从自己博客上转过来,分享给大家,望抛砖引玉吧~~~ 从投稿到录 ...

  9. Soil Ecology Letters被ESCI收录

    2021年5月5日,Soil Ecology Letters 被Emerging Source Citation Index(ESCI)数据库正式收录.这是创刊1年多之后,本刊入选的又一国际重要数据库 ...

最新文章

  1. linux 文本 查看 搜索
  2. python入门只需20分钟-史上最详细python学习路线-从入门到精通,只需5个月时间...
  3. ELK(ElasticSearch+Logstash+ Kibana)搭建实时日志分析平台
  4. php无法连接mysql_php无法连接数据库
  5. SD卡移植FAT32文件系统无MBR
  6. ad域同步其他ldap账号_域渗透——普通用户权限获得DNS记录
  7. pythonjava app切出后无网络连接_Python爬虫爬资源时由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。怎么破?...
  8. HTML学习二_HTML常用的行级标签,常用实体字符及表单标签
  9. 【Kafka】Kafka BrokerEndPointNotAvailableException: End point with security protocol PLAINTEXT not
  10. java框架之SpringBoot(5)-SpringMVC的自动配置
  11. 一文读懂 KMP 算法 | 原力计划
  12. 玩转Bootstrap(JS插件篇)-第1章 模态弹出框 :1-2 动画过渡
  13. java反编译的语句_Java开发网 - 请教,java反编译的问题
  14. android自定义application,Android Test Orchestrator和自定义Application类
  15. 中国移动MM7 API用户手册
  16. 实现数据结构中的栈---后进先出LIFO
  17. miui11稳定版获取完整root_怎么获取root权限-MIUI11系统开启系统ROOT权限图文教程-支持小米红米全部机型...
  18. Microsoft Power Apps部署方案
  19. linux git 命令备忘
  20. Sanitize检测的几种常见问题

热门文章

  1. 敏之澳电商:入驻拼多多开店流程及费用多少?
  2. linux卡死怎么办
  3. 第一篇Blog,随便写一点吧:)
  4. 微创脑科学通过上市聆讯:年营收3.8亿 年内盈利降47%
  5. 《计算机应用基础》课程计划,计算机应用基础课程教学计划
  6. Word图片保存后失真(变模糊)解决方法
  7. 通俗易懂、细致入微讲解卡尔曼滤波
  8. 如何给图片降噪?看完你就学会了
  9. flv f4v mp4 视频播放器代码
  10. 如何清除计算机搜索框内的搜索历史记录,如何清除搜索框中的网站访问历史记录...