Given a string containing only digits, restore it by returning all possible valid IP address combinations.

For example:
Given "25525511135",

return ["255.255.11.135", "255.255.111.35"]. (Order does not matter)

Analysis:

This is a recursive problem. A string will be divided into four parts. For each part, we should determine whether it is a valid IP segment.

Solution:

 public class Solution {
public List<String> restoreIpAddresses(String s) {
List<String> res = new ArrayList<String>();
if (s.length()==0) return res; res = restoreRecur(s,0,4);
return res;
} public List<String> restoreRecur(String s, int curIndex, int num){
List<String> res = new ArrayList<String>();
if (curIndex>=s.length()) return res; if (num==1){
String temp = s.substring(curIndex,s.length());
if (temp.length()>3) return res;
int val = Integer.parseInt(temp);
if (temp.length()==3 && val>=100 && val<=255){
res.add(temp);
return res;
}
if (temp.length()==2 && val>=10 && val<=99){
res.add(temp);
return res;
}
if (temp.length()==1){
res.add(temp);
return res;
}
return res;
} if (curIndex+1>=s.length()) return res;
int end = curIndex+3;
if (curIndex+3>s.length())
end = s.length(); for (int i=curIndex+1;i<=end;i++){
String temp = s.substring(curIndex,i);
int val = Integer.parseInt(temp);
List<String> nextRes = new ArrayList<String>();
if (temp.length()==3 && val>=100 && val<=255){
nextRes = restoreRecur(s,i,num-1);
}
if (temp.length()==2 && val>=10 && val<=99){
nextRes = restoreRecur(s,i,num-1);
}
if (temp.length()==1){
nextRes = restoreRecur(s,i,num-1);
}
for (int j=0;j<nextRes.size();j++)
res.add(temp+"."+nextRes.get(j));
}
return res; }
}

Leetcode-Resotre IP Addresses的更多相关文章

  1. LeetCode: Restore IP Addresses 解题报告

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

  2. [LeetCode] Restore IP Addresses 复原IP地址

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  3. [leetcode]Restore IP Addresses @ Python

    原题地址:https://oj.leetcode.com/problems/restore-ip-addresses/ 题意: Given a string containing only digit ...

  4. LeetCode OJ-- Restore IP Addresses

    https://oj.leetcode.com/problems/restore-ip-addresses/ string到int的ip地址格式化. 分别用 i+1,j+1,k+1,表示前三个地址段的 ...

  5. LeetCode——Restore IP Addresses

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  6. [LeetCode] Restore IP Addresses 回溯

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  7. LeetCode Restore IP Addresses

    DFS class Solution { public: vector<string> restoreIpAddresses(string s) { return insertDot(s, ...

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

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

  9. 【leetcode】Restore IP Addresses

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

  10. 【LeetCode】93. Restore IP Addresses

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

随机推荐

  1. php中数据类型自动转换

    1.1 转为布尔型(即返回值为0) 空字符串''或"" 数字0或0.0 字符'0'或"0" 空值NULL 没有成员的数组    其余都转换成布尔型true,包含 ...

  2. Win10怎么打开或关闭自动维护功能

  3. Atitit.  Exception in thread "main" java.lang.Error: Unresolved compilation problem:

    Atitit.  Exception in thread "main" java.lang.Error: Unresolved compilation problem: 1.1. ...

  4. layui实现点击按钮添加行(方法渲染创建的table)

    / jquery实现的搜索功能 $('#search_btn').on('click',function(){ var txt=$('#inputValue').val(); var value=$( ...

  5. Android Screen Monitor

    实现屏幕同步前提是安装有JDK和配置好ADB的环境变量 1.官方地址 http://code.google.com/p/android-screen-monitor/ 2.解压缩得到asm.jar 3 ...

  6. SPI—读写串行 FLASH

    SPI协议简介SPI 协议是由摩托罗拉公司提出的通讯协议(Serial Peripheral Interface),即串行外围设备接口,是一种高速全双工的通信总线.它被广泛地使用在 ADC. LCD ...

  7. Unix系统编程()close系统调用

    close系统调用关闭一个打开的文件描述符,并将其释放回调用进程,供该进程继续使用.当一进程终止时,将自动关闭其已打开的所有文件描述符. int close(int fd); 显式关闭不再需要的文件描 ...

  8. fiddler获取手机请求

    最近由于项目的关系,一直在测试APP,但是比较令人头痛的问题就是,APP的测试没有办法像web的测试那样,可以直接的通过开发工具看到请求和响应,对于一些东西只能去看日志(比较麻烦),偶然间发现了fid ...

  9. (转)64位开源处理器Rocket的源代码简单介绍

    转载地址: http://blog.csdn.net/leishangwen/article/details/46604819 最近大概阅读了一下UCB发布的Rocket处理器的源码,对源代码各个文件 ...

  10. CDH 安装配置指南(Tarball方式)

    采用CDH Tarbal方式安装Hadoop集群. 1. 环境组件版本 组件名称 组件版本 用途 jdk 1.8 jdk-8u191-linux-x64 oracle jdk mysql mysql- ...