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 ...
随机推荐
- Docker在linux系统下的安装
系统要求 本安装教程仅限于CentOS7,其他系统不适用.centos-extras仓库必须是启用状态,这个仓库默认状态是启用,如果不是启用状态,请修改. 卸载旧版本的Docker Docker的旧版 ...
- 300iq Contest 1 简要题解
300iq Contest 1 简要题解 咕咕咕 codeforces A. Angle Beats description 有一张\(n\times m\)的方阵,每个位置上标有*,+,.中的一种. ...
- 论文笔记:LightGBM: A Highly Efficient Gradient Boosting Decision Tree
引言 GBDT已经有了比较成熟的应用,例如XGBoost和pGBRT,但是在特征维度很高数据量很大的时候依然不够快.一个主要的原因是,对于每个特征,他们都需要遍历每一条数据,对每一个可能的分割点去计算 ...
- Gearman介绍、原理分析、实践改进
gearman是什么? 它是分布式的程序调用框架,可完成跨语言的相互调用,适合在后台运行工作任务.最初是2005年perl版本,2008年发布C/C++版本.目前大部分源码都是(Gearmand服务j ...
- 颜色rgba和16进制
今天阅读代码的时候看到了一个实现颜色渐变的效果,不同于以往使用函数实现的颜色渐变,这个是规律的递增rgba里面的几个参数完成的,看起来就像是等差数列一样.没想到还能这样来,简单的了解了一下 rgba的 ...
- drf之组件(认证、权限、排序、过滤、分页等)和xadmin、coreapi
认证Authentication 可以在配置文件中配置全局默认的认证方案 REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_fr ...
- 使用 sql server 默认跟踪分析执行的 SQL 语句
如果没有启用 SQL SERVER 的跟踪器来跟踪 SQL SERVER 的 SQL 执行情况,又想查最近的 SQL 执行情况,网上一般说是使用 LogExprorer 这个工具,网上找了这个工具很久 ...
- CSS3 完善盒模型
CSS3 改善了传统盒模型结构,增强了盒子构成要素的功能,扩展了盒模型显示的方式. 改善结构:为盒子新增轮廓区: 增强功能:内容区增强 CSS 自动添加内容功能,增强内容移除.换行处理:允许多重定义背 ...
- dm9000网卡 S3C2440
配置U-Boot支持dm9000网卡 原理图 # vi drivers/net/Makefile obj-$(CONFIG_DRIVER_NET_CS8900) += cs8900.o obj-$(C ...
- 量化金融策略开源框架:QUANTAXIS
简介: QUANTAXIS量化金融策略框架,是一个面向中小型策略团队的量化分析解决方案,是一个从数据爬取.清洗存储.分析回测.可视化.交易复盘的本地一站式解决方案. QUANTAXIS量化金融策略框架 ...