LeetCode: 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)

分递归和循环两种解法,递归会超时,循环:16 milli secs。

class Solution {
public:
    // 递归解法
    void ip(string &s, string &tmp, vector<string> &result,
            int nStart, int pos)
    {
        if (nStart == s.size() && pos == 4)
        {
            result.push_back(tmp);
            return;
        }
        
        int x = 0;
        for (int i = nStart; i < nStart+4 && i < s.size(); ++i)
        {
            x = x*10 + s[i] - '0';
            if (x > 255 || x < 0)
                break;
            
            string str = tmp;
            if (pos > 0)
                str.append(1, '.');
            
            string st = s.substr(nStart, i-nStart+1);
            // 判断是否合法
            if (st.size() > 1 && st[0] == '0')
                break;
                
            str += st;
            ip(s, str, result, i+1, pos+1);
            
        }
    }
    vector<string> restoreIpAddresses(string s) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        vector<string> result;
        int nSize = s.size();
        int a = 0;
        // 循环解
        for (int i = 0; i < nSize && i < 4; ++i)
        {
            string strTmp;
            a = a*10 + s[i] - '0';
            if (a < 0 || a > 255)
                break;
            string st = s.substr(0, i+1);
            // 判断是否合法
            if (st.size() > 1 && st[0] == '0')
                break;
            
            int b = 0;
            for (int j = i+1; j < nSize && j < i+5; ++j)
            {
                b = b*10 + s[j] - '0';
                if (b < 0 || b > 255)
                    break;
                string st = s.substr(i+1, j-i);
                // 判断是否合法
                if (st.size() > 1 && st[0] == '0')
                    break;

                int c = 0;
                for (int k = j + 1; k < nSize && k < j+5; ++k)
                {
                    c = c*10 + s[k] - '0';
                    if (c < 0 || c > 255)
                        break;
                    string st = s.substr(j+1, k-j);
                    // 判断是否合法
                    if (st.size() > 1 && st[0] == '0')
                        break;
                        
                    int d = 0;
                    bool flag = true;
                    for (int l = k+1; l < nSize; ++l)
                    {
                        d = d*10 + s[l] - '0';
                        if (d < 0 || d > 255)
                        {
                            flag = false;
                            break;
                        }
                    }
                    st = s.substr(k+1);
                    // 判断是否合法
                    if ((st.size() > 1 && st[0] == '0') || st.size() == 0)
                            flag = false;
                    if (flag)
                    {
                        string tmp = s.substr(0, i+1) + '.' + s.substr(i+1, j-i) +   
                            "." + s.substr(j+1, k-j) + "." + s.substr(k+1, nSize-k);
                        result.push_back(tmp);
                    }
                }
            }
                
        }
        
        // string tmp;
        // ip(s, tmp, result, 0, 0);
        return result;
    }
};

点赞