【原題目】:

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.

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

 

【My Solution】:

1.PNG

 

本來想跑迴圈,然後進行判斷後做替換,但後來發現Java String有replace的寫法,看起來比較乾淨俐落

public String replace(char oldChar,
                      char newChar)

replace()方法透過用newChar取代字串中所有的oldchar

 

arrow
arrow
    文章標籤
    LeetCode Java String replace用法
    全站熱搜

    Mark Zhang 發表在 痞客邦 留言(0) 人氣()