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. (MVC)从客户端中检测到有潜在危险的 Request.Form 值

    在传统的.net Request验证中 ,只需要在WebConfig HttpRuntime节点 加入 RequestValidateMode 属性,值为2.0(此处2.0并非Framework版本) ...

  2. Chrome应用技巧之颜色拾取

    之前在Chrome应用店找了个插件实现拾色功能.并且很不理想.不知道是不是曾经Chrome自带的开发工具没提供到拾色功能还是我没发现.今天无意中发现Chomer自带的开发工具可拾色,请看以下的GIF动 ...

  3. pandas DataFrame 数据处理常用操作

    Xgboost调参: https://wuhuhu800.github.io/2018/02/28/XGboost_param_share/ https://blog.csdn.net/hx2017/ ...

  4. 转:BOOTSTRAP 增加、关闭、折叠TAB代码下载

    http://git.oschina.net/hbbcs/bootStrap-addTabs

  5. 关于audio标签播放跨域的问题

    遇到过的错误: DOMException: The play() request was interrupted by a new load request. DOMException: Failed ...

  6. Unity3D刚体不同力的测试(ForceMode,AddForce,RelativeAddForce)

    摘自圣典的一段翻译: ForceAdd a continuous force to the rigidbody, using its mass.添加一个可持续力到刚体,使用它的质量.Accelerat ...

  7. C#元祖Tuple的事例

    数组合并了同样类型的对象.而元祖合并了不同类型的对象.元祖起源于函数编程语言(F#) NET Framework定义了8个泛型Tuple(自NET4.0)和一个静态的Tuple类,他们作用元祖的工厂, ...

  8. ToStringBuilder学习(一):常用方法介绍

    一.简介与引入    1.ToStringBuilder.HashCodeBuilder.EqualsBuilder.ToStringStyle.ReflectionToStringBuilder.C ...

  9. CSU 1335: 高桥和低桥 (二分查找,树状数组)

    Description 有个脑筋急转弯是这样的:有距离很近的一高一低两座桥,两次洪水之后高桥被淹了两次,低桥却只被淹了一次,为什么?答案是:因为低桥太低了,第一次洪水退去之后水位依然在低桥之上,所以不 ...

  10. ExtPager ,分页

    package cn.edu.hbcf.common.vo; public class ExtPager { private Integer start; private Integer limit; ...