原题链接: 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。

剩下的就是NP问题的套路了,递归中套一个for循环,不熟悉的朋友能够看看N-Queens哈。

Restore IP Addresses -- LeetCode的更多相关文章

  1. Restore IP Addresses leetcode java

    题目: Given a string containing only digits, restore it by returning all possible valid IP address com ...

  2. leetcode 93. Restore IP Addresses(DFS, 模拟)

    题目链接 leetcode 93. Restore IP Addresses 题意 给定一段序列,判断可能组成ip数的所有可能集合 思路 可以采用模拟或者DFS的想法,把总的ip数分成四段,每段判断是 ...

  3. 【leetcode】Restore IP Addresses

    Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...

  4. 【LeetCode】93. Restore IP Addresses

    Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...

  5. LeetCode: Restore IP Addresses 解题报告

    Restore IP Addresses My Submissions Question Solution Given a string containing only digits, restore ...

  6. 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 ...

  7. Leetcode 22. Generate Parentheses Restore IP Addresses (*) 131. Palindrome Partitioning

    backtracking and invariant during generating the parathese righjt > left  (open bracket and cloas ...

  8. leetcode -day29 Binary Tree Inorder Traversal &amp; Restore IP Addresses

    1.  Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' ...

  9. 93. Restore IP Addresses

    题目: Given a string containing only digits, restore it by returning all possible valid IP address com ...

随机推荐

  1. JVM的参数配置

    JVM管理的内存叫堆.在32Bit操作系统上有1.5G-2G的限制,而64Bit的就没有. JVM初始分配的内存由-Xms指定,默认是物理内存的1/64但小于1G. JVM最大分配的内存由-Xmx指定 ...

  2. [九省联考2018]林克卡特树(DP+wqs二分)

    对于k=0和k=1的点,可以直接求树的直径. 然后对于60分,有一个重要的转化:就是求在树中找出k+1条点不相交的链后的最大连续边权和. 这个DP就好.$O(nk^2)$ 然后我们完全不可以想到,将b ...

  3. 【计算几何】 Codeforces Beta Round #67 (Div. 2) E. Ship's Shortest Path

    读懂题意其实是模板题.就是细节略多. #include<cstdio> #include<cmath> #include<algorithm> using name ...

  4. 1.5(java学习笔记)this关键字

    this关键字主要有三个作用 1.调用本类中的属性. public class TextThis { public static void main(String[] args){ Person p1 ...

  5. 千克与磅之间的转换 Exercise05_05

    /** * @author 冰樱梦 * 时间:2018年下半年 * 题目:千克与磅之间的转换 * */ public class Exercise05_05 { public static void ...

  6. Exercise03_01

    import javax.swing.JOptionPane; public class TheDirection { public static void main(String[] args){ ...

  7. (转)[Unity3D]关于Assets资源目录结构管理

    分享个我们项目常用的目录结构,微调过很多次,最终到了这个版本.个人认为这种管理资源方式是不错的.欢迎探讨各个细节~ 更新于2013.5.30   Asserts   --Editor 自写的灵活方便插 ...

  8. 125.乘积最大(划分性DP)

    1017 乘积最大 2000年NOIP全国联赛普及组NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解 题目描述 Descriptio ...

  9. httpclient4.3访问https

    1.创建一个访问https的工具类 package org.aaa.tool;import java.io.File; import java.io.IOException; import java. ...

  10. 用Qemu模拟vexpress-a9 (二) --- 搭建u-boot调试环境

    参考: http://blog.csdn.net/caspiansea/article/details/12986565 环境介绍 Win7 64 + Vmware 11 + ubuntu14.04 ...