본문 바로가기

old/Algorithm Solving

LeetCode 1108. Defanging an IP Address 자바 문제 풀이

반응형

문제

Defanging an IP Address - LeetCode

문제 해결 방법

  • 입력받은 ip address에서 .을 [.]으로 바꾸는 문제입니다.
  • 스트링의 replace 메서드를 알고 있으면 쉽게 바꿀수 있습니다. replace메서드를 알고 있냐고 묻는 문제 입니다.

Github Link

https://github.com/eunhanlee/LeetCode_1108_DefanginganIPAddress_Solution.git

replace 시간복잡도: O(n), 공간복잡도: O(1)

class Solution {
/**
     * 주어진 IP 주소에서 마침표를 지정된 대체 문자열로 대체하여 수정합니다.
     *
     * @param address 주소를 수정할 IP 주소
     * @return 수정된 IP 주소
     */
    public String defangIPaddr(String address) {
        return address.replace(".","[.]");
    }
}

참고

자바 스트링 replace와 replaceAll의 차이점

반응형