[LeetCode] Restore IP Address [28]
题目
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);
- }
- }
- };
另外。我开通了微信公众号--分享技术之美,我会不定期的分享一些我学习的东西.
)
[LeetCode] Restore IP Address [28]的更多相关文章
- LeetCode:Restore IP Address
93. Restore IP Addresses Given a string containing only digits, restore it by returning all possible ...
- [Leetcode] restore ip address 存储IP地址
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- [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 ...
- [LeetCode] Restore IP Addresses 复原IP地址
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- [LintCode] Restore IP Address 复原IP地址
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- [leetcode]Restore IP Addresses @ Python
原题地址:https://oj.leetcode.com/problems/restore-ip-addresses/ 题意: Given a string containing only digit ...
- LeetCode——Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- LeetCode: Restore IP Addresses 解题报告
Restore IP Addresses My Submissions Question Solution Given a string containing only digits, restore ...
- [LeetCode] Restore IP Addresses 回溯
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
随机推荐
- Aspose.Cells 基础用法
最近使用Aspose.Cells做Excel,在怎么添加批注和添加内部导航链接上耗费了一些时间,最后在官网上找到相关用法,记录一下. 代码不用过多介绍,看看即可明白. 测试代码下载 Workbook ...
- Visual Studio Code 好用的 source code editor
short cut https://code.visualstudio.com/shortcuts/keyboard-shortcuts-linux.pdf go to definition : F1 ...
- JSON的相关内容
包括: JSON概述, JSON语法规则, 使用JSON(JSON对象,JSON数组,JSON文档与对象的转换) JSON应用 额外内容(PHP输出JSON数据,解析JSON字符串,客户端如何使用服务 ...
- 6.安装和配置OpenStack图片服务组件
安装和配置图片服务组件 这里是安装在控制器上 安装和配置图片服务组件 yum install –y openstack-glance python-glanceclient 编辑/etc/glance ...
- python的递归算法学习(2):具体实现:斐波那契和其中的陷阱
1.斐波那契 什么是斐波那契,斐波那契额就是一个序列的整数的排序,其定义如下: Fn = Fn-1 + Fn-2 with F0 = 0 and F1 = 1 也就是,0,1,1,2,3,5,8,13 ...
- hdu 5084(矩阵操作)
HeHe Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- hdu 5063(思路题-反向操作数组)
Operation the Sequence Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- AC日记——Sliding Window poj 2823
2823 思路: 单调队列: 以前遇到都是用线段树水过: 现在为了优化dp不得不学习单调队列了: 代码: #include <cstdio> #include <cstring> ...
- HDU 2546.饭卡-动态规划0-1背包
饭卡 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...
- HDU 1203 【01背包/小数/概率DP】
I NEED A OFFER! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...