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

[pull] master from youngyangyang04:master #532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 3 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Mar 4, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
给0054.替换数字.md 加入python解法
  • Loading branch information
iqna126 authored Jan 16, 2025
commit 1d0333f59634bc68b9c4f253f95a2ef8f7a5009e
40 changes: 40 additions & 0 deletions problems/kamacoder/0054.替换数字.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,46 @@ public class Main {
}
```

### Python:
```python
class Solution(object):
def subsitute_numbers(self, s):
"""
:type s: str
:rtype: str
"""

count = sum(1 for char in s if char.isdigit()) # 统计数字的个数
expand_len = len(s) + (count * 5) # 计算扩充后字符串的大小, x->number, 每有一个数字就要增加五个长度
res = [''] * expand_len

new_index = expand_len - 1 # 指向扩充后字符串末尾
old_index = len(s) - 1 # 指向原字符串末尾

while old_index >= 0: # 从后往前, 遇到数字替换成"number"
if s[old_index].isdigit():
res[new_index-5:new_index+1] = "number"
new_index -= 6
else:
res[new_index] = s[old_index]
new_index -= 1
old_index -= 1

return "".join(res)

if __name__ == "__main__":
solution = Solution()

while True:
try:
s = input()
result = solution.subsitute_numbers(s)
print(result)
except EOFError:
break

```

### Go:
````go
package main
Expand Down

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