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

2022-01-31 02:22:25

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 ：

Output ：
"1[.]1[.]1[.]1"
Copy code ``````

# Examples 2

`````` Input ：

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.

## java

``````class Solution {
}
}
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){
char *ans = malloc(n + 7);
for (int i = 0, j = 0; i < n; ++i) {
ans[j++] = '[';
ans[j++] = '.';
ans[j++] = ']';
} else {
}
}
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:
for (int i = address.size(); i >= 0; --i) {
}
}
}
};
Copy code ``````

## python

``````class Solution:
Copy code ``````

## go

``````func defangIPaddr(address string) string {
}
Copy code ``````

## rust

``````impl Solution {
}
}
Copy code ``````