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)

Hide Tags

Backtracking String

   

   这题是一道回溯题,其实记录好断开的位置便容易处理了。我使用的便是递归搜索的方法,用一个数组记录了断开的位置。
#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 回溯的更多相关文章

  1. LeetCode: Restore IP Addresses 解题报告

    Restore IP Addresses My Submissions Question Solution Given a string containing only digits, restore ...

  2. [LeetCode] Restore IP Addresses 复原IP地址

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  3. [leetcode]Restore IP Addresses @ Python

    原题地址:https://oj.leetcode.com/problems/restore-ip-addresses/ 题意: Given a string containing only digit ...

  4. LeetCode——Restore IP Addresses

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  5. LeetCode Restore IP Addresses

    DFS class Solution { public: vector<string> restoreIpAddresses(string s) { return insertDot(s, ...

  6. 【leetcode】Restore IP Addresses

    Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...

  7. leetcode 93. Restore IP Addresses(DFS, 模拟)

    题目链接 leetcode 93. Restore IP Addresses 题意 给定一段序列,判断可能组成ip数的所有可能集合 思路 可以采用模拟或者DFS的想法,把总的ip数分成四段,每段判断是 ...

  8. 【LeetCode】93. Restore IP Addresses

    Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...

  9. 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 ...

随机推荐

  1. 可以字符串string转化成list,tuple,dict的eval()方法

    功能:将字符串str当成有效的表达式来求值并返回计算结果. 语法: eval(source[, globals[, locals]]) -> value 参数: source:一个Python表 ...

  2. 14,vue+uwsgi+nginx部署路飞学城

    有一天,老男孩的苑日天给我发来了两个神秘代码,听说是和mjj的结晶 超哥将这两个代码,放到了一个网站上,大家可以自行下载 路飞学城django代码 https://files.cnblogs.com/ ...

  3. java.lang.RuntimeException: Handler (com.***.behavior.BEvent$1) {421bca80} sending message to a Hand

    java.lang.RuntimeException: Handler (com.***.behavior.BEvent$1) {421bca80} sending message to a Hand ...

  4. 【Binary Search Tree Iterator 】cpp

    题目: Implement an iterator over a binary search tree (BST). Your iterator will be initialized with th ...

  5. 嗯,ACM按照这个一步一步来。

        转一个搞ACM需要的掌握的算法.   要注意,ACM的竞赛性强,因此自己应该和自己的实际应用联系起来. 适合自己的才是好的,有的人不适合搞算法,喜欢系统架构,因此不要看到别人什么就眼红, 发挥 ...

  6. Vue组件编写

    Vue无疑是近来最火的一个前端框架,它吸取了angular1.x和react的精华,性能优良,而且易于上手,本文主要是关于如何去写一个组件. 首先是项目目录 编写组件 在这里我写了一个日期控件(移动端 ...

  7. Pytest+allure生成测试报告

    1.Allure.zip包的下载地址: https://github.com/allure-framework/allure2 在跳转页面选择一个allure.zip包的版本下载 若以上方法无法下载z ...

  8. 解决Navicat for MySQL 连接 Mysql 8.0.11 出现1251- Client does not support authentication protocol 错误

    安装MySQL8.0之后,使用Navicat连接数据库,报1251错误. 上网搜索解决方案,网上说出现这种情况的原因是:mysql8 之前的版本中加密规则是mysql_native_password, ...

  9. pin

    sjhh@123456 Michael zhang zhangxiaocong_2011@yeah.net

  10. 博客挪窝了 http://my.oschina.net/jrrx/blog

    1. 博客园很不错: 2. 由于各种原因,挪到了 http://my.oschina.net/jrrx/blog