LeetCode OJ 93. Restore IP Addresses
题目
Given a string containing only digits, restore it by returning all possible valid IP address combinations.
Example:
Input: "25525511135"
Output: ["255.255.11.135", "255.255.111.35"]
解答
一开始看还挺难的,想了想发现其实就是DFS。索的每一步就相当于从剩下的字符串中截取一段,需要确保截取的这一段合法(不大于255,同时除了0之外首位不能为0)。剪枝有两种情况,一种是已经截取了4次但字符串还没截取完,另一种是截取不到4次就已经截取完了。当截取4次且字符串已经截取完,就得到了一个解。
下面是AC的代码:
class Solution {
public:
vector<string> ans;
vector<string> temp;
void cut(string s){
int length = s.size();
int seg = temp.size();
if(seg == 4 && length == 0){
string temps;
for(vector<string>::iterator iter = temp.begin(); iter != temp.end(); iter++){
if(iter != temp.begin()){
temps += ".";
}
temps += *iter;
}
ans.push_back(temps);
}
else if(seg == 4 && length != 0){
;
}
else if(seg < 4 && length == 0){
;
}
else{
for(int i = 1; i < 4 && i < length + 1; i++){
if(i != 1 && s[0] == '0'){
continue;
}
if(atoi(s.substr(0, i).c_str()) < 256){
temp.push_back(s.substr(0, i));
cut(s.substr(i, length - i));
temp.pop_back();
}
}
}
}
vector<string> restoreIpAddresses(string s) {
cut(s);
return ans;
}
};
121
四个月❤️
LeetCode OJ 93. Restore IP Addresses的更多相关文章
- 【LeetCode】93. Restore IP Addresses
Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...
- 【一天一道LeetCode】#93. Restore IP Addresses
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】93. Restore IP Addresses 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- LeetCode OJ:Restore IP Addresses(存储IP地址)
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- leetcode 93. Restore IP Addresses(DFS, 模拟)
题目链接 leetcode 93. Restore IP Addresses 题意 给定一段序列,判断可能组成ip数的所有可能集合 思路 可以采用模拟或者DFS的想法,把总的ip数分成四段,每段判断是 ...
- 93.Restore IP Addresses(M)
93.Restore IP Addresses Medium 617237FavoriteShare Given a string containing only digits, restore it ...
- 93. Restore IP Addresses
题目: Given a string containing only digits, restore it by returning all possible valid IP address com ...
- [LeetCode] 93. Restore IP Addresses 复原IP地址
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- leetcode 93 Restore IP Addresses ----- java
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
随机推荐
- AnimDynamics简介
转自:http://www.cnblogs.com/corgi/p/5405452.html AnimDynamics简介 AnimDynamics是UE4.11 Preview 5测试版本发布的An ...
- ip route rule 路由策略 高级路由 捆绑 网桥
http://lwfs.net/2005/11/28/10/ #!/bin/bash IP0= IP1= GW0= GW1= NET0= NET1= DEV0=eth0 DEV1=eth1 # com ...
- ZooKeeper系列(9):ZooKeeper实现分布式Barrier和Queue
1. 快速开始 1.1概述: Zookeeper是Hadoop的一个子项目,它是分布式系统中的协调系统,可提供的服务主要有:配置服务.名字服务.分布式同步.组服务等. 1.2 使用常见 1.2.1 统 ...
- C语言:冒泡排序
void sort(int arr[],int len) { ; ; i<len; i++) { printf("第%d轮:\n", i); // len-i+1:新轮比上轮 ...
- CPU Rings, Privilege, and Protection.CPU的运行环, 特权级与保护
原文标题:CPU Rings, Privilege, and Protection 原文地址:http://duartes.org/gustavo/blog/ [注:本人水平有限,只好挑一些国外高手的 ...
- Android被忽略的tools
自动生成的布局xml文件,很多都带有tools字样:但是大部分都被我们删除了: 其实它的作用是让我们这些开发者预览用的,十分的方便: 事例一个TextView: <TextView androi ...
- 在Raid模式下装Win10找不到固态硬盘怎么办
现在新出厂的笔记本电脑中,系统的BIOS内,SATA Controller Mode默认设置的是为Intel RST Premium模式,该模式会将硬盘组成磁盘阵列的模式(Raid模式),而原版的Wi ...
- Docker使用札记 - 常用命令
1. 删除untagged的镜像(TAG列为<none>) docker images -f "dangling=true" -q|xargs docker rmi
- Xcode 如何导入IOS项目
前言:基于mac上如何导入ios项目的文章,手机自动化项目需要进行手机元素定位,前提是导入IOS项目 1.安装Xcode 到官网下载mac版Xcode:当前使用版本Version 7.3.1 http ...
- Mysql 多字段去重
使用group by去重现在有如下表 id name age1 张三 232 李四 343 张三 234 李四 32 需求 : 按照name和age字段联合去重 sql如下 select * from ...