-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |