这个题目就和Palindrome Partitioning很像了,而且比那个的DFS的递归要简单一些,让常人更好理解一些。但是边界条件更多,要考虑清楚。

整体思路就是深度优先搜索,首先看到边界条件没,如果没有,就深度搜索:

一开始搜索1个字符和剩下的字符串,判断该字符的index是否越界了,对于剩下的字符串递归;

然后是2个字符和剩下的字符串,判断这2个字符的首字符是否是0,对于剩下的字符串递归;

然后是3个字符和剩下的字符串,判断这3个字符的首字符是否是0,并且这3个字符组成的数字是否小于等于255,对于剩下的字符串递归。

ok,思路理清楚了,上代码:

import java.util.ArrayList;public class Solution {ArrayList<String> rt = new ArrayList<String>();ArrayList<Integer> cand = new ArrayList<Integer>();String str;public ArrayList<String> restoreIpAddresses(String s) {// Start typing your Java solution below// DO NOT write main() function
        rt.clear();cand.clear();str = s;if (s.length() > 12)return rt;dfs(0);return rt;}//index 是当前检测的第一个字符private void dfs(int index) {//终止条件,已经检测到合法的candidate,那么添加到结果集if (cand.size() == 4 && index == str.length()) {addCandToRt();}//因为1位和2位数字肯定小于255,所以直接往更深搜索if (index >= str.length() || cand.size() >= 4)return;cand.add(Integer.parseInt(str.substring(index, index+1)));dfs(index+1);cand.remove(cand.size()-1);if (index >= str.length()-1 || str.substring(index, index+1).equals("0") || cand.size() >= 4)return;cand.add(Integer.parseInt(str.substring(index, index+2)));dfs(index+2);cand.remove(cand.size()-1);if (index >= str.length()-2 || str.substring(index, index+1).equals("0") || cand.size() >= 4)return;if (Integer.parseInt(str.substring(index, index+3)) <= 255) {cand.add(Integer.parseInt(str.substring(index, index+3)));dfs(index+3);cand.remove(cand.size()-1);}}private void addCandToRt() {String ip = cand.get(0) + "."+ cand.get(1) + "."+ cand.get(2) + "."+ cand.get(3);rt.add(ip);}public static void main(String[] args) {String s = "010010";Solution sl = new Solution();ArrayList<String> all = sl.restoreIpAddresses(s);for (int i = 0; i < all.size(); i++) {System.out.println(all.get(i));}}
}

我对这个题目的理解是,一个完整的DFS,加上数个条件的剪枝。

回头看看有没有别的办法解这道题目。

转载于:https://www.cnblogs.com/lihaozy/p/3182696.html

[leetcode] Restore IP Addresses相关推荐

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

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

  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

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

  4. LeetCode Restore IP Addresses(回溯法)

    题意:给出一个由数字组成的字符串,求其能表示的ip地址列表 注意不能有前缀0,如010 思路:每次操作时,有两种情况,一种是添加点,一种是将其作为当前数的后序数. 在小数点的个数超过3时,递归退出. ...

  5. [LeetCode]93.Restore IP Addresses

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

  6. 【DFS + backtrack】LeetCode 93. Restore IP Addresses

    LeetCode 93. Restore IP Addresses Solution1:我的答案 怎么就这么慢... class Solution { public:vector<string& ...

  7. Restore IP Addresses leetcode java

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

  8. LeetCode 93. Restore IP Addresses

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

  9. 【leetcode刷题笔记】Restore IP Addresses

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

最新文章

  1. 修改eclipse启动时eclipse使用的jre
  2. 解决Windows下新安装的CodeBlocks无法编译运行
  3. Android项目开发新版本需要注意的事项
  4. sql命令(四)-操作数据表中的记录
  5. NNACL2021 放榜啦~
  6. 宝塔apache mysql_宝塔Linux面板命令大全,安装宝塔,Nginx,Apache,MySQL等 - SEO中文网...
  7. GitHub简单入门
  8. Bootstrap实战 - 注册和登录
  9. elasticsearch 英文数字组合字符串模糊检索
  10. 头条App项目测试实战(三)文章写评论功能用例设计
  11. DAO题目:开发一个程序,用于记录车辆购置税
  12. 用js屏蔽脚本加载的广告
  13. windows PE结构解析
  14. 扫福得福背后,支付宝 AR 红包的技术创新与故事
  15. rsyslog 定义格式
  16. 一、在PyCharm上直接调试py脚本
  17. 7.1.5、Sqoop__sqoop常用命令参数,import,export,hive,数据库连接,hbase等
  18. 那些有趣 Python 库
  19. 【转载】ARPU ARPPU傻傻分不清楚?手游收入指标名词解释
  20. C语言学习-Day4

热门文章

  1. flask and html connection
  2. 2019年如何打造自己的“前端品牌”
  3. 647. Palindromic Substrings
  4. CLAMAV 杀毒软件安装及使用配置
  5. 从锁的原理到构建分布式锁
  6. DOS命令大全(转)
  7. Android--视频播放器
  8. 转载一篇NAT实验,备忘
  9. iOS6和iOS7代码的适配(1)
  10. puts(char *) gets(char *)