CODE 40: Restore IP Addresses
来源:互联网 发布:淘宝实时访客其他来源 编辑:程序博客网 时间:2024/06/05 14:23
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)
public ArrayList<String> restoreIpAddresses(String s) {// Start typing your Java solution below// DO NOT write main() functionif (null == s) {return new ArrayList<String>();} else if (s.length() <= 3) {return new ArrayList<String>();}return dfs(s, 0, 0);}ArrayList<String> dfs(String s, int start, int th) {if (th < 3 && start >= s.length() - 3 + th) {return null;} else if (th == 3 && start < s.length() - 3) {return null;} else if (th == 3 && start >= s.length() - 3) {if (Integer.valueOf(s.substring(start)) > 255|| (s.substring(start).length() > 1 && s.substring(start).startsWith("0"))) {return null;}ArrayList<String> strings = new ArrayList<String>();strings.add(s.substring(start));return strings;}ArrayList<String> news = new ArrayList<String>();for (int i = 1; i <= 3; i++) {if (start + i < s.length()) {String tmp = s.substring(start, start + i);if (Integer.valueOf(tmp) > 255|| (tmp.length() > 1 && tmp.startsWith("0"))) {continue;}ArrayList<String> strs = dfs(s, start + i, th + 1);if (null != strs) {for (String str : strs) {news.add(tmp + "." + str);}}}}return news;}
- CODE 40: Restore IP Addresses
- restore-ip-addresses Java code
- leetcode 日经贴,Cpp code -Restore IP Addresses
- [C++]LeetCode: 40 Restore IP Addresses
- LeetCode: Restore IP Addresses
- Restore IP Addresses
- LeetCode : Restore IP Addresses
- [LeetCode] Restore IP Addresses
- Restore IP Addresses
- [LeetCode]Restore IP Addresses
- Restore IP Addresses
- Restore IP Addresses
- LeetCode:Restore IP Addresses
- Restore IP Addresses
- Restore IP Addresses
- LeetCode Restore IP Addresses
- [leetcode] Restore IP Addresses
- Restore IP Addresses
- Clash of Clans(COC)资源打包相关技巧探密
- 百度PC客户端研发 2014校招秋季笔试题
- js正则表达式进行格式校验
- apache开启gzip的方法
- Codeforces Round #201 (Div. 1) / 346A Alice and Bob (数论&想法题)
- CODE 40: Restore IP Addresses
- 九度OnlineJudge之1029:魔咒词典
- PHP循环函数使用介绍之PHP基础入门教程
- Working Practice-别再卧室里工作
- 30 Useful Linux Commands for System Administrators【很基础的命令】
- eclipse运行 Maven Test命令时控制台输出乱码的解决办法
- 精通JAVA的25个标准
- 多线程编程
- JavaScript加强之自定义event事件