[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 ...
随机推荐
- P4570 [BJWC2011]元素 线性基 + 贪心
题意 给定n个物品,每个物品有一个编号和价值,问如何取使得拿到的物品价值总和最大,并且取得物品的编号的子集异或和不能为0. 思路 这是个贪心,我们先按照价值从大到小排序,然后贪心地取,如果当前要取的物 ...
- gym/102059 E
gym/102059 待通过:A.D.G.J.M 已补过:E E:电路题,判断一个图是不是简单电路.不需要特殊的技巧,利用set存图,把度数为2的点都删掉,融入到一条边上即可. #include &l ...
- 杭电多校第九场 HDU6415 Rikka with Nash Equilibrium dp
Rikka with Nash Equilibrium Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 524288/524288 K ...
- Codeforces Round #480 (Div. 2) A. Links and Pearls
题目地址:http://codeforces.com/contest/980/problem/A 官方题解: 我的理解:o表示珍珠,-表示链子,给一串字符串你可以任意重组这条项链(不能删去),判断这条 ...
- Mysql之zip格式的安装
Mysql之zip格式的安装 一.配置环境变量 首先,我新建一个文件夹,将其改名为MySQL Sever 5.6,其路径为D:\Program Files (x86)\MySQL\MySQL Seve ...
- Oracle sqlldr 在DOS窗口导入多列数据到数据库表
sqlldr 用法详见:https://www.cnblogs.com/rootq/archive/2009/03/01/1401061.html 测试内容: 1.创建数据库表: create tab ...
- docker-将自己的Linux打包为镜像
基于原始文件和目录从0开始制作镜像: 1).基于CentOS7 Linux纯净系统(初始化安装完成),将Linux整个系统打包成tar文件即可: cd /root/ tar --numeric-own ...
- SpannableString设置文本背景色
参考内容: http://blog.csdn.net/harvic880925/article/details/38984705 http://blog.it985.com/14433.html 1. ...
- 搜索专题题解(FJUT - OJ 17级搜索强化训练)
题目连接:http://120.78.128.11/Contest.jsp?cid=221#H 题目都比较难,每道题都很经典,我也做的很慢,这篇博文算是个收录.具体题目题解点击下面超链接吧. 一.Br ...
- TestNG(八) 类分组测试
package com.course.testng.groups; import org.testng.annotations.Test; @Test(groups = "stu" ...