Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f602c30

Browse files
🐱(string): 848. 字母移位
1 parent 319de8b commit f602c30

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

‎docs/data-structure/string/README.md‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,6 +1466,39 @@ class Solution:
14661466
return res
14671467
```
14681468

1469+
## 848. 字母移位
1470+
1471+
[原题链接](https://leetcode-cn.com/problems/shifting-letters/)
1472+
1473+
### 思路
1474+
1475+
越靠前的字母需要移动次数越多,可以算出每个字母需要移动的总次数,然后再对字母进行统一移动处理。时间复杂度为 $O(n)$。
1476+
1477+
```python
1478+
class Solution:
1479+
def shiftingLetters(self, S: str, shifts: List[int]) -> str:
1480+
length = len(shifts)
1481+
1482+
# 每个字母要移动的总次数
1483+
num = [0 for _ in range(length)]
1484+
num[length - 1] = shifts[length - 1]
1485+
for i in range(length - 2, -1, -1):
1486+
num[i] = shifts[i] + num[i + 1]
1487+
1488+
s_list = list(S)
1489+
ord_a = ord('a')
1490+
ord_z = ord('z')
1491+
for i in range(length):
1492+
shift = num[i] % 26
1493+
c = s_list[i]
1494+
ord_res = ord(c) + shift
1495+
if ord_res > ord_z:
1496+
ord_res = ord_res - ord_z - 1 + ord_a
1497+
s_list[i] = chr(ord_res)
1498+
1499+
return "".join(s_list)
1500+
```
1501+
14691502
## 1108. IP 地址无效化
14701503

14711504
[原题链接](https://leetcode-cn.com/problems/defanging-an-ip-address/)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /