848. Shifting Letters**
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**相关推荐
- LeetCode 848. Shifting Letters
原题目:https://leetcode-cn.com/problems/shifting-letters/ 思路: 逆序进行,sum记录逆序的和,该值就是S[i]要shift的值. 代码: clas ...
- Shifting Letters
We have a string S of lowercase letters, and an integer array shifts. Call the shift of a letter, th ...
- leetcode刷题规划
LeetCode精华题目列表[刷题规划系列] – TuringPlanet 目录 算法题到底在考察什么? 题目列表 Array String Linked List Queue Stack Advan ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
原文地址:https://www.cnblogs.com/grandyang/p/4606334.html 终于将LeetCode的大部分题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开 ...
- Codeforces Problem 708A Letters Cyclic Shift(implementation)
此文章可以使用目录功能哟↑(点击上方[+]) 比赛链接→AIM Tech Round 3 (Div. 1) Codeforces Problem 708A Letters Cyclic Shift ...
- 【LeetCode】字符串 string(共112题)
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...
- LeetCode笔记:Biweekly Contest 85
LeetCode笔记:Biweekly Contest 85 1. 题目一 1. 解题思路 2. 代码实现 2. 题目二 1. 解题思路 2. 代码实现 3. 题目三 1. 解题思路 2. 代码实现 ...
- IEEE signal processing letters 投稿经验
转自:http://emuch.net/t.php?tid=6226942 前段时间比较幸运地中了一篇spl,把自己浅薄的经验写出来,直接从自己博客上转过来,分享给大家,望抛砖引玉吧~~~ 从投稿到录 ...
- Soil Ecology Letters被ESCI收录
2021年5月5日,Soil Ecology Letters 被Emerging Source Citation Index(ESCI)数据库正式收录.这是创刊1年多之后,本刊入选的又一国际重要数据库 ...
最新文章
- linux 文本 查看 搜索
- python入门只需20分钟-史上最详细python学习路线-从入门到精通,只需5个月时间...
- ELK(ElasticSearch+Logstash+ Kibana)搭建实时日志分析平台
- php无法连接mysql_php无法连接数据库
- SD卡移植FAT32文件系统无MBR
- ad域同步其他ldap账号_域渗透——普通用户权限获得DNS记录
- pythonjava app切出后无网络连接_Python爬虫爬资源时由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。怎么破?...
- HTML学习二_HTML常用的行级标签,常用实体字符及表单标签
- 【Kafka】Kafka BrokerEndPointNotAvailableException: End point with security protocol PLAINTEXT not
- java框架之SpringBoot(5)-SpringMVC的自动配置
- 一文读懂 KMP 算法 | 原力计划
- 玩转Bootstrap(JS插件篇)-第1章 模态弹出框 :1-2 动画过渡
- java反编译的语句_Java开发网 - 请教,java反编译的问题
- android自定义application,Android Test Orchestrator和自定义Application类
- 中国移动MM7 API用户手册
- 实现数据结构中的栈---后进先出LIFO
- miui11稳定版获取完整root_怎么获取root权限-MIUI11系统开启系统ROOT权限图文教程-支持小米红米全部机型...
- Microsoft Power Apps部署方案
- linux git 命令备忘
- Sanitize检测的几种常见问题