Leetcode 1108. Defanging an IP Address

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Defanging an IP Address

2. Solution

解析:Version 1,遍历替换字符即可。

  • Version 1
1
2
3
4
5
6
7
8
9
class Solution:
def defangIPaddr(self, address: str) -> str:
res = ''
for ch in address:
if ch == '.':
res += '[.]'
else:
res += ch
return res

Reference

  1. https://leetcode.com/problems/defanging-an-ip-address/
如果有收获,可以请我喝杯咖啡!