dfs即可

注意边界情况:

(1)s长度不在4 和 12之间

(2)"010"这种形式是错误的

 1 class Solution {
 2 public:
 3     vector<string> restoreIpAddresses(string s) {
 4         vector<string> res;
 5         if(s.length() < 4 || s.length() > 12){
 6             return res;
 7         }
 8
 9         dfs(s,"",res,0);
10         return res;
11     }
12     void dfs(string ip,string tmpres,vector<string> &res,int cnt){
13         if(cnt == 3 && isvalid(ip)){
14             string tmp = tmpres+ip;
15             res.push_back(tmp);
16             return;
17         }
18         for(int i = 1 ; i < 4 && i < ip.length() ; ++i){
19             string tmp = ip.substr(0,i);
20             if(isvalid(tmp)){
21                 dfs(ip.substr(i),tmpres+tmp+".",res,cnt+1);
22             }
23         }
24     }
25     bool isvalid(string str){
26         if(str.length() <= 0)
27             return false;
28         int tmp = atoi(str.c_str());
29         if(str[0] == '0'){
30             if(str == "0"){
31                 return true;
32             }
33             else
34             {
35                 return false;
36             }
37         }
38         if(tmp <= 255 && tmp > 0){
39             return true;
40         }
41         return false;
42     }
43 };

转载于:https://www.cnblogs.com/cane/p/3955457.html

LeetCode--Restore IP Addresses相关推荐

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

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

  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

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

  4. LeetCode Restore IP Addresses(回溯法)

    题意:给出一个由数字组成的字符串,求其能表示的ip地址列表 注意不能有前缀0,如010 思路:每次操作时,有两种情况,一种是添加点,一种是将其作为当前数的后序数. 在小数点的个数超过3时,递归退出. ...

  5. [leetcode] Restore IP Addresses

    这个题目就和Palindrome Partitioning很像了,而且比那个的DFS的递归要简单一些,让常人更好理解一些.但是边界条件更多,要考虑清楚. 整体思路就是深度优先搜索,首先看到边界条件没, ...

  6. [LeetCode]93.Restore IP Addresses

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

  7. 【DFS + backtrack】LeetCode 93. Restore IP Addresses

    LeetCode 93. Restore IP Addresses Solution1:我的答案 怎么就这么慢... class Solution { public:vector<string& ...

  8. Restore IP Addresses leetcode java

    题目: Given a string containing only digits, restore it by returning all possible valid IP address com ...

  9. LeetCode 93. Restore IP Addresses

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

  10. 【leetcode刷题笔记】Restore IP Addresses

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

最新文章

  1. Android OkHttp完全解析
  2. php简单网站源码包含数组_PHP 数组
  3. 介绍Jupyter和Pandas
  4. php初学遇到的问题。
  5. 输出表格csv_自动提取生成excel表格!推荐一款开源工具
  6. arduino-esp32:LVGL中文字库(通用)
  7. 使用 spire.doc 将word转pdf
  8. 爬取中国地震网地震数据
  9. vs2015调试iisexpress无法启动的问题解决方案整理
  10. 阿里云国际站服务器怎么样
  11. 乐观的态度使工作充满激情
  12. CYUSB3014 USB3.0与FPGA设计
  13. ATECC508A芯片开发笔记(一):初识加密芯片
  14. [转]VC常用小知识
  15. 李沐学AI之数据探索性分析
  16. 北京理工大学计算机实验广域网通信与有,北京理工大学计算机实验七报告表.doc...
  17. 正则表达式( 形容词、转义符号、补充)
  18. ToC战场进入尾声,ToB市场战争厮杀即将升级?
  19. [渝粤教育] 信阳师范学院 计算机网络原理 参考 资料
  20. 基本知识 100118

热门文章

  1. c语言程序设计黄保和第二章,c语言程序设计答案(选择题+编程)黄保和、江戈版...
  2. Python入门经典(2K超清)
  3. 传奇进去选择服务器位置偏移,传奇聊天框偏移怎么解决,GOM GEE聊天框错位的解决方法...
  4. cefsharp winform 页面里的单击事件不触发_事件详解C 键盘事件及事件流
  5. 自动驾驶 4-4 纵向车辆建模Longitudinal Vehicle Modeling
  6. 计算机检索系统中 每一种文献特征,自考《档案文献检索》串讲资料(1)
  7. 52. N皇后 II
  8. 第三章CDMA的原理与应用(2)
  9. Birth-Death process 生灭过程
  10. 数位DP算法概述及习题