给你一个有效的 IPv4[2] 地址 address
,返回这个 IP 地址的无效化版本。
所谓无效化 IP 地址,其实就是用 "[.]"
代替了每个 "."
。
示例 1:
输入: address = "1.1.1.1"
输出: "1[.]1[.]1[.]1"
示例 2:
输入: address = "255.100.50.0"
输出: "255[.]100[.]50[.]0"
replace()
方法;package string;
/**
* Created with IntelliJ IDEA.
* Version : 1.0
* Author : cunyu
* Email : cunyu1024@foxmail.com
* Website : https://cunyu1943.github.io
* Date : 2020/4/3 17:34
* Project : LeetCode
* Package : string
* Class : OneOneZeroEight
* Desc : 1108. IP 地址无效化
*/
public class OneOneZeroEight {
public static void main(String[] args) throws Exception{
OneOneZeroEight oneOneZeroEight = new OneOneZeroEight();
String address = "192.168.2.231";
System.out.println(oneOneZeroEight.defangIPaddr(address));
}
public String defangIPaddr(String address) {
return address.replace(".", "[.]");
}
}
[1]
1108. IP 地址无效化: https://leetcode-cn.com/problems/defanging-an-ip-address/
[2]
IPv4: https://baike.baidu.com/item/IPv4