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

class Solution {public String shiftingLetters(String S, int[] shifts) {if(S==null||S.isEmpty())return S;StringBuilder sb=new StringBuilder(S);for(int i=0;i<sb.length();i++){for(int j=0;j<=i;j++){sb.setCharAt(j,(char)(((sb.charAt(j)-'a'+shifts[i])%26+'a')));}}return sb.toString();}
}

这道题使用StringBuffer时 时间超过限制Time Limit Exceeded,使用StringBuilder才能通过,通过的思路是在遍历shifts的时候,将它加到i之前的所有已经遍历过的字符上。

Shifting Letters相关推荐

  1. 848. Shifting Letters**

    848. Shifting Letters** https://leetcode.com/problems/shifting-letters/ 题目描述 We have a string S of l ...

  2. LeetCode 848. Shifting Letters

    原题目:https://leetcode-cn.com/problems/shifting-letters/ 思路: 逆序进行,sum记录逆序的和,该值就是S[i]要shift的值. 代码: clas ...

  3. Codeforces Problem 708A Letters Cyclic Shift(implementation)

    此文章可以使用目录功能哟↑(点击上方[+]) 比赛链接→AIM Tech Round 3 (Div. 1)  Codeforces Problem 708A Letters Cyclic Shift ...

  4. 【LeetCode】字符串 string(共112题)

    [3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...

  5. leetcode刷题规划

    LeetCode精华题目列表[刷题规划系列] – TuringPlanet 目录 算法题到底在考察什么? 题目列表 Array String Linked List Queue Stack Advan ...

  6. LeetCode All in One 题目讲解汇总(持续更新中...)

    原文地址:https://www.cnblogs.com/grandyang/p/4606334.html 终于将LeetCode的大部分题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开 ...

  7. LeetCode笔记:Biweekly Contest 85

    LeetCode笔记:Biweekly Contest 85 1. 题目一 1. 解题思路 2. 代码实现 2. 题目二 1. 解题思路 2. 代码实现 3. 题目三 1. 解题思路 2. 代码实现 ...

  8. IEEE signal processing letters 投稿经验

    转自:http://emuch.net/t.php?tid=6226942 前段时间比较幸运地中了一篇spl,把自己浅薄的经验写出来,直接从自己博客上转过来,分享给大家,望抛砖引玉吧~~~ 从投稿到录 ...

  9. Soil Ecology Letters被ESCI收录

    2021年5月5日,Soil Ecology Letters 被Emerging Source Citation Index(ESCI)数据库正式收录.这是创刊1年多之后,本刊入选的又一国际重要数据库 ...

最新文章

  1. 安装 Docker 一
  2. IDEA Spark程序报错处理
  3. python3 deque(双向队列)
  4. android view强制重绘_android view 相关方法 layout draw 布局 重绘 | 学步园
  5. Integer.valueOf(String) 方法之惑
  6. redis tomcat session
  7. thinkphp php5.4报错,ThinkPHP6.0在PHP8下报错解决方法
  8. 技术周刊(2019-02-11 Serverless)
  9. 【英语学习】【科学】【Glencoe Science】【D】Human Body Systems 目录及术语表
  10. java 获取路径_java 获取当前类的路径
  11. 英语发音规则---A字母
  12. springmvc + ehcache + redis+mybatis 分布式架构
  13. flv文件修复工具——FLVMDI的使用方法
  14. Hudi Timeline简析
  15. Tweet with Disaster(Kaggle NLP项目实战)
  16. 【狮子数学】chapter6-05-阿贝尔定理(第99讲)
  17. Windows安装配置Python Scrapy环境
  18. 事物认识分析基本方法(转)
  19. 【干货收藏】测试人员必看的经典书籍
  20. GoAhead2.5源代码分析之7-form请求处理(form.c)

热门文章

  1. 微服务设计指导-使用云原生微服务解决传统海量跑批时引起的系统间“级联雪崩”以及效率
  2. 锐捷路由器如何配置虚拟服务器,[路由器自动配置方法技巧] 锐捷路由器配置方法...
  3. 多个路由器无线桥接,共享网络
  4. PYsystem003 中职网络安全
  5. [解决]IDEA每次启动都会打开Licenses激活弹窗、IDEA打不开
  6. Android 11 新特性和API兼容
  7. CSS在Mac /Win上兼容显示
  8. 夜间环境人脸识别_基于人脸识别的夜间疲劳驾驶判断方法与流程
  9. UVA 488 Triangle Wave
  10. Vue+H5 活动内容调用微信分享