题1108、IP 地址无效化

一、题目

给你一个有效的 IPv4 地址 address,返回这个 IP 地址的无效化版本。
所谓无效化 IP 地址,其实就是用 “[.]” 代替了每个 “.”。1

示例 1:

输入:address = “1.1.1.1”
输出:“1[.]1[.]1[.]1”\

示例 2:

输入:address = “255.100.50.0”
输出:“255[.]100[.]50[.]0”

提示:给出的 address 是一个有效的 IPv4 地址

二、思路

三、代码

public class T1108 {

    public static void main(String[] args) {

        System.out.println( defangIPaddr( "2.1.1.1" ) );
        System.out.println( defangIPaddr( "255.100.50.0" ) );
    }

    public static String defangIPaddr(String address) {

        StringBuffer output = new StringBuffer();

        for ( int i = 0; i < 3; i++ ) {

            int index = address.indexOf(".");
            output.append( address.substring( 0, index ) );
            output.append( "[.]" );
            address = address.substring( index+1 );

        }

        output.append( address );

        return output.toString();
    }
}

  1. 来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/defanging-an-ip-address
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 ↩︎

发布了25 篇原创文章 · 获赞 0 · 访问量 113

猜你喜欢

转载自blog.csdn.net/weixin_45980031/article/details/103466203