Skip to content

Commit

Permalink
880
Browse files Browse the repository at this point in the history
  • Loading branch information
ProHiryu committed Jul 1, 2020
1 parent fc2611e commit 505c82d
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions String/880.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
## Decoded String at Index

#### Description

[link](https://leetcode.com/problems/decoded-string-at-index/)

---

#### Solution

- See Code

---

#### Code

O(n)

```python
class Solution:
'''
先求出刚好超过K的重复字符串长度,再往前寻找对应的第K个字符
当当前c为数字时,用N除去对应c,K同时也对N取余,等于在后续位置进行子问题查找
'''
def decodeAtIndex(self, S: str, K: int) -> str:
N = 0
for i, c in enumerate(S):
N = N * int(c) if c.isdigit() else N + 1
if K <= N: break
for j in range(i, -1, -1):
c = S[j]
if c.isdigit():
N /= int(c)
K %= N
else:
if K == N or K == 0: return c
N -= 1
```

0 comments on commit 505c82d

Please sign in to comment.