资讯专栏INFORMATION COLUMN

93. Restore IP Addresses

andong777 / 1927人阅读

摘要:第一种解法,找出第一部分合法的剩余部分变成相似子问题。这里的特性是最大数字不能超过。比上个方法好的地方在于才会判断数字是否合法,避免了很多这种不需要检查的情况。

</>复制代码

  1. Given a string containing only digits, restore it by returning all possible valid IP address combinations.
  2. For example:
  3. Given "25525511135",
  4. return ["255.255.11.135", "255.255.111.35"]. (Order does not matter)

第一种解法,backtracking, 找出第一部分合法的IP, 剩余部分变成相似子问题。

</>复制代码

  1. public class Solution {
  2. public List restoreIpAddresses(String s) {
  3. List res = new ArrayList();
  4. if(s.length() < 4 || s.length() > 12) return res;
  5. dfs(s, "", res, 1);
  6. return res;
  7. }
  8. public void dfs(String s, String temp, List res, int count){
  9. if(count == 4 && isValid(s)){
  10. res.add(temp + s);
  11. return;
  12. }
  13. for(int i = 1; i < Math.min(4, s.length()); i++){
  14. String cur = s.substring(0, i);
  15. if(isValid(cur)){
  16. dfs(s.substring(i), temp + cur + ".", res, count+1);
  17. }
  18. }
  19. }
  20. public boolean isValid(String s){
  21. if(s.charAt(0) == "0") return s.equals("0");
  22. int num = Integer.parseInt(s);
  23. return 0 < num && num < 256;
  24. }
  25. }

2 这里IP的特性是最大数字不能超过255。比上个方法好的地方在于a+b+c+d= s.length()才会判断数字是否合法,避免了很多1+1+1+N这种不需要检查的情况。

</>复制代码

  1. public class Solution {
  2. public List restoreIpAddresses(String s) {
  3. List ret = new ArrayList<>();
  4. StringBuffer ip = new StringBuffer();
  5. for(int a = 1 ; a < 4 ; ++ a)
  6. for(int b = 1 ; b < 4 ; ++ b)
  7. for(int c = 1 ; c < 4 ; ++ c)
  8. for(int d = 1 ; d < 4 ; ++ d)
  9. {
  10. if(a + b + c + d == s.length() )
  11. {
  12. int n1 = Integer.parseInt(s.substring(0, a));
  13. int n2 = Integer.parseInt(s.substring(a, a+b));
  14. int n3 = Integer.parseInt(s.substring(a+b, a+b+c));
  15. int n4 = Integer.parseInt(s.substring(a+b+c));
  16. if(n1 <= 255 && n2 <= 255 && n3 <= 255 && n4 <= 255)
  17. {
  18. ip.append(n1).append(".").append(n2)
  19. .append(".").append(n3).append(".").append(n4);
  20. if(ip.length() == s.length() + 3) ret.add(ip.toString());
  21. ip.delete(0, ip.length());
  22. }
  23. }
  24. }
  25. return ret;
  26. }
  27. }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/66922.html

相关文章

  • LeetCode: 93. Restore IP Addresses

    摘要:以剩下的字符串,当前字符串,剩余单元数传入下一次递归。结束条件字符串长度为,并且剩余单元数为 Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example:Given 25525511135, return [2...

    Shisui 评论0 收藏0
  • leetcode93. Restore IP Addresses

    摘要:题目要求返回字符串能够组成的所有地址。思路与代码地址由位二进制数字构成,一共分为个区间,每个区间位。那么我们只要划分出这四个区间,然后判断这四个区间的值是否符合标准即可。 题目要求 Given a string containing only digits, restore it by returning all possible valid IP address combinatio...

    chenjiang3 评论0 收藏0
  • [LeetCode] 93. Restore IP Addresses

    Problem Given a string containing only digits, restore it by returning all possible valid IP address combinations. Example: Input: 25525511135Output: [255.255.11.135, 255.255.111.35] Solution class So...

    xingqiba 评论0 收藏0
  • leetcode-93-Restore IP Addresses

    摘要:题目描述题目理解将一段字符广度搜索截取,分别有种组合形式,添加限制条件,过滤掉不适合的组合元素。长度,大小,首字母应用如果进行字符串的子元素组合穷举,可以应用。所有的循环,利用到前一个状态,都可以理解为动态规划的一种分支 题目描述:Given a string containing only digits, restore it by returning all possible va...

    wmui 评论0 收藏0
  • [LintCode/LeetCode] Restore IP Addresses

    摘要:第一个分割点第二个分割点第三个分割点 Problem Given a string containing only digits, restore it by returning all possible valid IP address combinations. Example Given 25525511135, return [ 255.255.11.135, 255....

    bingo 评论0 收藏0

发表评论

0条评论

andong777

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<