๋ฐ์ํ
๋ฌธ์ |
Given a valid (IPv4) IP address
, return a defanged version of that IP address.
A defanged IP address replaces every period "."
with "[.]"
.
Example 1:
Input: address = "1.1.1.1" Output: "1[.]1[.]1[.]1"
Example 2:
Input: address = "255.100.50.0" Output: "255[.]100[.]50[.]0"
Constraints:
- The given
address
is a valid IPv4 address.
ํ์ด๊ณผ์ |
1.๊ท์น
- IPv4 ์ฃผ์์ธ ์๋ฐฐ์ด์์ . ์ ์ ๋ถ [.] ๋ก ๋์ฒดํฉ๋๋ค.
2.์์
address๋ฅผ ๋ณต์ฌํ ์ string๋ณ์๋ฅผ ๋ง๋ค์ด ์ค ํ . ๋ง [.] ๋ก ๋ฐ๊ฟ์ค๋๋ค.
๋๋ string::replace๋ฅผ ์ด์ฉํ์ฌ ์๋ฌธ์์ด ๋ด์์ . ์ [.] ๋ฐ๊พธ์ด ์ค๋๋ค.
3.์ฝ๋
class Solution { public: string defangIPaddr(string address) { string answer; int len = address.length(); for (int i = 0; i < len; i++) answer += (address.at(i) == '.') ? "[.]" : address.substr(i, 1); return answer; } }; | cs |
๋๋
string defangIPaddr(string address) { for (int i = 0; i < address.length();i++) if (address.at(i) == '.') address.replace(i++, 1, "[.]"); return address; } | cs |
๋ฐ์ํ