Restore IP Addresses -- LeetCode
原题链接: http://oj.leetcode.com/problems/restore-ip-addresses/
这道题的解法很接近于NP问题。也是採用递归的解法。
基本思路就是取出一个合法的数字,作为IP地址的一项,然后递归处理剩下的项。能够想象出一颗树,每一个结点有三个可能的分支(由于范围是0-255,所以能够由一位两位或者三位组成)。而且这里树的层数不会超过四层,由于IP地址由四段组成,到了之后我们就不是必需再递归下去。能够结束了。这里除了上述的结束条件外,还有一个就是字符串读完了。能够看出这棵树的规模是固定的。不会像寻常的NP问题那样,时间复杂度取决于输入的规模。是指数量级的,所以这道题并非NP问题,由于他的分支是四段。有限制。代码例如以下:
public ArrayList<String> restoreIpAddresses(String s) {
ArrayList<String> res = new ArrayList<String>();
if(s==null || s.length()==0)
return res;
helper(s,0,1,"",res);
return res;
}
private void helper(String s, int index, int segment, String item, ArrayList<String> res)
{
if(index>=s.length())
return;
if(segment == 4)
{
String str = s.substring(index);
if(isValid(str))
{
res.add(item+"."+str);
}
return;
}
for(int i=1;i<4&&(i+index<=s.length());i++)
{
String str = s.substring(index, index+i);
if(isValid(str))
{
if(segment==1)
helper(s,index+i,segment+1,str,res);
else
helper(s,index+i,segment+1,item+"."+str,res);
}
}
}
private boolean isValid(String str)
{
if(str==null || str.length()>3)
return false;
int num = Integer.parseInt(str);
if(str.charAt(0)=='0' && str.length()>1)
return false;
if(num>=0 && num<=255)
return true;
return false;
}
实现中须要一个推断数字是否为合法ip地址的一项的函数,首先要在0-255之间,其次前面字符不能是0。
Restore IP Addresses -- LeetCode的更多相关文章
- Restore IP Addresses leetcode java
题目: Given a string containing only digits, restore it by returning all possible valid IP address com ...
- leetcode 93. Restore IP Addresses(DFS, 模拟)
题目链接 leetcode 93. Restore IP Addresses 题意 给定一段序列,判断可能组成ip数的所有可能集合 思路 可以采用模拟或者DFS的想法,把总的ip数分成四段,每段判断是 ...
- 【leetcode】Restore IP Addresses
Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...
- 【LeetCode】93. Restore IP Addresses
Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...
- LeetCode: Restore IP Addresses 解题报告
Restore IP Addresses My Submissions Question Solution Given a string containing only digits, restore ...
- LeetCode解题报告—— Reverse Linked List II & Restore IP Addresses & Unique Binary Search Trees II
1. Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass ...
- Leetcode 22. Generate Parentheses Restore IP Addresses (*) 131. Palindrome Partitioning
backtracking and invariant during generating the parathese righjt > left (open bracket and cloas ...
- leetcode -day29 Binary Tree Inorder Traversal & Restore IP Addresses
1. Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' ...
- 93. Restore IP Addresses
题目: Given a string containing only digits, restore it by returning all possible valid IP address com ...
随机推荐
- [BZOJ1559]密码
数据范围特别小,考虑状压DP 因为要求给定的字符串在母串中出现,所以可以用AC自动机辅助DP 因为AC自动机不能处理模式串互相包含的情况,所以先把互相包含的串去掉(暴力就行,数据范围太小) 因为要状压 ...
- 【枚举】bzoj1709 [Usaco2007 Oct]Super Paintball超级弹珠
由于子弹的轨迹是可逆的,因此我们可以枚举所有敌人的位置,然后统计他们能打到的位置,这些位置也就是能打到他们的位置咯. O(n*k). #include<cstdio> using name ...
- 【函数式权值分块】【块状链表】bzoj3065 带插入区间K小值
显然是块状链表的经典题.但是经典做法的复杂度是O(n*sqrt(n)*log^2(n))的,出题人明确说了会卡掉. 于是我们考虑每个块内记录前n个块的权值分块. 查询的时候差分什么的,复杂度就是O(n ...
- [CodePlus2017NOV]晨跑
题目大意: 三个人分别以a,b,c的速度在一个圈上晨跑,在时间为0时,他们一起出发,问何时能相遇? 思路: lcm(a,b,c)即可. #include<cstdio> #include& ...
- 一年的天数 Exercise06_16
/** * @author 冰樱梦 * 时间:2018年下半年 * 题目:一年的天数 * */ public class Exercise06_16 { public static void main ...
- 关于 xk 的位数。
关于 xk 的位数. 如果x大于0小于l,那么位数=1+小数部分×k, 如果x≥l,那么位数=trunc(ln(x)/ln(10)×k)+1+小数部分×k. trunc//向下取整
- 简述es6各种简单方法
1.取代var的let和const 局部变量都可以使用let 固定变量都可以使用const 2.字符串的变化 反引号的使用 3.解构赋值 let [a, b, c] = [1, 2, 3]; let ...
- projecteuler---->problem=11----Largest product in a grid
In the 2020 grid below, four numbers along a diagonal line have been marked in red. 08 02 22 97 38 1 ...
- Oracle Database Link 的创建和使用小见
假设:需要从数据库db_a通过db_link连接到db_b查询数据库b的部分相关信息 前提条件: 数据库a账户需要有创建dblink的权限,如果没有可以使用dba账户赋权限 grant CREATE ...
- 搭建MongoDB分片集群
在部门服务器搭建MongoDB分片集群,记录整个操作过程,朋友们也可以参考. 计划如下: 用5台机器搭建,IP分别为:192.168.58.5.192.168.58.6.192.168.58.8.19 ...