【LeetCode】848. Shifting Letters 解题报告(Python)
【LeetCode】848. Shifting Letters 解题报告(Python)
标签(空格分隔): LeetCode
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.me/
题目地址:https://leetcode.com/problems/shifting-letters/description/
题目描述:
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
题目大意
(这个题本身简单,但是读懂题目很重要)
给出了一个字符串S,以及和这个字符串等长的数组shifts。定义了一个shift操作:把某个字符在字母表上移动某位(字母’z’再向右移得到’a’)。现在遍历shifts,每个操作都是把当前位数之前的所有字符移动shift位。求最后得到的字符串。
解题方法
坑还是挺明显的:需要把当前位数之前的所有字符串都去shift操作。看出题目给的字符串挺长的,如果普通的遍历,在每次遍历的时候再把之前所有shift过的再次shift,那么就会超时。
所以正确的做法是先求出每个字符串需要shift的次数。即对shifts进行位置之后的求和。得出要shift的位数之后,按照题目给的那种循环去操作就好了。
(应该没有人傻到真的去循环,而不是用求余操作吧233,逃……)
class Solution(object):
def shiftingLetters(self, S, shifts):
"""
:type S: str
:type shifts: List[int]
:rtype: str
"""
_len = len(S)
shifts_sum = sum(shifts)
shifts_real = []
for shift in shifts:
shifts_real.append(shifts_sum)
shifts_sum -= shift
def shift_map(string, shift_time):
shifted = ord(s) + (shift_time % 26)
return chr(shifted if shifted <= ord('z') else shifted - 26)
ans = ''
for i, s in enumerate(S):
ans += shift_map(s, shifts_real[i])
return ans
日期
2018 年 6 月 10 日 ———— 等了两天的腾讯比赛复赛B的数据集,结果人家在复赛刚开始就给了。。
【LeetCode】848. Shifting Letters 解题报告(Python)的更多相关文章
- LeetCode 848. Shifting Letters
原题链接在这里:https://leetcode.com/problems/shifting-letters/ 题目: We have a string S of lowercase letters, ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- 848.Shifting Letters——weekly contest 87
848. Shifting Letters 题目链接:https://leetcode.com/problems/shifting-letters/description/ 思路:O(N^2)复杂度过 ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
随机推荐
- 43-Reverse Nodes in k-Group
Reverse Nodes in k-Group My Submissions QuestionEditorial Solution Total Accepted: 58690 Total Submi ...
- BAT的一些题
114.java中实现多态的机制是什么 答:重写,重载.方法的重写Overriding和重载Overloading是Java多态性的不同表现. 重写Overriding是父类与子类之间多态性的一种表 ...
- Oracle--计算某一日期为一年中的第几周
我自己实现的脚本: select T31267.CREATED_DATE as F31265, (select to_char(to_date(T31267.CREATED_DATE,'yyyy-mm ...
- 关于vim复制剪贴粘贴命令的总结-转
最近在使用vim,感觉很好很强大,但是在使用复制剪切粘贴命令是,碰到了一些小困惑,网上找了一些资料感觉很不全,讲的也不好,遂自己进行实践并总结了. 首先是剪切(删除): 剪切其实也就顺带删除了所选择的 ...
- SpringBoot集成Kafka的实战用法大全
本文是SpringBoot+Kafka的实战讲解,如果对kafka的架构原理还不了解的读者,建议先看一下<大白话kafka架构原理>.<秒懂kafka HA(高可用)>两篇文章 ...
- A Child's History of England.23
King William, fearing he might lose his conquest, came back, and tried to pacify the London people b ...
- A Child's History of England.31
The English in general were on King Henry's side, though many of the Normans were on Robert's. But t ...
- pyqt5的下拉菜单,可以进行输入文字
- 转 序列化Serializable和Parcelable的区别详解
什么是序列化,为什么要进行序列化 答:对象要进行传输(如:activity 与activity间 ,网络间 进程间等等).存储到本地就必须进行序列化 . 这种可传输的状态就是序列化. 怎么序列化??两 ...
- 在应用程序中的所有其他bean被销毁之前执行一步工作
1.实现ServletContextListener.ApplicationContextAware两个接口,在销毁方法里借助ApplicationContextAware注入的application ...