Loading...

09-27 leetcode 0880

链接 880. Decoded String at Index

题目

You are given an encoded string s. To decode the 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 d, the entire current tape is repeatedly written d - 1 more times in total.
Given an integer k, return the kth letter (1-indexed) in the decoded string.

题解

这题没啥好说的。。直接暴力写就行(

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution:
def decodeAtIndex(self, s: str, k: int) -> str:
length = 0
for char in s:
if char.isdigit():
length *= int(char)
else:
length += 1
for char in reversed(s):
k %= length
if k == 0 and char.isalpha():
return char
if char.isdigit():
length //= int(char)
else:
length -= 1
return ""

Comment