Skip to content

Latest commit

 

History

History
44 lines (39 loc) · 1.06 KB

File metadata and controls

44 lines (39 loc) · 1.06 KB

1108. IP 地址无效化

https://leetcode-cn.com/problems/defanging-an-ip-address/

Java

/*
 * @Author: Goog Tech
 * @Date: 2020-08-09 07:42:42
 * @LastEditTime: 2020-08-09 07:43:54
 * @Description: https://leetcode-cn.com/problems/defanging-an-ip-address/
 * @FilePath: \leetcode-googtech\#1108. Defanging an IP Address\Solution.java
 */

class Solution {
    public String defangIPaddr(String address) {
        StringBuffer sb = new StringBuffer();
        char[] chars = address.toCharArray();
        for(char c : chars) {
            sb = c == '.' ? sb.append("[.]") : sb.append(c);
        }
        return sb.toString();
    }
}

Python

'''
Author: Goog Tech
Date: 2020-08-09 07:42:47
LastEditTime: 2020-08-09 07:43:15
Description: https://leetcode-cn.com/problems/defanging-an-ip-address/
FilePath: \leetcode-googtech\#1108. Defanging an IP Address\Solution.py
'''

class Solution(object):
    def defangIPaddr(self, address):
        """
        :type address: str
        :rtype: str
        """
        return address.replace('.', '[.]')