美文网首页
1108. Defanging an IP Address

1108. Defanging an IP Address

作者: 随时学丫 | 来源:发表于2020-01-01 18:13 被阅读0次

    1108. Defanging an IP Address

    Easy

    183503Share

    Given a valid (IPv4) IP address, return a defanged version of that IP address.

    A defanged IP address replaces every period "." with "[.]".

    Example 1:

    Input: address = "1.1.1.1"
    Output: "1[.]1[.]1[.]1"
    

    Example 2:

    Input: address = "255.100.50.0"
    Output: "255[.]100[.]50[.]0"
    

    Java

    class Solution {
        public String defangIPaddr(String address) {
            int len = address.length();
            String res = "";
            for(int i = 0; i<len; i++){
                char add = address.charAt(i);
                if(add == '.'){
                    res += "[.]";
                } else {
                    res += add;
                }
            }
            return res;
        }
    }
    

    Python

    class Solution:
        def defangIPaddr(self, address: str) -> str:
            res = ''
            for add in address:
                if add == '.':
                    res += '[.]'
                else:
                    res += add
            return res
    

    相关文章

      网友评论

          本文标题:1108. Defanging an IP Address

          本文链接:https://www.haomeiwen.com/subject/wurgoctx.html