[LeetCode] 1108. Defanging an IP Address
Description
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.
Analyse
将ip地址中的.
替换成[.]
, 简单题
在address上直接replace,或者插入到一个新的字符串里去
string defangIPaddr(string address)
{
for(int i = 0; i < address.size(); i++)
{
if (address[i] == '.')
{
address.replace(i, 1, "[.]");
i+=2; //使i越过已经replace过的 .
}
}
cout << address << endl;
return address;
}
string defangIPaddr(string address)
{
string result;
for(int i = 0; i < address.length(); i++)
{
if (address[i] == '.')
{
result += "[.]";
}
else
{
result += address[i];
}
}
return result;
}
[LeetCode] 1108. Defanging an IP Address的更多相关文章
- LeetCode 1108. Defanging an IP Address (IP 地址无效化)
题目标签:String 题目给了我们一组 ip address,让我们把 . 变成 [.],这题可以用replace,但是这样做的话,好像没意义了.所以还是走一下array,具体看code. Java ...
- 【Leetcode_easy】1108. Defanging an IP Address
problem 1108. Defanging an IP Address solution: class Solution { public: string defangIPaddr(string ...
- 【leetcode】1108. Defanging an IP Address
题目如下: Given a valid (IPv4) IP address, return a defanged version of that IP address. A defanged IP a ...
- 【LeetCode】468. Validate IP Address 解题报告(Python)
[LeetCode]468. Validate IP Address 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- LeetCode.1108-使IP地址无效(Defanging an IP Address)
这是小川的第393次更新,第426篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第257题(顺位题号是1108).给定有效(IPv4)IP地址,返回该IP地址的无效版本. ...
- LeetCode:Restore IP Address
93. Restore IP Addresses Given a string containing only digits, restore it by returning all possible ...
- [Leetcode] restore ip address 存储IP地址
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- [LeetCode] Restore IP Address [28]
题目 Given a string containing only digits, restore it by returning all possible valid IP address comb ...
- [LintCode] Restore IP Address 复原IP地址
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
随机推荐
- CF1097D Makoto and a Blackboard 质因数分解 DP
Hello 2019 D 题意: 给定一个n,每次随机把n换成它的因数,问经过k次操作,最终的结果的期望. 思路: 一个数可以表示为质数的幂次的积.所以对于这个数,我们可以分别讨论他的质因子的情况. ...
- Gym - 101667H - Rock Paper Scissors FFT 求区间相同个数
Gym - 101667H:https://vjudge.net/problem/Gym-101667H 参考:https://blog.csdn.net/weixin_37517391/articl ...
- 2018 Petrozavodsk Winter Camp, Yandex Cup
A. Ability Draft solved by RDC 60min start, 148 min AC, 1Y 题意:两只 Dota 队伍,每队 \(n\) 个英雄,英雄一开始无技能,他们需要按 ...
- lightoj 1049 - One Way Roads(dfs)
Time Limit: 0.5 second(s) Memory Limit: 32 MB Nowadays the one-way traffic is introduced all over th ...
- lightoj 1245 Harmonic Number (II)(简单数论)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1245 题意:求f(n)=n/1+n/2.....n/n,其中n/i保留整数 显 ...
- JAVA - 一个for循环实现99乘法表
public class Test03 {public static void main(String[] args) { int lie = 1; for (int hang = 1; hang&l ...
- 纯JS实现在一个字符串b中查找另一个字符串a出现的所有位置,并且不使用字符串的方法(递归)
问题:判断字符串A在中所有出现字符串B中(长度大于1)的索引.不得使用字符串方法indexof,substring等 有小伙伴在面试遇到了这个问题,乍一看如果使用使用字符串方法indexof,subs ...
- springBoot异常统一处理
springBoot异常统一处理 采用@ControllerAdvice注解和@ExceptionHandler注解,可以对异常进行统一处理. 1.结构图: 2.pom.xml文件: <?xml ...
- Git 从入门到熟练|不敢说精通
前言 如果有一定版本管理软件基础或使用过 svn 的你,这篇 git 的文章应该是最适合你的.作者也是从 svn 过来,从开始的觉得 git 麻烦,到最后还是感觉 git 是最好用的版本控制软件. 虽 ...
- ACM代码模板
功能介绍 写了I/O函数,支持以下几种方式 read(num); //读入一个数num(任意整数类型,下同) read(num1,num2,num3,num4); //读入任意多个数 read(arr ...