题目如下:

解题思路:本题首先要很快速的计算出任意一个字符shift后会变成哪个字符,其实也很简单,让shift = shift % 26,接下来再做计算。第二部是求出每个字符要shift的次数。可以得出S[0]的shift次数等于sum(shifts),S[1]的次数等于sum(shifts)-shifts[0],S[2]的次数等于sum(shifts)-shifts[0]-shifts[1]...依次类推。这个规律可以保证整个算法的复杂度是O(n)。

代码如下:

class Solution(object):
def getShift(self,c,shift):
shift = shift % 26
nextc = ord(c) + shift
if nextc > ord('z'):
nextc -= ord('z')
nextc -= 1
nextc += ord('a')
return chr(nextc) def shiftingLetters(self, S, shifts):
"""
:type S: str
:type shifts: List[int]
:rtype: str
"""
amount = 0
for i in shifts:
amount += i
res = ''
for i in xrange(len(S)):
res += self.getShift(S[i],amount)
amount -= shifts[i]
return res

【leetcode】848. Shifting Letters的更多相关文章

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

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

  2. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

  3. 【leetcode】Find All Anagrams in a String

    [leetcode]438. Find All Anagrams in a String Given a string s and a non-empty string p, find all the ...

  4. 848.Shifting Letters——weekly contest 87

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

  5. 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)

    [LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  6. 【LeetCode】91. Decode Ways 解题报告(Python)

    [LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...

  7. 【LeetCode】880. Decoded String at Index 解题报告(Python)

    [LeetCode]880. Decoded String at Index 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  8. 【LeetCode】833. Find And Replace in String 解题报告(Python)

    [LeetCode]833. Find And Replace in String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...

  9. 【LeetCode】392. Is Subsequence 解题报告(Python)

    [LeetCode]392. Is Subsequence 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/is-subseq ...

随机推荐

  1. 移动端自动化==>什么是Appium

    转自:http://www.imdsx.cn/ 手机App分为两大类,原生App(Native App)和混合APP(Hybrid App) 原生App(Native App) 原生App实际就是我们 ...

  2. 【Spring】---【IOC】

    Spring 2017-08-15 08:25:57 [IOC] 分享几篇好文 谈谈对Spring IOC的理解 Spring的IOC原理(转载) java框架篇---spring IOC 实现原理 ...

  3. 模态框——angular

    ui-bootstrap-tpls.js库 $uibModal服务 $uibModalInstance服务 一.在angular中应用modal $uibModal 使用方法:直接注入到控制器中. . ...

  4. C++:函数求阶乘(如有不好之处还请谅解)

    #include<iostream> using namespace std; long long f1(int n); int main() { int n=0; cin>> ...

  5. 【Qt开发】Qt中图像的显示与基本操作

    Qt可显示基本的图像类型,利用QImage.QPxmap类可以实现图像的显示,并且利用类中的方法可以实现图像的基本操作(缩放.旋转). 1. Qt可显示的图像类型 参考Qt的帮助文档,可支持的类型,即 ...

  6. [转帖]linux screen 命令详解,xshell关掉窗口或者断开连接,查看断开前执行的命令

    linux screen 命令详解,xshell关掉窗口或者断开连接,查看断开前执行的命令 https://binwaer.com/post/12.html yun install -y screen ...

  7. 防抖和节流 lodash插件

    lodash.debounce lodash.debounce(function(){ },1000) 函数防抖原理 调用函数时,马上清理定时器.然后再设置一个定时器包含函数

  8. Luogu P5330 [SNOI2019]数论

    题目 如果\(P>Q\)的话我们先交换一下\(P,Q\). 我们先枚举所有满足第一个条件的数,对于\(x\equiv a_i(mod\ P)\),设\(x=a_i+kP(k\in[0,\lflo ...

  9. P3158 [CQOI2011]放棋子(dp+组合数)

    P3158 [CQOI2011]放棋子 放棋子的顺序和方案数无关,所以可以从按颜色递推 设$f[u][p][k]$为放到第$u$种颜色,所剩空间$p*k$的方案数 $g[u][i][j]$表示第$u$ ...

  10. SpringAOP用到了什么代理,以及动态代理与静态代理的区别

    spring aop (面向切面)常用于数据库事务中,使用了2种代理. jdk动态代理:对实现了接口的类生成代理对象.要使用jdk动态代理,要求类必须要实现接口. cglib代理:对类生成代理对象. ...