leetcode菜鸡斗智斗勇系列(2)--- 把一个ipv4地址转换成一串数字
1.原题:
https://leetcode.com/problems/defanging-an-ip-address/
这道题本身很简单,
Given a valid (IPv4) IP address, return a defanged version of that IP address.
A defanged IP address replaces every period "." with "[.]".
翻译就是,给出一个ipv4地址,把这个地址中的“.”转换为"[.]",但是作者不是很懂为啥要换成这个。2333
2.解题思路:
其实这道题最简单的办法就是用正则表达式,但是考虑到很多正则表达式的时间复杂度和空间复杂度有点大,我们这次就用傻办法,反正以后肯定会有更合适的正则表达式使用的地方。
a.所需的知识点:
substr :https://www.cnblogs.com/xzxl/p/7243490.html ,因为'.' 是一个char,所以不能直接转换到"[.]",因为是一个string,所以我们需要用substr。
b.解题思路:
class Solution {
public:
string defangIPaddr(string address) {
for (int i = address.size() - 2; i >= 0; i--)
if (address[i] == '.')
address = address.substr(0, i) + "[.]" + address.substr(i + 1);
return address;
}
};
我们从倒数第二位开始扫描,因为地址的最后一位肯定不是 ‘。’
我们一旦发现了一个点,我们就用substr来重组整个地址,然后输出,非常简单。
leetcode菜鸡斗智斗勇系列(2)--- 把一个ipv4地址转换成一串数字的更多相关文章
- leetcode菜鸡斗智斗勇系列(5)--- 寻找拥有偶数数位的数字
1.原题: https://leetcode.com/problems/find-numbers-with-even-number-of-digits/ Given an array nums of ...
- leetcode菜鸡斗智斗勇系列(1)---把一个链表中的二进制数字转换为一个整型数(int)
Convert Binary Number in a Linked List to Integer这道题在leetcode上面算作是“easy”,然而小生我还是不会做,于是根据大佬的回答来整理一下思路 ...
- leetcode菜鸡斗智斗勇系列(6)--- 检查一个string里面有几个对称的字段
1.原题: https://leetcode.com/problems/split-a-string-in-balanced-strings/ Split a String in Balanced S ...
- leetcode菜鸡斗智斗勇系列(4)--- 单一数字的乘积和总合的减法
1.原题: https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/ Given an i ...
- leetcode菜鸡斗智斗勇系列(3)--- Jewels and Stones珠宝和钻石
1.原题: https://leetcode.com/problems/jewels-and-stones/ You're given strings J representing the types ...
- leetcode菜鸡斗智斗勇系列(10)--- Decrypt String from Alphabet to Integer Mapping
1.原题: https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/submissions/ Giv ...
- leetcode菜鸡斗智斗勇系列(9)--- Range Sum of BST
1.原题: https://leetcode.com/problems/range-sum-of-bst/ Given the root node of a binary search tree, r ...
- leetcode菜鸡斗智斗勇系列(8)--- Find N Unique Integers Sum up to Zero
1.原题: https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/ Given an integer n, retur ...
- leetcode菜鸡斗智斗勇系列(7)--- 用最小的时间访问所有的节点
1.原题: https://leetcode.com/problems/minimum-time-visiting-all-points/ On a plane there are n points ...
随机推荐
- block中self会造成循环引用问题
将代码块中的 self换成unsafeSelf __unsafe_unretained 与 __weak 99%相同 __weak 当对象释放之后 会自动设置为nil 而__unsafe_unreta ...
- 二叉树的操作--C语言实现
树是一种比较复杂的数据结构,它的操作也比较多.常用的有二叉树的创建,遍历,线索化,线索化二叉树的遍历,这些操作又可以分为前序,中序和后序.其中,二叉树的操作有递归与迭代两种方式,鉴于我个人的习惯,在这 ...
- js杂项积累
主要内容: 一 浏览器重定向Http请求跨域 二 html select标签 可以设置属性multipe,变为多选 三 document.wirte只应在script标签的顶层代码中使用.不能放在函数 ...
- C#中的Stopwatch类简单使用
Stopwatch实例可以度量一个间隔的运行时间, 或度量多个间隔内所用时间的总和. 命名空间System.Diagnostics. 简单使用 using System; using System.D ...
- Snack3 一个新的微型JSON框架
Snack3 一个新的微型JSON框架 一个作品,一般表达作者的一个想法.因为大家想法不同,所有作品会有区别.就做技术而言,因为有很多有区别的框架,所以大家可以选择的框架很丰富. snack3.基于j ...
- 让微信推送Jenkins构建消息
Jenkins作为开发必备之神器,各家大小公司都在使用.Jenkins自身内置了基于邮件推送构建结果的功能.但是随着移动互联网的发展,邮件这玩意已经越来越少使用了,是否有一种办法能把jenkins构建 ...
- mac office软件的安装与破解
1.mac office 软件的安装及破解 http://10176523.cn/archives/29/ 下载后安装 切记不要登录 然后用这个文件 破解
- go break的使用
文章目录 一.踩得坑 二.break的使用 1.break用于for循环 2. break用于select 3.break用于嵌套循环 4.break label . goto label .cont ...
- linux 通过管道更方便的查询输入记录
history |sort -k4|uniq -f3 -c |sort -n -k2 history:得到输入记录|sort -k4:在第4列进行排序 -k4 第4列 每一列用空格分隔|uniq -f ...
- wxxcx_learn异常处理
属于基础框架,分级别 捕获异常,处理异常(记录日志,修复异常,返回客户端错误),抛出异常 全局异常处理(AOP)对错误同一格式化 try{ $banner = BannerModel::getBann ...