Restore IP Address

这道题和Palindrome Partitioning很像,都是怎么切数组的问题,唯一需要注意的是ip的规定

1)不能出现0和其他数字组合, 类似0.00.01.1。0只能单独出现或者在一个片段中不为开头的数字。

2)每一个片段数字最大为255

3)只能有四个片段

4)这里需要注意一个细节: 在这里加入sub.length()>3就无需继续的原因也是避免sub过大,超过integer,从而使Integer.parseInt(s) threw exception

public class Solution {
    /**
     * @param s the IP string
     * @return All possible valid IP addresses
     */
    public ArrayList<String> restoreIpAddresses(String s) {
        // Write your code here
        ArrayList<String> result = new ArrayList<String>();
        if("".equals(s)){
            return result;
        }
        List<String> ips = new ArrayList<String>();
        helper(result, ips, s, 0);
        return result;
    }

    private void helper(ArrayList<String> result, List<String> ips,
                        String s, int start){
        if(start == s.length() && ips.size() == 4){
            StringBuilder ip = new StringBuilder();
            for(String ipPart : ips){
                ip.append(ipPart);
                ip.append(‘.‘);
            }
            result.add(ip.deleteCharAt(ip.length()-1).toString());
            return;
        }

        for (int i = start; i < s.length(); i++){
            String sub = s.substring(start, i+1);
            if(sub.length()>3 || ips.size()>4 ||
               isZeroStart(sub) || Integer.parseInt(sub)>255){
                continue;
            }
            ips.add(sub);
            helper(result, ips, s, i+1);
            ips.remove(ips.size()-1);
        }
    }

    private boolean isZeroStart(String s){
        if(‘0‘ == s.charAt(0) && s.length() > 1){
            return true;
        }
        return false;
    }
}
时间: 2024-11-13 18:23:58

Restore IP Address的相关文章

[Leetcode] restore ip address 存储IP地址

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) 题意:给定一由纯数字组成的字符串,以

LeetCode:Restore IP Address

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 ["255.255.11.135", "255.255.111.35"]. (Order does no

[LeetCode] Restore IP Address [28]

http://blog.csdn.net/asmcos/article/details/46676101 http://blog.csdn.net/asmcos/article/details/46676087 http://blog.csdn.net/asmcos/article/details/46676073 http://blog.csdn.net/asmcos/article/details/46676053 http://blog.csdn.net/asmcos/article/de

Restore 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) 解题思路: 注意到ip地址分成

leetcode -day29 Binary Tree Inorder Traversal &amp; Restore IP Addresses

1.  Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? 分析:求二叉树的中序

[string]Restore 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) 深度优先遍历的思想,类似的题目还

Leetcode题解(2):L93/Restore IP Addresses

L93: Restore 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 n

Java for LeetCode 093 Restore 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) 解题思路: 使用循环即可解决,

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 ["255.255.11.135", "255.255.111.35"]. (Order does not matter) 什么时候临时容器符合题意加入结