[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 ...
随机推荐
- IOS VLC编译步骤(包含移植和截图功能)
http://blog.csdn.net/Kan_Crystal/article/details/40424673 一.下载源码 先到VLC官网将源码下载到本机,以下链接为官网编译操作地址:https ...
- ros move_base costmap 理解和实现动态窗口法避障
以下大部分内容参考自 ros_by_example_hydro_volume_1.pdf local costmap 是怎么生成的?跟三维点云有什么关系? global costmap在没有全局地图下 ...
- 腾讯云通信WebIM事件回调的坑~
最近在开过工作中用到了腾讯IM的功能,由于业务的需要主要使用到了: 1.loginInfo 用户登录,用户信息 2.getRecentContactList 获得最近联系人 3.getLastGrou ...
- HDU 6112.今夕何夕-蔡勒公式 (2017"百度之星"程序设计大赛 - 初赛(A)1005)
1005:今夕何夕 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Probl ...
- stl set求交集 并集 差集
#include <iostream>#include <set> using namespace std; typedef struct tagStudentInfo{ i ...
- 【强连通分量】tarjan算法及kosaraju算法+例题
阅读前请确保自己知道强连通分量是什么,本文不做赘述. Tarjan算法 一.算法简介 Tarjan算法是一种由Robert Tarjan提出的求有向图强连通分量的时间复杂度为O(n)的算法. 首先我们 ...
- Visio中如何让重叠图形都显示
如图,两个矩形重叠部分的边框都显示了,是拆分操作,不是组合.
- java实现点选汉字验证码(自己修改后的)
参考:http://blog.csdn.net/qq_26680031/article/details/51168527 package com.rd.p2p.web; import java.awt ...
- Facebook KeyHash生成方法
1. 去https://code.google.com/p/openssl-for-windows/downloads/list下载OpenSSL工具 2. 在C盘根目录下新建一个openssl的文 ...
- python开发微信支付学习记录(转)
前言 微信支付是由微信及财付通联合推出的移动支付创新产品.如今,随着微信支付的全面开放,相关需求也越来越多,很多开发人员进行微信支付开发及商家申请微信支付时,面临着诸多疑惑. 要想开发顺利进行,首先要 ...