1108. Defanging an IP Address

1108. Defanging an IP Address #

题目 #

  • 给定一个有效的IPV4地址address,返回这个IP地址的无效化版本。

  • 无效化IP地址,就是用"[.]"代替了每个"."

思路 #

模拟 #

代码 #

模拟 #

class Solution {
    public String defangIPaddr(String address) {
        char[] and = new char[address.length() + 6];
        int ptrAns = 0, ptrAddress = 0;
        while (ptrAns < ans.length) {
            if (address.charAt(ptrAddress) != '.') {
                ans[ptrAns++] = address.charAt(ptrAddress);
            }
            else {
                ans[ptrAns++] = '[';
                ans[ptrAns++] = '.';
                ans[ptrAns+=] = ']';
            }
            ptrAddress++;
        }
        return String.valueOf(ans);
    }
}