LeetCode 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
题解:
First calculate how many steps should each char stift by accumlating the number from n-1 to 0.
Then, shift char by the corresponding step.
If it already past 'z', minus by 26.
Time Complexity: O(n). n = shifts.length.
Space: O(n).
AC Java:
class Solution {
public String shiftingLetters(String S, int[] shifts) {
if(S == null || S.length() == 0){
return S;
} int n = shifts.length;
int sum = 0;
for(int i = n-1; i>=0; i--){
sum = (sum+shifts[i])%26;
shifts[i] = sum;
} char [] arr = S.toCharArray();
for(int i = 0; i<n; i++){
arr[i] += shifts[i];
if(arr[i] > 'z'){
arr[i] -= 26;
}
} return new String(arr);
}
}
LeetCode 848. Shifting Letters的更多相关文章
- 【LeetCode】848. Shifting Letters 解题报告(Python)
[LeetCode]848. Shifting Letters 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- 848.Shifting Letters——weekly contest 87
848. Shifting Letters 题目链接:https://leetcode.com/problems/shifting-letters/description/ 思路:O(N^2)复杂度过 ...
- 【leetcode】848. Shifting Letters
题目如下: 解题思路:本题首先要很快速的计算出任意一个字符shift后会变成哪个字符,其实也很简单,让shift = shift % 26,接下来再做计算.第二部是求出每个字符要shift的次数.可以 ...
- 848. Shifting Letters
问题描述: 问题规约为:对每一个数组S,移动(shifts[0] + shitfs[1]+...+shitfs[i] )mod 26位 def shiftingLetters(self, S: str ...
- [LeetCode] Shifting Letters 漂移字母
We have a string S of lowercase letters, and an integer array shifts. Call the shift of a letter, th ...
- [Swift]LeetCode848. 字母移位 | Shifting Letters
We have a string S of lowercase letters, and an integer array shifts. Call the shift of a letter, th ...
- [LeetCode] Remove Duplicate Letters 移除重复字母
Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...
- LeetCode Remove Duplicate Letters
原题链接在这里:https://leetcode.com/problems/remove-duplicate-letters/ 题目: Given a string which contains on ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
随机推荐
- 关于vs无法创建虚拟目录的问题
插入链接:https://blog.csdn.net/zhao1955/article/details/92182935 补充:改完之后不要忘记以管理员的身份运行vs
- 对ssm框架里面的一些常用注解的理解
@Componcnt :作用就是把当前类对象存入spring容器中 属性:value 用于指定bean的id 当我们不写的时候默认就是当前类名,并且首字母要小写 ------------------- ...
- 基于OpenGL的三维曲面动态显示实现
在使用Visual C++的MFC AppWizard建立应用程序框架后,生成了多个类,与OpenGL编程相关的类是视图类,主要的显示任务都在其中完成. 1.基于OpenGL绘图的基本设置 1.1 设 ...
- Java 数组(三)二维数组
如果一维数组的各个元素仍然是一个数组,那么它就是一个二维数组.二维数组常用于表示表,表中的信息以行和列的形式组织,第一个下标代表元素所在的行,第二个下标代表所在的列. 一.二维数组的创建 1.先声明, ...
- sqlserver一次性修改多条
修改客户表 编号为 0101007002,0101007003的楼栋号 007-1-102,007-1-201 UPDATE gas_customerSET building= CASEWHEN g ...
- win2003下安装python3.4 + pyspider
昨天尝试了在win2003下安装python2.7.这个是文章地址:https://www.cnblogs.com/alpiny/p/11706606.html 但是程序跑了一晚上,发现有一点问题,是 ...
- mysql IFNULL
IFNULL(v1,v2); 其中:如果 v1 不为 NULL,则 IFNULL 函数返回 v1; 否则返回 v2 的结果.
- ThinkCMF_X1.6.0-X2.2.3框架任意内容包含漏洞的简单分析复现(附自动化验证脚本)
1.漏洞概述 攻击者可利用此漏洞构造恶意的url,向服务器写入任意内容的文件,达到远程代码执行的目的 2.影响版本 ThinkCMF X1.6.0 ThinkCMF X2.1.0 ThinkCMF X ...
- IDisposable 接口
提供一种用于释放非托管资源的机制. 地址:https://docs.microsoft.com/zh-cn/dotnet/api/system.idisposable?view=netframewor ...
- FFLIB
用于分布式程序的c++类库,封装了socket.rpc.lua.CQRS框架.算法等组件,适于SNS.WEBGAME.MMO后台程序, about C++,linux https://github.c ...