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. Cache、Buffer的区别

    什么是Cache?什么是Buffer?二者的区别是什么? Buffer和Cache的区别 buffer与cache操作的对象就不一样. 1.buffer(缓冲)是为了提高内存和硬盘(或其他I/O设备) ...

  2. [Codeforces967C]Stairs and Elevators(二分查找)

    [不稳定的传送门] Sloution 每次试一下最近的2个楼梯或者电梯就行了 Code #include <cstdio> #include <algorithm> #incl ...

  3. ArrayList & Vector的源码实现

    #ArrayList & Vector #####前言: 本来按照计划,ArrayList和Vector是分开讲的,但是当我阅读了ArrayList和Vector的源码以后,我就改变了注意,把 ...

  4. python协程和IO多路复用

     协程介绍                                                                                                ...

  5. angular 模块化之directive

    通过使用directive使页面模块化.需要哪部分直接调用即可.原本这些操作需要后端配合,现在前端即可.将原本的html代码拆为不同的模块,然后通过directive衔接加载到主页面中.首先页面通过d ...

  6. 基类VS接口

    该篇引用 CLR via C# 中的13.11节. 应该设计基类还是接口,这个问题不能一概而论,下面提供一些指导性原则: 1. IS_A关系(指属于,例如汽车属于交通工具) vs CAN_DO关系(指 ...

  7. SpringMVC 整合 kaptcha(验证码功能)

    一.添加依赖 <dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptch ...

  8. Python 3基础教程11-如何利用pip命令安装包和模块

    本文介绍如何利用pip命令安装Python相关的包和模块.在Python中有些方法或者模块是自带的功能,也叫(build-in),内构函数,实际使用,可能内构函数或者模块不能完成我们的任务,我们就需要 ...

  9. sources-x.list

    deb http://debian.ustc.edu.cn/ubuntu/ xenial main multiverse restricted universe deb http://debian.u ...

  10. [OpenCV] Ptr类模板

    1.C++泛型句柄类 我们知道在包含指针成员的类中,需要特别注意类的复制控制,因为复制指针时只复制指针中的地址,而不会复制指针指向的对象.这将导致当两个指针同时指向同一对象时,很可能一个指针删除了一对 ...