Created
September 27, 2020 13:43
-
-
Save 2hanX/151782067e523c45abe9446d92989d51 to your computer and use it in GitHub Desktop.
给你一个有效的 IPv4 地址 address,返回这个 IP 地址的无效化版本。 所谓无效化 IP 地址,其实就是用 "[.]" 代替了每个 "."。 示例 1: 输入:address = "1.1.1.1"
输出:"1[.]1[.]1[.]1" 示例 2: 输入:address = "255.100.50.0"
输出:"255[.]100[.]50[.]0" 来源:力扣(LeetCode)
链接:https://leetcode-cn.co
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include <string.h> | |
using namespace std; | |
string defangIPaddr(string address) | |
{ | |
for (int i = 0; i < address.length(); ++i) | |
{ | |
if (address[i] == '.') | |
{ | |
address.replace(i,1,"[.]"); | |
i+=2; | |
} | |
} | |
return address; | |
} | |
int main(int argc, char const *argv[]) | |
{ | |
string address; | |
cin>>address; | |
string ans; | |
ans = defangIPaddr(address); | |
cout<<ans<<endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment