http://www.cnblogs.com/remlostime/archive/2012/11/14/2770072.html

 class Solution {
private:
vector<string> ret;
int pos[];
public:
bool check(string &s, int beg, int end)
{
string ip(s, beg, end - beg + );
if (ip.size() == )
return "" <= ip && ip <= "";
else if (ip.size() == )
return "" <= ip && ip <= "";
else if (ip.size() == )
return "" <= ip && ip <= "";
else
return false;
} void dfs(int dep, int maxDep, string &s, int start)
{
if (dep == maxDep)
{
if (start == s.size())
{
int beg = ;
string addr;
for(int i = ; i < maxDep; i++)
{
string ip(s, beg, pos[i] - beg + );
beg = pos[i] + ;
addr += i == ? ip : "." + ip;
}
ret.push_back(addr);
}
return;
} for(int i = start; i < s.size(); i++)
if (check(s, start, i))
{
pos[dep] = i;
dfs(dep + , maxDep, s, i + );
}
} vector<string> restoreIpAddresses(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
ret.clear();
dfs(, , s, );
return ret;
}
};

http://yucoding.blogspot.com/2013/04/leetcode-question-83-restore-ip.html

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)

Analysis:
This problem can be viewed as a DP problem. There needed 3 dots to divide the string, and make sure the IP address is valid:  less than or equal to 255, greater or equal to 0, and note that, "0X" or "00X" is not valid.
For the DP, the length of each part is from 1 to 3. We use a vector<string> to store each part, and cut the string every time. Details see the code.

Note that "atoi" is for c-string, <string> need to convert to cstring by str.c_str();

Code(Updated 201309):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution {
public:
    bool valid(string s){
        if (s.size()==3 && (atoi(s.c_str())>255 || atoi(s.c_str())==0)){return false;}
        if (s.size()==3 && s[0]=='0'){return false;}
        if (s.size()==2 && atoi(s.c_str())==0){return false;}
        if (s.size()==2 && s[0]=='0'){return false;}
        return true;
    }
 
    void getRes(string s, string r, vector<string> &res, int k){
        if (k==0){
            if (s.empty()){res.push_back(r);}
            return;
        }else{
            for (int i=1;i<=3;i++){
                if (s.size()>=i && valid(s.substr(0,i))){
                    if (k==1){getRes(s.substr(i),r+s.substr(0,i),res,k-1);}
                    else{getRes(s.substr(i),r+s.substr(0,i)+".",res,k-1);}
                }
            }
        }
    }
 
    vector<string> restoreIpAddresses(string s) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        vector<string> res;
        getRes(s,"",res,4);
        return res;
    }
};

Code(old version):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class Solution {
public:
 
    void dp(string s,vector<string> &cur ,vector<string> &res){              
        if (cur.size()==3){ // if there are 4 parts in the original string
            cur.push_back(s); //all 4 parts and check if valid
            bool r = true;
            for (int i=0;i<4;i++){
                if (atoi(cur[i].c_str())>255){  //check value
                    r = false;
                    break;
                }
                if ((cur[i].size()>1 && cur[i][0]=='0')){ //check "0X" "00X" and "0XX" cases
                    r =false;
                    break;
                }
            }       
            if (r){
                res.push_back(cur[0]+"."+cur[1]+"."+cur[2]+"."+cur[3]);
            }
            cur.pop_back();
             
        }else{
            for (int i=0;i<3;i++){
                if (s.size()>i+1){
                    cur.push_back(s.substr(0,i+1));
                    dp(s.substr(i+1,(s.size()-i-1)),cur,res);
                    cur.pop_back();
                }
            }
        }
         
    }
 
 
    vector<string> restoreIpAddresses(string s) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        vector<string> res,cur;
        if (s.size()>12 || s.size()<4 ){return res;}
        dp(s,cur,res); // cur stores the current separation
        return res;
    }
};

 

leetcode-Restore IP Addresses-ZZ的更多相关文章

  1. LeetCode: Restore IP Addresses 解题报告

    Restore IP Addresses My Submissions Question Solution Given a string containing only digits, restore ...

  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 @ Python

    原题地址:https://oj.leetcode.com/problems/restore-ip-addresses/ 题意: Given a string containing only digit ...

  4. LeetCode——Restore IP Addresses

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

  5. [LeetCode] Restore IP Addresses 回溯

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

  6. LeetCode Restore IP Addresses

    DFS class Solution { public: vector<string> restoreIpAddresses(string s) { return insertDot(s, ...

  7. leetcode 93. Restore IP Addresses(DFS, 模拟)

    题目链接 leetcode 93. Restore IP Addresses 题意 给定一段序列,判断可能组成ip数的所有可能集合 思路 可以采用模拟或者DFS的想法,把总的ip数分成四段,每段判断是 ...

  8. 【leetcode】Restore IP Addresses

    Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...

  9. 【LeetCode】93. Restore IP Addresses

    Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...

  10. LeetCode解题报告—— Reverse Linked List II & Restore IP Addresses & Unique Binary Search Trees II

    1. Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass ...

随机推荐

  1. Django Rest Framework(阿奇)

    Django Rest Framework 一. 什么是RESTful REST与技术无关,代表的是一种软件架构风格,REST是Representational State Transfer的简称,中 ...

  2. linux操作python

    Linux安装python3: sudo apt-get install python3.5 安装python库 sudo apt-get install python3-setuptools sud ...

  3. node.js修改全局安装文件路径

    在进行 node.js 的开发过程中,我们需要下载大量的依赖模块,为了不让 c 盘的东西太过于散乱,可以通过修改node的配置参数,来修改node依赖的下载路径. 步骤: ①创建两个文件夹:node_ ...

  4. org.elasticsearch.script.Script使用

    org.elasticsearch.script.Script使用 public Map<String, Object> builderMapPackage(PageBean pageBe ...

  5. Linux - 组管理和权限管理

    l Linux组基本介绍 在linux中的每个用户必须属于一个组,不能独立于组外.在linux中每个文件有所有者.所在组.其它组的概念. 1) 所有者 2) 所在组 3) 其它组 4) 改变用户所在的 ...

  6. element ui 表格提交时获取所有选中的checkbox的数据

    <el-table ref="multipleTable" :data="appList" @selection-change="changeF ...

  7. MySQL PRIMARY KEY 和 UNIQUE的区别

    primary key = unique +  not null unique 就是唯一,当你需要限定你的某个表字段每个值都唯一,没有重复值时使用.比如说,如果你有一个person 表,并且表中有个身 ...

  8. 10个重要的算法C语言实现源代码

    包括拉格朗日,牛顿插值,高斯,龙贝格,牛顿迭代,牛顿-科特斯,雅克比,秦九昭,幂法,高斯塞德尔 .都是经典的数学算法,希望能开托您的思路.转自kunli.info 1.拉格朗日插值多项式 ,用于离散数 ...

  9. ZOJ 3769 Diablo III

    描述 Diablo III is an action role-playing video game. A few days ago, Reaper of Souls (ROS), the new e ...

  10. 虚拟机下linux 的root密码忘记怎么修改(转)

    1.开机时任意按一个方向键,进入界面,选择linux系统,按e键进入 2.然后用上下键选择kerner(内核)那一行,按e键进入编辑界面,编辑界面最后一行显示如下:(grub edit> ker ...