题目

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地址。

这个题能够理解成,将字符串分割成若干个小块,将当中合法的(数值在0-255)组合起来,拼接成一个合法的IP地址。讲到这也就看出来了这还是一个组合问题。对于组合问题。算法都是结构性的递归套循环,看例如以下代码。

代码实现

class Solution {
public:
vector<string> restoreIpAddresses(string s) {
vector<string> ret;
helper(0, 0, s, string(), ret);
return ret;
}
void helper(int k, int start, const string& s, string part, vector<string> & ret){
if(k>4) return;
if(k==4){
if(start == s.size()){
ret.push_back(part);
}
return;
}
int key=0;
if(k>0)
part += ".";
for(int i=start; i<s.size(); ++i){
if(i>start && s[start] == '0') break;
key = 10*key + s[i]-'0';
if(key > 255) return;
string temp = part;
temp += s.substr(start, i-start+1);
helper(k+1, i+1, s, temp, ret);
}
}
};
假设你认为本篇对你有收获。请帮顶。

另外。我开通了微信公众号--分享技术之美,我会不定期的分享一些我学习的东西.
你能够搜索公众号:swalge 或者扫描下方二维码关注我

(转载文章请注明出处: http://blog.csdn.net/swagle/article/details/30027655
)

[LeetCode] Restore IP Address [28]的更多相关文章

  1. LeetCode:Restore IP Address

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

  2. [Leetcode] restore ip address 存储IP地址

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

  3. [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 ...

  4. [LeetCode] Restore IP Addresses 复原IP地址

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

  5. [LintCode] Restore IP Address 复原IP地址

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

  6. [leetcode]Restore IP Addresses @ Python

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

  7. LeetCode——Restore IP Addresses

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

  8. LeetCode: Restore IP Addresses 解题报告

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

  9. [LeetCode] Restore IP Addresses 回溯

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

随机推荐

  1. flexigrid 学习总结

    最近看到了一款flexigrid表格组件,简单美观,在下载使用的过程中,发现缺少很多功能.<基于jQuery的GridView-Flexigrid(2)-扩展和修复>给我带来了很大的帮助, ...

  2. mysql创建用户后无法登陆

    创建用户后登陆失败的原因是存在匿名用户: root@controller:~# mysql -h localhost -uaa -ppassword ERROR 1045 (28000): Acces ...

  3. app:compileDebugNdk,NDK

    Error:Execution failed for task ':app:compileDebugNdk'. > Error: Your project contains C++ files ...

  4. unicode和utf8之间的关系

    ,字符编码是计算机技术的基石,想要熟练使用计算机,懂得一点字符编码的知识,还是很有必要的. 1. ASCII码 我们知道,在计算机内部,所有的信息最终都表示为一个二进制的字符串.每一个二进制位(bit ...

  5. C/51单片机

    1.    串口也可以有多根线的,但是各线之间没有协调同步发送,而是各自是独自发送的.并口是同步发送,同步一次8位同时成功同时失败,类比事务. 2.    ASCII码的前32位是通讯预留的编码即使现 ...

  6. centos7 挂载exfat格式的u盘

    下载 两个包 rpm -ivh http://download1.rpmfusion.org/free/el/updates/7/x86_64/f/fuse-exfat-1.2.8-1.el7.x86 ...

  7. node.js批量重命名文件

    把当前路径的img文件夹里面的文件加上.png后缀 var fs = require("fs"); var path = 'img' fs.readdir(path, functi ...

  8. ASPOSE.WORD 另存为HTML

    var fi = new FileInfo(Environment.CurrentDirectory + "\\AE9302C0-AE48-4F4B-8489-6A428D9163C9_AL ...

  9. ELK集群

    kafka集群-------------------1. 下载wget http://mirror.rise.ph/apache/kafka/0.11.0.0/kafka_2.12-0.11.0.0. ...

  10. Unique Word Abbreviation -- LeetCode

    An abbreviation of a word follows the form <first letter><number><last letter>. Be ...