【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 ...
随机推荐
- 分布式任务队列 Celery —— 详解工作流
目录 目录 前文列表 前言 任务签名 signature 偏函数 回调函数 Celery 工作流 group 任务组 chain 任务链 chord 复合任务 chunks 任务块 mapstarma ...
- Chrome 浏览器添加跨域支持
开发前端本地项目时,涉及到与后端服务器的通信联调,在使用 ajax 时由于浏览器的安全策略不允许跨域.一种方式是本地搭建转发服务器,今天又 GET 到一种更直接的方式,在 Chrome 浏览器开启时添 ...
- C# user32.dll 找窗口 填数据
工具:SpyLite [DllImport("user32.dll", EntryPoint = "FindWindow")] private extern s ...
- WebMvcConfigurerAdapter详解和过时后的替代方案
一.什么是WebMvcConfigurerAdapter Spring内部的一种配置方式采用JavaBean的形式来代替传统的xml配置文件形式进行针对框架个性化定制 二.WebMvcConfigur ...
- 获取当前页面的title
#-*-coding:utf-8-*-from selenium import webdriverdriver = webdriver.Firefox()driver.get("https: ...
- Discrete Mathematics and Its Applications | 1 CHAPTER The Foundations: Logic and Proofs | 1.4 Predicates and Quantifiers
The statements that describe valid input are known as preconditions and the conditions that the outp ...
- swagger 访问格式
boot工程格式如下 http://10.166.10.169:8085/swagger-ui.html 非boot工程加个自己项目名 http://10.166.10.169:8085/xxx/sw ...
- 使用app.config中的数据对数据库链接信息初始化
看到别人数据库信息都是在app.config里面设置的,今天来尝试了一下,报了 "System.Configuration.ConfigurationSettings.AppSettings ...
- 语言I—2019秋作业02
这个作业属于那个课程 这个作业要求在哪里 我在这个课程的目标是 这个作业在那个具体方面帮助我实现目标 参考文献 C语言程序设计I https://edu.cnblogs.com/campus/zswx ...
- 1、Java语言概述与开发环境——JDK JRE JVM理解
一.理解概念: 1.JDK(Java Development Kit Java开发工具包) JDK是提供给Java开发人员使用的,其中包含Java的开发工具,也包括JRE,所以安装了JDK,就不用单独 ...