原题链接在这里: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 Sx 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. 1 <= S.length = shifts.length <= 20000
  2. 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);
}
}

类似Group Shifted Strings.

LeetCode 848. Shifting Letters的更多相关文章

  1. 【LeetCode】848. Shifting Letters 解题报告(Python)

    [LeetCode]848. Shifting Letters 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  2. 848.Shifting Letters——weekly contest 87

    848. Shifting Letters 题目链接:https://leetcode.com/problems/shifting-letters/description/ 思路:O(N^2)复杂度过 ...

  3. 【leetcode】848. Shifting Letters

    题目如下: 解题思路:本题首先要很快速的计算出任意一个字符shift后会变成哪个字符,其实也很简单,让shift = shift % 26,接下来再做计算.第二部是求出每个字符要shift的次数.可以 ...

  4. 848. Shifting Letters

    问题描述: 问题规约为:对每一个数组S,移动(shifts[0] + shitfs[1]+...+shitfs[i] )mod 26位 def shiftingLetters(self, S: str ...

  5. [LeetCode] Shifting Letters 漂移字母

    We have a string S of lowercase letters, and an integer array shifts. Call the shift of a letter, th ...

  6. [Swift]LeetCode848. 字母移位 | Shifting Letters

    We have a string S of lowercase letters, and an integer array shifts. Call the shift of a letter, th ...

  7. [LeetCode] Remove Duplicate Letters 移除重复字母

    Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...

  8. LeetCode Remove Duplicate Letters

    原题链接在这里:https://leetcode.com/problems/remove-duplicate-letters/ 题目: Given a string which contains on ...

  9. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

随机推荐

  1. spring boot Websocket

    本文只作为个人笔记,大部分代码是引用其他人的文章的. 参考: https://blog.csdn.net/moshowgame/article/details/80275084 在springboot ...

  2. Django 路由正则URL

    Django 路由正则URL URL1 # 路由 url(r'^detail/', views.detail) {#点击跳转到指定用户下显示信息#} <li><a target=&q ...

  3. jboss/wildfly安全域的密码加密和解密

    加密: java_path=$(source /opt/wildfly/bin/.Beta1.jar:/opt/wildfly/modules/system/layers/base/org/jboss ...

  4. 深入理解 Linux Cgroup 系列(一):基本概念

    原文链接:深入理解 Linux Cgroup 系列(一):基本概念 Cgroup 是 Linux kernel 的一项功能:它是在一个系统中运行的层级制进程组,你可对其进行资源分配(如 CPU 时间. ...

  5. (一)使用phantomjs将动态HTML页面生成图片

    因为工作需要,笔者需要将一个动态的HTML5页面生成图片,并将图片发送给用户. 其中难点在于怎样将动态H5生成图片 笔者翻阅资料后,决定使用phantomjs这个插件,关于这个插件的安装,非常简单,笔 ...

  6. linux或者shell进入vi命令

    vi的基本操作 a) 进入vi     在系统提示符号输入vi及文件名称后,就进入vi全屏幕编辑画面: $ vi file  不过有一点要特别注意,就是您进入vi之后,是处于「命令行模式(comman ...

  7. ASP.NET MVC 允许跨域请求设置

    场景:创建一个图片上传的站点,用于其他站点跨域上传附件和图片之类. 上传插件结合百度的 webuploader.js 经常会碰到,跨域的问题,如下, 处理方式呢,是在web.config 中配置允许跨 ...

  8. SpringCloud-Consul开发环境配置

    一.consul安装 1.下载:https://www.consul.io/downloads.html: 2.选择版本:本人开发环境是windows,所以选择win64: 3.安装:保存至D:/Sp ...

  9. python爬虫User Agent用户代理

    UserAgent简介 UserAgent中文名为用户代理,是Http协议中的一部分,属于头域的组成部分,UserAgent也简称UA.它是一个特殊字符串头,是一种向访问网站提供你所使用的浏览器类型及 ...

  10. 【转载】C#的DataTable使用NewRow方法创建新表格行

    在C#的DataTable数据表格操作过程中,DataRow类表示DataTable中的数据行信息,但DataRow没有可以直接实例化的构造方法,在创建DataTable的新行的时候,不可直接使用Da ...