【leetcode】848. Shifting Letters
题目如下:

解题思路:本题首先要很快速的计算出任意一个字符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的更多相关文章
- 【LeetCode】848. Shifting Letters 解题报告(Python)
[LeetCode]848. Shifting Letters 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
- 【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 ...
- 848.Shifting Letters——weekly contest 87
848. Shifting Letters 题目链接:https://leetcode.com/problems/shifting-letters/description/ 思路:O(N^2)复杂度过 ...
- 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)
[LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】91. Decode Ways 解题报告(Python)
[LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
- 【LeetCode】880. Decoded String at Index 解题报告(Python)
[LeetCode]880. Decoded String at Index 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【LeetCode】833. Find And Replace in String 解题报告(Python)
[LeetCode]833. Find And Replace in String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...
- 【LeetCode】392. Is Subsequence 解题报告(Python)
[LeetCode]392. Is Subsequence 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/is-subseq ...
随机推荐
- LoadRunner之检查点
一.什么是检查点 LoadRunner中检查点是用来判断脚本是否执行成功的.如果不加检查点,只要服务器返回的HTTP状态码是200,VuGen就认为脚本执行通过了.但是很多情况下服务器返回200并不代 ...
- Ubuntu vimrc 和 bashrc 配置
先上效果图,把vimrc 和bashrc 备份一下.. vimrc: map <F9> :call SaveInputData()<CR> func! SaveInputDat ...
- 【Qt开发】状态栏设置
1.在Qt 里面,状态栏显示的信息有三种类型:临时信息.一般信息和永久信息. 其中,临时信息指临时显示的信息,比如QAction 的提示等,也可以设置自己的 临时信息,比如程序启动之后显示Read ...
- Golang中的error类型
Golang中的error类型 error类型本身就是一个预定义好的接口,里面定义了一个method type error interface { Error() string } 生成一个新的err ...
- 关于SQL连接语句中的Integrated Security=SSPI/ture/false
解决方法:即:Security Support Provider Interface设置Integrated Security为 True 的时候,连接语句前面的 UserID, PW 是不起作用的, ...
- linux下安装msgpack,yar,phalcon
安装msgpack扩展 下载:http://pecl.php.net/package/msgpack cd /usr/local tar zxvf msgpack-0.5.5.tgz cd msgpa ...
- Mysql中explain作用详解
一.MYSQL的索引 1.索引(Index):帮助Mysql高效获取数据的一种数据结构.用于提高查找效率,可以比作字典.可以简单理解为排好序的快速查找的数据结构.2.索引的作用:便于查询和排序(所以添 ...
- bootstrap使用总结(导航在carousel居中之上)
在导航中想实现这样 carousel 在底部,导航条在上面中间,div结构为以下 <div class="navbar-wrapper"style="width: ...
- C++关于erase的复杂度(转载)
被这个问题困扰了很多次,有必要整理一下. 当然最好的参考资料就是http://www.cplusplus.com/reference/set/set/erase/ 里的Complexcity部分了,但 ...
- 拼接HTML代码在UIWebVIew中显示
其原理:通过网络请求获得相关的信息,再通过手机端进行拼HTML,然后在WebView进行展示,此处还对文章中的图片增加点击效果,可以保存到相册中:文章的样式已经存在项目中,直接去调用: 1:首先了解两 ...