[LeetCode] Restore IP Addresses 回溯
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)
这题是一道回溯题,其实记录好断开的位置便容易处理了。我使用的便是递归搜索的方法,用一个数组记录了断开的位置。
#include <string>
#include <vector>
#include <iostream>
using namespace std; class Solution {
public:
vector<string> restoreIpAddresses(string s) {
vector<string> ret;
if(s.size()<) return ret;
int idx[] = {,,,};
helpFun(ret,s,idx,);
return ret;
} void helpFun(vector<string> &ret,string & s,int * idx, int id)
{
if(id==){
// for(int i =0;i<4;i++)
// cout<<idx[i]<<" ";
// cout<<endl;
if(helpFun2(s.substr(idx[])))
ret.push_back(s.substr(idx[],idx[]-idx[])+"."+
s.substr(idx[],idx[]-idx[])+"."+
s.substr(idx[],idx[]-idx[])+"."+
s.substr(idx[]) );
return ;
}
for(int i =idx[id-]+;i<s.length();i++){
if(helpFun2(s.substr(idx[id-],i-idx[id-]))){
idx[id] = i;
helpFun(ret,s,idx,id+);
}
else
return ;
}
} bool helpFun2(string s)
{
if(s.length()==&&s[]=='') return true;
if(s[]=='') return false;
int sum = ;
for(int i=;i<s.length();i++){
sum = sum* + s[i]-'';
if(sum>) return false;
}
return true;
}
}; int main()
{
string s="";
Solution sol;
vector<string> ret = sol.restoreIpAddresses(s);
for(int i=;i<ret.size();i++)
cout<<ret[i]<<endl;
return ;
}
[LeetCode] Restore IP Addresses 回溯的更多相关文章
- LeetCode: Restore IP Addresses 解题报告
Restore IP Addresses My Submissions Question Solution Given a string containing only digits, restore ...
- [LeetCode] Restore IP Addresses 复原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
DFS class Solution { public: vector<string> restoreIpAddresses(string s) { return insertDot(s, ...
- 【leetcode】Restore IP Addresses
Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...
- leetcode 93. Restore IP Addresses(DFS, 模拟)
题目链接 leetcode 93. Restore IP Addresses 题意 给定一段序列,判断可能组成ip数的所有可能集合 思路 可以采用模拟或者DFS的想法,把总的ip数分成四段,每段判断是 ...
- 【LeetCode】93. Restore IP Addresses
Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...
- LeetCode解题报告—— Reverse Linked List II & Restore IP Addresses & Unique Binary Search Trees II
1. Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass ...
随机推荐
- strak组件(3):URL别名的优化
将生成URL别名的功能进行解耦.效果和上一节的一样. 效果图: 新增函数 get_url_name(self, param) # 生成url别名,需要一个参数(list/add/edit/delete ...
- Card Hand Sorting 18中南多校第一场C题
一.题意 随机给你一堆牌(标准扑克牌),之后让你按照: 第一优先规则:所有相同花色的在一起 第二优先规则:所有相同花色的必须按照升序或者降序排列 问,你最少要拿出多少张牌插入到其他的地方以维持这个状况 ...
- python-1基础总结
输入 >>> name = input() 1--如果字符串里面有很多字符都需要转义,就需要加很多\,为了简化,Python还允许用r''表示''内部的字符串默认不转义,可以自己试 ...
- PHP.20-图片上传下载
图片上传下载 思路: 1.创建图片上传的存放目录 /uploads/ 2.index.php //浏览页面,提供上传表单 上传表单:文件上传必须使用enctype="multipart/fo ...
- svn Previous operation has not finished; run 'cleanup' if it was interrupted
svn cleanup failed–previous operation has not finished; run cleanup if it was interrupted Usually, a ...
- luogu2221 [HAOI2012]高速公路
和sdoi的相关分析很像qwq,推柿子然后线段树搞搞 #include <iostream> #include <cstdio> using namespace std; ty ...
- 什么是App加壳,以及App加壳的利与弊
非著名程序员涩郎 非著名程序员,字耿左直右,号涩郎,爱搞机,爱编程,是爬行在移动互联网中的一名码匠!个人微信号:loonggg,微博:涩郎,专注于移动互联网的开发和研究,本号致力于分享IT技术和程序猿 ...
- 剑指Offer - 九度1504 - 把数组排成最小的数
剑指Offer - 九度1504 - 把数组排成最小的数2014-02-06 00:19 题目描述: 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输 ...
- js限制文本框只能输入特定字符
限制只能输入数字 // ---------------------------------------------------------------------- // <summary> ...
- MySQL增强半同步几个重要参数搭配的测试
Preface Semi-synchronous replication is supported since MySQL 5.5 and then enhanced graduall ...