-
Notifications
You must be signed in to change notification settings - Fork 43
/
decoded-string-at-index.py
54 lines (49 loc) · 1.14 KB
/
decoded-string-at-index.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# V0
# V1
# https://blog.csdn.net/fuxuemingzhu/article/details/81977433
# https://blog.csdn.net/qq_26440803/article/details/84261151
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
# V2
# Time: O(n)
# Space: O(1)
class Solution(object):
def decodeAtIndex(self, S, K):
"""
:type S: str
:type K: int
:rtype: str
"""
i = 0
for c in S:
if c.isdigit():
i *= int(c)
else:
i += 1
for c in reversed(S):
K %= i
if K == 0 and c.isalpha():
return c
if c.isdigit():
i /= int(c)
else:
i -= 1