leetcode-93 Restore IP Addresses

2024-01-19 11:48
文章标签 leetcode ip 93 restore addresses

本文主要是介绍leetcode-93 Restore IP Addresses,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

原题链接: http://oj.leetcode.com/problems/restore-ip-addresses/ 

这道题的解法非常接近于NP问题,也是采用递归的解法。基本思路就是取出一个合法的数字,作为IP地址的一项,然后递归处理剩下的项。可以想象出一颗树,每个结点有三个可能的分支(因为范围是0-255,所以可以由一位两位或者三位组成)。并且这里树的层数不会超过四层,因为IP地址由四段组成,到了之后我们就没必要再递归下去,可以结束了。这里除了上述的结束条件外,另一个就是字符串读完了。可以看出这棵树的规模是固定的,不会像平常的NP问题那样,时间复杂度取决于输入的规模,是指数量级的,所以这道题并不是NP问题,因为他的分支是四段,有限制。代码如下:

class Solution {
public:vector<string> restoreIpAddresses(string s) {vector<string> res;if(s.size() < 4 || s.size() > 12) return res;helper(s,"",0,res);return res;}void helper(string s,string tmp,int count,vector<string> &res){if(count == 3){if(isValid(s)) res.push_back(tmp+s);return;}for(int i = 1; i < 4 && i < s.size(); i++){string subtmp = s.substr(0,i);if(isValid(subtmp))helper(s.substr(i),tmp+subtmp+'.',count+1,res);}}bool isValid(string str){
<span style="white-space:pre">	</span>if(str.size() <= 0) return false;int tmp = stoll(str);if(str[0] == '0' && str.size() > 1) return false;else if(tmp >=0 && tmp <= 255) return true;return false;}};
实现中需要一个判断数字是否为合法ip地址的一项的函数,首先要在0-255之间,其次前面字符不能是0。剩下的就是 NP问题 的套路了,递归中套一个for循环,不熟悉的朋友可以看看 N-Queens

关于字符串的substr函数(http://www.cplusplus.com/reference/string/string/substr/)

string substr (size_t pos = 0, size_t len = npos) const;

Position of the first character to be copied as a substring.
If this is equal to the string length, the function returns an empty string.//在上面的函数helper中的第一个参数可能是""字符串
If this is greater than the string length, it throws out_of_range.
Note: The first character is denoted by a value of 0 (not 1).


stoll("")的值为多少了?

cpp.sh上运行的结果 Exit code: 0 (normal program termination)

ubuntu14.04上运行的结果:

terminate called after throwing an instance of 'std::invalid_argument'
  what():  stoll
Aborted (core dumped)

几点注意的地方:

1. 在验证字符串是否是数字的时候,要注意0的情况,001,010,03都是非法的(0是合法的)。所以,如果第一位取出来是0,那么我们就判断字符串是否是"0",不是的情况都是非法的

2. 取字符串的时候,注意位数不够的问题,不仅<4, 而且<s.length()

3. 注意substring的范围

4. 字符串转换成数字 Integer.parseInt(); 

5. 别忘了IP 地址里面的 "."

6. 到第4个Part的时候我们就可以整体验证剩下的所有字符串(因为第4个Part最后一定要取到结尾才是正确的字符串)


参考:http://blog.csdn.net/linhuanmars/article/details/24683699

http://blog.csdn.net/u011095253/article/details/9158449

这篇关于leetcode-93 Restore IP Addresses的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/622330

相关文章

SQL Server中,用Restore DataBase把数据库还原到指定的路径

restore database 数据库名 from disk='备份文件路径' with move '数据库文件名' to '数据库文件放置路径', move '日志文件名' to '日志文件存放置路径' Go 如: restore database EaseWe from disk='H:\EaseWe.bak' with move 'Ease

[vivado][IP核]FFT

刘东华的IP核详解: 1、 2、

[vivado][IP核]DDS

刘东华的IP核详解: 1、 这里的是指IP核配置中的相位数据的宽度。 2、 实际使用此IP核时并没有“频率分辨率”可以配,是靠改变来变的。 3、 4、 5、 数据输出的ready在数据正式输出时才会有。 自己仿真: 使用SIN/COS LUT only的模式,使用一个累加器作为相位输入,不知怎么,输出为X。

[ip核][vivado]aurora

Xapp1193:  discovered:1)并不是所有芯片都支持aurora.xc7z010就没有。                     2)XDC文件的指令-允许未约束的引脚的存在:                 set_property BITSTREAM.General.UnconstrainedPins {Allow} [current_design] PG046

[ip核][vivado]Block Menory Gennerator 学习

<刘东华的xilinx系列FPGA芯片IP核详解>读书摘录: 1. 2. 3.

[ip核][vivado]FIFO 学习

<xlinx FPGA应用进阶 通用IP核详解和设计开发>读书摘录: 1.        2.3.仿真模型 特点总结:1)复位后会有busy状态,需要等待wr_rst_busy信号低电平后才能正常写入                  2)prog_full信号的高电平长度可调                  3)仿真中的读状态很奇怪,并没有正常读取,都是XXX的状态。 所用的te

LeetCode--231 2的幂

题目 给定一个整数,编写一个函数来判断它是否是 2 的幂次方。 示例 示例 1:输入: 1输出: true解释: 20 = 1示例 2:输入: 16输出: true解释: 24 = 16示例 3:输入: 218输出: false class Solution {public:bool isPowerOfTwo(int n) {if (n <= 0) return fals

LeetCode--234 回文链表

题目 请判断一个链表是否为回文链表。 示例 示例 1:输入: 1->2输出: false示例 2:输入: 1->2->2->1输出: true /*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val

LeetCode--220 存在重复元素 III

题目 给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使得 nums [i] 和 nums [j] 的差的绝对值最大为 t,并且 i 和 j 之间的差的绝对值最大为 ķ。 示例 示例 1:输入: nums = [1,2,3,1], k = 3, t = 0输出: true示例 2:输入: nums = [1,0,1,1], k = 1, t = 2输出: true示例

LeetCode--217 存在重复元素

题目 给定一个整数数组,判断是否存在重复元素。如果任何值在数组中出现至少两次,函数返回 true。如果数组中每个元素都不相同,则返回 false。 示例 示例 1:输入: [1,2,3,1]输出: true示例 2:输入: [1,2,3,4]输出: false示例 3:输入: [1,1,1,3,3,4,3,2,4,2]输出: true class Solution {p