Leetcode-Resotre IP Addresses
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的更多相关文章
- LeetCode: Restore IP Addresses 解题报告
Restore IP Addresses My Submissions Question Solution Given a string containing only digits, restore ...
- [LeetCode] Restore IP Addresses 复原IP地址
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- [leetcode]Restore IP Addresses @ Python
原题地址:https://oj.leetcode.com/problems/restore-ip-addresses/ 题意: Given a string containing only digit ...
- LeetCode OJ-- Restore IP Addresses
https://oj.leetcode.com/problems/restore-ip-addresses/ string到int的ip地址格式化. 分别用 i+1,j+1,k+1,表示前三个地址段的 ...
- LeetCode——Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- [LeetCode] Restore IP Addresses 回溯
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- LeetCode Restore IP Addresses
DFS class Solution { public: vector<string> restoreIpAddresses(string s) { return insertDot(s, ...
- 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 ...
随机推荐
- Oracle 动态sql 实现方式
/******************************************************************* Sample Program 10: Dynamic SQL ...
- NYOJ 10 skiing (深搜和动归)
skiing 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描写叙述 Michael喜欢滑雪百这并不奇怪. 由于滑雪的确非常刺激.但是为了获得速度.滑的区域必须向下倾斜.并且 ...
- ITIL学习心笔记总结
四:价值: 被验证的价值1:革新意识---管理也是第一生产力 被验证的价值2:统一思路---这不是一个人的战斗 被验证的价值3:体系的价值---‘新木桶原理’ ITSM的实施经验表明,“”一流的工程师 ...
- FreeSWITCH技巧:notify与message-waiting
FreeSWITCH技巧:notify与message-waiting @(Freeswitch经验点滴) 现象描述 在客户端登陆抓包时,发现了FreeSWITCH发来的包: NOTIFY sip:9 ...
- app store上传图片显示错误:未能创建 屏幕快照
在iTunes Connect中加入一个app后.加入屏幕快照时,依照要求的尺寸上传照片成功,可是在保存的时候提示"未能创建Screenshots for 4-inch iPhone5 an ...
- 点滴积累【other】---存储过程删除所有表中的数据(sql)
USE [QG_Mis24] GO /****** Object: StoredProcedure [dbo].[p_set1] Script Date: 07/18/2013 13:25:57 ** ...
- [elk]logstash的grok匹配逻辑grok+date+mutate
重点参考: http://blog.csdn.net/qq1032355091/article/details/52953837 logstash的精髓: grok插件原理 date插件原理 kv插件 ...
- redis命令_ZADD
ZADD key score member [[score member] [score member] ...] 将一个或多个 member 元素及其 score 值加入到有序集 key 当中. 如 ...
- 李洪强iOS开发OC[001]-NSLog函数的使用方法
- vim添加一键编译
引用来自: http://blog.chinaunix.net/uid-21202106-id-2406761.html; 事先声明,我使用的VIM完全是基于终端的,而不是gvim或vim-x11.因 ...