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 ...
随机推荐
- Mysql 表分区分类
针对Mysql数据库,表分区类型简析. [1]表分区类型 (1)Range分区:按范围分区.按列值的范围区间进行分区存储:比如:id小于10存储在一个分区:id大于10小于20存储在另外一个分区: ( ...
- Java使用正则表达式匹配多行 Pattern flags
Java中正则匹配有多种模式,若不选择模式则默认为单行匹配 匹配模式(Pattern flags) compile()方法有两个模式 未开匹配模式 Pattern compile(String reg ...
- 60 网络编程(二)——URL
认识URI.URL.URN 详细请参考:https://blog.51cto.com/xoyabc/1905492 URI:uniform resource Indent 统一资源标识符 URL:un ...
- [SOJ #721]第三送分题(2019-11-14考试)/[CF675E]Trains and Statistic
题目大意 在一条直线上有\(n\)个点.在第\(i\)个点可以花费\(1\)的代价到达\((i,a_i]\)中任意一点,用\(S[i][j]\)表示从点\(i\)到点\(j\)的最少花费,求\(\su ...
- Java.util.Math类--数学相关的工具类
Math类--数学相关的工具类 java.util.Math类是数学相关的工具类,里面提供了大量的静态方法,完成与数学运算相关的操作. public static double abs(double ...
- NModbus4 读取串口设备数值
使用NModbus4 读取串口 public static void aget() { byte[] array = new byte[8]; using (SerialPort port = new ...
- ex_gcd求不定方程的最小正整数解
#include<bits/stdc++.h> using namespace std; int gcd(int a,int b) {return b?gcd(b,a%b):a;} int ...
- 单词diamaund钻石diamaund英文
Diamond Di"a*mond (?; 277), n. [OE. diamaund, the hardest iron, steel, diamond, Gr. . Perh. the ...
- Js编程实践
js编程实践思维导向图 ---欢迎收藏 ^ - ^
- 深入理解 ValueTask
深入理解 ValueTask .NET Framework 4 里面的命名空间为 System.Threading.Tasks的 Task 类.这个类以及它派生的 Task<TResult> ...