current position:Home>[algorithm learning] 1108 IP address invalidation (Java / C / C + + / Python / go / trust)

[algorithm learning] 1108 IP address invalidation (Java / C / C + + / Python / go / trust)

2022-01-31 02:22:25 White hat of the second leader

This is my participation 11 The fourth of the yuegengwen challenge 3 God , Check out the activity details :2021 One last more challenge

Thank you very much for reading this article ~ welcome 【 give the thumbs-up 】【 Collection 】【 Comment on 】~ It's not hard to give up , But persistence must be cool ~ I hope all of us can make a little progress every day ~ This paper is written by The white hat of the second leader :https://juejin.cn/user/2771185768884824/posts Original blog ~


1108. IP Address invalidation :

Give you an effective IPv4 Address address, Go back to this IP Invalid version of address .

The so-called invalidation IP Address , In fact, it is to use "[.]" Instead of every ".".

Examples 1

 Input :
    address = "1.1.1.1"

 Output :
    "1[.]1[.]1[.]1"
 Copy code 

Examples 2

 Input :
    address = "255.100.50.0"

 Output :
    "255[.]100[.]50[.]0"
 Copy code 

Tips

  • Given address Is an effective IPv4 Address

analysis

  • This algorithm problem two is in charge. I believe everyone can do it , I don't seem to have anything to say .
  • In fact, the translation of the title is to . Replace all with [.] .
  • except C and C++ You can focus on the following , Others are basically brought with the language API.

Answer key

java

class Solution {
    public String defangIPaddr(String address) {
        return address.replace(".","[.]");
    }
}
 Copy code 

c

The prompt says the entered address Is an effective IPv4 Address , That means there must be three . Need to replace with [.] , You can know that the returned result is more than the input parameters 6 Characters ,strlen The length returned does not contain hidden characters at the end of the string '\0'.

char * defangIPaddr(char * address){
    int n = strlen(address);
    char *ans = malloc(n + 7);
    for (int i = 0, j = 0; i < n; ++i) {
        if (address[i] == '.') {
            ans[j++] = '[';
            ans[j++] = '.';
            ans[j++] = ']';
        } else {
            ans[j++] = address[i];
        }
    }
    ans[n + 6] = '\0';
    return ans;
}
 Copy code 

c++

Can't find a one-time replacement for all API, Here is the reverse order to replace , Why? ? If it's a positive sequence , After replacement . Your position will move back , You need to move the subscript , Otherwise, there will be a dead cycle .

class Solution {
public:
    string defangIPaddr(string address) {
        for (int i = address.size(); i >= 0; --i) {
            if (address[i] == '.') {
                address.replace(i, 1, "[.]");
            }
        }
        return address;
    }
};
 Copy code 

python

class Solution:
    def defangIPaddr(self, address: str) -> str:
        return address.replace('.', '[.]')
 Copy code 

go

func defangIPaddr(address string) string {
    return strings.ReplaceAll(address, ".", "[.]")
}
 Copy code 

rust

impl Solution {
    pub fn defang_i_paddr(address: String) -> String {
        address.replace(".", "[.]")
    }
}
 Copy code 

 Insert picture description here


Original title transmission gate :https://leetcode-cn.com/problems/defanging-an-ip-address/


copyright notice
author[White hat of the second leader],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201310222237136.html

Random recommended