[LintCode] Restore IP Address 复原IP地址
Given a string containing only digits, restore it by returning all possible valid IP address combinations.
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地址的更多相关文章
- [LeetCode] Restore IP Addresses 复原IP地址
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- [Leetcode] restore ip address 存储IP地址
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- [LeetCode] 93. Restore IP Addresses 复原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 ...
- 华东师大OJ:IP Address【IP地址转换】
/*===================================== IP Address Time Limit:1000MS Memory Limit:30000KB Total Subm ...
- 468 Validate IP Address 验证IP地址
详见:https://leetcode.com/problems/validate-ip-address/description/ Java实现: class Solution { public St ...
- 093 Restore IP Addresses 复原IP地址
给定一个只包含数字的字符串,复原它并返回所有可能的IP地址格式.例如:给定 "25525511135",返回 ["255.255.11.135", " ...
- Leetcode93. Restore IP Addresses复原IP地址
给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式. 示例: 输入: "25525511135" 输出: ["255.255.11.135", ...
- Windows Azure Cloud Service (44) 将Cloud Service加入Virtual Network Subnet,并固定Virtual IP Address(VIP)
<Windows Azure Platform 系列文章目录> 在之前的文章中,笔者已经详细介绍了如何将Virtual Machine加入Virtual Network,并且绑定固定的Pr ...
随机推荐
- Linux SSH远程文件/目录传输命令scp
转载地址:http://www.vpser.net/manage/scp.html 相信各位VPSer在使用VPS时会经常在不同VPS间互相备份数据或者转移数据,大部分情况下VPS上都已经安装了Ngi ...
- 如何安装Ecshop for linux
下载 http://update.shopex.com.cn/version/program/ECShop/download_ecshop_utf8.php 解压缩之后把upload文件夹中的内容放到 ...
- SurfaceView
我们先来看下官方API对SurfaceView的介绍 SurfaceView的API介绍 Provides a dedicated drawing surface embedded inside of ...
- kafka producer源码
producer接口: /** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor l ...
- 【java 断点续传】
模拟 断点续传 首先,先读取word文件的 一部分 package com.sxd.readLines; import java.io.File; import java.io.FileInputSt ...
- SpringJDBC解析4-query方法
重要步骤说明: 首先是从PersonServiceImpl方法进去,调用JdbcTemplate的query方法,然后执行一连串错中复杂的调用,而且里面有很多函数都是以回调形式处理, 1)JdbcTe ...
- poj3186 Treats for the Cows(区间)
题目链接:http://poj.org/problem?id=3186 题意:第一个数是N,接下来N个数,每次只能从队列的首或者尾取出元素. ans=每次取出的值*出列的序号.求ans的最大值. 样例 ...
- Ajax实现点击省份显示相应城市
功能:不用级联效果,自己写ajax,从接口读取省份城市数据,实现点击省份显示相应城市.后端根据省份ID,给前端返回城市. 一.DOM结构(套用blade模板) <div class=" ...
- 并查集 + 线段树 LA 4730 Kingdom
题目传送门 题意:训练指南P248 分析:第一个操作可以用并查集实现,保存某集合的最小高度和最大高度以及城市个数.运用线段树成端更新来统计一个区间高度的个数,此时高度需要离散化.这题两种数据结构一起使 ...
- 并查集(图论) LA 3644 X-Plosives
题目传送门 题意:训练指南P191 分析:本题特殊,n个物品,n种元素则会爆炸,可以转移到图论里的n个点,连一条边表示u,v元素放在一起,如果不出现环,一定是n点,n-1条边,所以如果两个元素在同一个 ...