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

Have you met this question in a real interview?

Yes
Example

Given "25525511135", return

[
"255.255.11.135",
"255.255.111.35"
]

Order does not matter.

LeetCode上的原题,请参见我之前的博客Restore IP Addresses

解法一:

class Solution {
public:
/**
* @param s the IP string
* @return All possible valid IP addresses
*/
vector<string> restoreIpAddresses(string& s) {
vector<string> res;
helper(s, , "", res);
return res;
}
void helper(string s, int k, string out, vector<string>& res) {
if (k == ) {
if (s.empty()) res.push_back(out);
return;
}
for (int i = ; i < ; ++i) {
if (s.size() < i) break;
int val = stoi(s.substr(, i));
if (val > || i != to_string(val).size()) continue;
helper(s.substr(i), k - , out + s.substr(, i) + (k == ? "" : "."), res);
}
}
};

解法二:

class Solution {
public:
/**
* @param s the IP string
* @return All possible valid IP addresses
*/
vector<string> restoreIpAddresses(string& s) {
vector<string> res;
for (int a = ; a < ; ++a)
for (int b = ; b < ; ++b)
for (int c = ; c < ; ++c)
for (int d = ; d < ; ++d)
if (a + b + c + d == s.size()) {
int A = stoi(s.substr(, a));
int B = stoi(s.substr(a, b));
int C = stoi(s.substr(a + b, c));
int D = stoi(s.substr(a + b + c, d));
if (A <= && B <= && C <= && D <= ) {
string t = to_string(A) + "." + to_string(B) + "." + to_string(C) + "." + to_string(D);
if (t.size() == s.size() + ) res.push_back(t);
}
}
return res;
}
};

[LintCode] Restore IP Address 复原IP地址的更多相关文章

  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 address 存储IP地址

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

  3. [LeetCode] 93. Restore IP Addresses 复原IP地址

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

  4. [LeetCode] Validate IP Address 验证IP地址

    In this problem, your job to write a function to check whether a input string is a valid IPv4 addres ...

  5. 华东师大OJ:IP Address【IP地址转换】

    /*===================================== IP Address Time Limit:1000MS Memory Limit:30000KB Total Subm ...

  6. 468 Validate IP Address 验证IP地址

    详见:https://leetcode.com/problems/validate-ip-address/description/ Java实现: class Solution { public St ...

  7. 093 Restore IP Addresses 复原IP地址

    给定一个只包含数字的字符串,复原它并返回所有可能的IP地址格式.例如:给定 "25525511135",返回 ["255.255.11.135", " ...

  8. Leetcode93. Restore IP Addresses复原IP地址

    给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式. 示例: 输入: "25525511135" 输出: ["255.255.11.135", ...

  9. Windows Azure Cloud Service (44) 将Cloud Service加入Virtual Network Subnet,并固定Virtual IP Address(VIP)

    <Windows Azure Platform 系列文章目录> 在之前的文章中,笔者已经详细介绍了如何将Virtual Machine加入Virtual Network,并且绑定固定的Pr ...

随机推荐

  1. Ubuntu14.04LTS系统输入法的安装

    由于安装的时候选择的是英文版,所以一进入系统问题就来了:无法输入中文. 我记得自己直接选的输入法是pinyin那个 在网上看到别人到blog,直接转过来吧,只为自己收藏下,如有需要请联系原作者. 转载 ...

  2. cocos2d-x CCScrollView和CCTableView的使用(转载)

    转载请注明来自:Alex Zhou的程序世界,本文链接:http://codingnow.cn/cocos2d-x/1024.html //============================== ...

  3. Error: Could not find or load main class test.EditFile

    今天写了一个简单的小程序,运行之后发现Error: Could not find or load main class test.EditFile,项目无法启动.删除main中的所有内容之后依旧提示该 ...

  4. 50个必备的实用jQuery代码段

    本文会给你们展示50个jquery代码片段,这些代码能够给你的javascript项目提供帮助.其中的一些代码段是从jQuery1.4.2才开始支持的做法,另一些则是真正有用的函数或方法,他们能够帮助 ...

  5. scrollview嵌套listview 滑动事件冲突的解决方法

    listView.setOnTouchListener(new View.OnTouchListener() {                            @Override       ...

  6. JavaScript设计模式——工厂模式

    工厂模式:是一种实现“工厂”概念的面上对象设计模式.实质是定义一个创建对象的接口,但是让实现这个接口的类来决定实例化哪个类.工厂方法让类的实例化推迟到子类中进行.创建一个对象常常需要复杂的过程,所以不 ...

  7. hdu2767 Proving Equivalences Tarjan缩点

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  8. git 学习笔记7--branch

    分支是git的必杀技. 站点另一个角度,分支是的快照移动有向图,刚好是拓扑排序的一种例子. basic git branch testing #创建分支 git checkout testing #切 ...

  9. Windows 8 系统安装

    系统城  http://www.xitongcheng.com/win8/ 1.  下载 win8: http://msdn.itellyou.cn/2.  准备 4G 以上 U 盘,下载 win8 ...

  10. BZOJ 1925[Sdoi2010]地精部落 题解

    题目大意: 1~n的排列中,要任意一个数要么比它左右的数都大或小,求所有的方案数. 思路: 主要思路:离散. 三个引理: ①在n->n-1的转化过程中,我们删除了一个点后,我们可以将n-1个点视 ...