【LeetCode】880. Decoded String at Index 解题报告(Python)
【LeetCode】880. Decoded String at Index 解题报告(Python)
标签(空格分隔): LeetCode
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/decoded-string-at-index/description/
题目描述:
An encoded string S is given. To find and write the decoded string to a tape, the encoded string is read one character at a time and the following steps are taken:
If the character read is a letter, that letter is written onto the tape.
If the character read is a digit (say d), the entire current tape is repeatedly written d-1 more times in total.
Now for some encoded string S, and an index K, find and return the K-th letter (1 indexed) in the decoded string.
Example 1:
Input: S = "leet2code3", K = 10
Output: "o"
Explanation:
The decoded string is "leetleetcodeleetleetcodeleetleetcode".
The 10th letter in the string is "o".
Example 2:
Input: S = "ha22", K = 5
Output: "h"
Explanation:
The decoded string is "hahahaha". The 5th letter is "h".
Example 3:
Input: S = "a2345678999999999999999", K = 1
Output: "a"
Explanation:
The decoded string is "a" repeated 8301530446056247680 times. The 1st letter is "a".
Note:
- 2 <= S.length <= 100
- S will only contain lowercase letters and digits 2 through 9.
- S starts with a letter.
- 1 <= K <= 10^9
- The decoded string is guaranteed to have less than 2^63 letters.
题目大意
从左到右是一个编码了的字符串,如果某个位置是字符,那么拼接到前面的字符串上去;如果某个位置是数字,那么将左边所有的字符串*这个数字的倍数,变成新的字符串。
解题方法
如果模拟题目中的操作进行解码,空间占用过大,一定会通不过。而且,题目只要求了求指定位置的字符,并没有要求所有的字符,所以全部解码没有必要。
参考了官方的解答。思路如下:
比如,对于一个解码了的字符串,appleappleappleappleappleapple
,并且要求的索引K=24的话,那么结果和K=4是一样的。因为单词apple
的size=5,重复了6次。所以第K个索引和第K%size个索引是一样的。
所以我们使用反向的计算,保持追踪解码字符串的size,如果解码字符串等于一个word
重复了d
次的时候,我们可以把K
变化为K % (word.length)
.
算法:
首先,找出解码字符串的长度。然后,我们反向查找,保持追踪size,也就是对编码字符串S[0], S[1], ..., S[i]
解码后的长度。
如果找到的是一个数字S[i]
,那么意味着解码字符串S[0], S[1], ..., S[i-1]
的长度应该是size / Integer(S[i])
;否则应该是size-1
.
代码如下:
class Solution(object):
def decodeAtIndex(self, S, K):
"""
:type S: str
:type K: int
:rtype: str
"""
size = 0
for c in S:
if c.isdigit():
size *= int(c)
else:
size += 1
for c in reversed(S):
K %= size
if K == 0 and c.isalpha():
return c
if c.isdigit():
size /= int(c)
else:
size -= 1
参考资料:https://leetcode.com/problems/decoded-string-at-index/solution/
日期
2018 年 8 月 23 日 ———— 疲惫说明在逆流而上
【LeetCode】880. Decoded String at Index 解题报告(Python)的更多相关文章
- [LeetCode] 880. Decoded String at Index 在位置坐标处解码字符串
An encoded string S is given. To find and write the decoded string to a tape, the encoded string is ...
- 【LeetCode】784. Letter Case Permutation 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 循环 日期 题目地址:https://leet ...
- 【LeetCode】724. Find Pivot Index 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和,再遍历 日期 题目地址:https://le ...
- 【LeetCode】398. Random Pick Index 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 每次遍历索引 字典保存索引 蓄水池抽样 日期 题目地 ...
- 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】760. Find Anagram Mappings 解题报告
[LeetCode]760. Find Anagram Mappings 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/find ...
- 【LeetCode】Pascal's Triangle II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
- 【LeetCode】474. Ones and Zeroes 解题报告(Python)
[LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
随机推荐
- Perl哈希%hash
哈希是 key/value 键/值对的集合. Perl中哈希变量以百分号 (%) 标记开始. 访问哈希元素格式:${key}. 以下是一个简单的哈希实例: 实例 #!/usr/bin/perl %da ...
- 【模板】单源最短路径(Dijkstra)/洛谷P4779
题目链接 https://www.luogu.com.cn/problem/P4779 题目大意 给定一个 \(n\) 个点 \(m\) 条边有向图,每个点有一个非负权值,求从 \(s\) 点出发,到 ...
- Set、Map、WeakSet 和 WeakMap 的区别
先总结: Set1. 成员不能重复2. 只有健值,没有健名,有点类似数组.3. 可以遍历,方法有add, delete,hasweakSet 1. 成员都是对象 2. 成员都是弱引用,随时可以消失. ...
- 打破砂锅问到底!HTTP和HTTPS详解
HTTP 引自维基百科HTTP:超文本传输协议(英文:HyperText Transfer Protocol,缩写:HTTP)是一种用于分布式.协作式和超媒体信息系统的应用层协议.HTTP是万维网的数 ...
- int是几位;short是几位;long是几位 负数怎么表示
其实可以直接通过stm32的仿真看到结果:(这里是我用keil进行的测试,不知道这种方法是否准确) 从上面看, char是8位 short是4*4=16位 int是8*4=32位 long是8* ...
- 容器之分类与各种测试(三)——queue
queue是单端队列,但是在其实现上是使用的双端队列,所以在queue的实现上多用的是deque的方法.(只要用双端队列的一端只出数据,另一端只进数据即可从功能上实现单端队列)如下图 例程 #incl ...
- vim使用配置(转)
在终端下使用vim进行编辑时,默认情况下,编辑的界面上是没有行号的.语法高亮度显示.智能缩进等功能的. 为了更好的在vim下进行工作,需要手动配置一个配置文件: .vimrc 在启动vim时,当前用户 ...
- Oracle中分割逗号函数REGEXP_SUBSTR
最近优化FORM中的查询条件遇到某个字段可以选取多个值的问题,思路当然就是选取时将多个值通过某个符号拼接起来,查询数据的时候将拼接后的字符串按照符号分割开,在分割逗号的时候用到了一个新的方法REGEX ...
- jquery datatable真实示例
1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncodin ...
- java实现文件压缩
java实现文件压缩:主要是流与流之间的传递 代码如下: package com.cst.klocwork.service.zip; import java.io.File; import java. ...