LeetCode 1108. Defanging an IP Address

原题链接在这里:https://leetcode.com/problems/defanging-an-ip-address/

题目:

Given a valid (IPv4) IP address, return a defanged version of that IP address.

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.

题解:

Replace all "." with "[.]".

Time Complexity: O(n). n = address.length().

Space: O(n).

AC Java:

 1 class Solution {
 2     public String defangIPaddr(String address) {
 3         if(address == null || address.length() == 0){
 4              return address;
 5         }
 6
 7         StringBuilder sb = new StringBuilder();
 8         for(int i = 0; i<address.length(); i++){
 9             char c = address.charAt(i);
10             if(c == ‘.‘){
11                 sb.append("[.]");
12             }else{
13                 sb.append(c);
14             }
15         }
16
17         return sb.toString();
18     }
19 }

原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12078982.html

时间: 2024-10-21 04:40:04

LeetCode 1108. Defanging an IP Address的相关文章

LeetCode.1108-使IP地址无效(Defanging an IP Address)

这是小川的第393次更新,第426篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第257题(顺位题号是1108).给定有效(IPv4)IP地址,返回该IP地址的无效版本. 一个无效的IP地址,是指用"[.]"取代每个点号".". 例如: 输入:address ="1.1.1.1" 输出:"1[.]1[.]1[.]1" 输入:address ="255.100.50.0" 输出:

[Leetcode] restore ip address 存储IP地址

Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example:Given"25525511135", return["255.255.11.135", "255.255.111.35"]. (Order does not matter) 题意:给定一由纯数字组成的字符串,以

LeetCode:Restore IP Address

93. Restore IP Addresses Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example: Given "25525511135", return ["255.255.11.135", "255.255.111.35"]. (Order does no

【一天一道LeetCode】#93. Restore IP Addresses

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example: Given "25525511135", return [&qu

在windows下运行docker的问题【Error getting IP address: ***】

环境配置系统:windows 10docker:Docker Toolbox https://www.docker.com/products/docker-toolbox 问题描述windows下安装完Docker Toolbox后运行Docker Quickstart Terminal可能会看到如下错误 Creating Machine default... Running pre-create checks... Creating machine... Error creating mach

Java Regex match IP address

Reference: [1] https://www.mkyong.com/regular-expressions/how-to-validate-ip-address-with-regular-expression/ import java.util.regex.Matcher; import java.util.regex.Pattern; public class IPAddressValidator{ private Pattern pattern; private Matcher ma

router 分配ip address(dhcp)和dhcp中继

路由器的主要功能是网络寻址,有时候也需要用来分配ip地址.但用路由器分配地址又会加大路由器的负载,所以,有的时候我们可以把分配地址的工作交给一台PC. 1.使用router分配ip address: Router>en Router#conf t Enter configuration commands, one per line.  End with CNTL/Z. Router(config)#int vlan 1 Router(config-if)#ip dhcp pool vlan1  

poj2105 IP Address(简单题)

题目链接:http://poj.org/problem?id=2105 Description Suppose you are reading byte streams from any device, representing IP addresses. Your task is to convert a 32 characters long sequence of '1s' and '0s' (bits) to a dotted decimal format. A dotted decima

Debian static ip address

1 // /etc/network/interfaces 2 auto eth0 3 iface eth0 inet static 4 address <Your ip address> 5 netmask <Your netmask> 6 gateway <Your gateway address> 7 broadcast <Your broadcast address> 8 9 // /etc/resolv.conf 10 nameserver <