릿코드(LEETCODE)

defanging-an-ip-address

cepiloth 2020. 2. 3. 16:02
반응형

https://leetcode.com/problems/defanging-an-ip-address/

 

Defanging an IP Address - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

단순 치환 문제

좀더 이쁘게 하는 방법이 없으려나

 

class Solution {
public:
    string defangIPaddr(string address) {
        string s;

        for (int i = 0; i < address.size(); i++)
        {
            char ch = address[i];
            if (ch == '.') {
                s.push_back('[');
                s.push_back('.');
                s.push_back(']');
            }
            else {
                s.push_back(ch);
            }
        }

        return s;
    }
};

 

반응형