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 <= 200000 <= 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 ...
随机推荐
- keil 编译器V6 定义函数在ram中运行-和在指定地址定义常量
之前一直是用v5编译,编译速度慢,换成V6编译速度差不多快50% ,而且arm后期只维护v5编译器不在更新v5编译器.切换到V6编译器大势所趋,把之前v5且换到v6需要如下更改: 1. CMSIS包需 ...
- 「NOI2018」冒泡排序
「NOI2018」冒泡排序 考虑冒泡排序中一个位置上的数向左移动的步数 \(Lstep\) 为左边比它大的数的个数,向右移动的步数 \(Rstep\) 为右边比它大的数的个数,如果 \(Lstep,R ...
- 2019-08-01 JQuery事件
Jquery简单的事件 l blur(fn) 当失去焦点时 l change(fn) 当内容发生改变时 l click(fn) 当鼠标单击时 l dblclick 当鼠标双击时 l focus(fn) ...
- 使用input的file进行上传进行预览
在使用file上传文件的时候,想到了图片预览的功能,然后查询了一些资料,一种是需要后端配合,将数据变成base64或者buff等数据传给后端然后调取接口进行显示,但是这种需要后端的配合和网络请求,感觉 ...
- Vue学习之组件切换及父子组件小结(八)
一.组件切换: 1.v-if与v-else方式: <!DOCTYPE html> <html lang="en"> <head> <met ...
- CSS 过渡动画
一.过渡 过渡(transition)是CSS3中具有颠覆性的特征之一,可以在不使用 Flash 动画或 JavaScript 的情况下,当元素从一种样式变换为另一种样式时为元素添加效果. 在CSS3 ...
- grub破解和bios加密
grub破解通过单用户模式,可以实现修改密码 grub加密以后,只能通过bios解除grub密码,方法如下 进入bios 修改启动方式,从CD启动 加载系统镜像,原系统默认挂载到/mnt/sysima ...
- FastJson--阿里开源的速度最快的Json和对象转换工具 https://www.cnblogs.com/kaituorensheng/p/8082631.html
import java.util.ArrayList;import java.util.List;import java.util.HashMap;import java.util.Map; impo ...
- Keras上实现简单线性回归模型
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/marsjhao/article/detai ...
- Linux基础:sort命令总结
本文只总结一些常用的用法,更详细的说明见man sort和sort --help. sort命令 sort命令用于串联排序指定文件并将结果写到标准输出. sort可以指定按照何种排序规则进行排序,如按 ...