DFS

  1. class Solution {
  2. public:
  3. vector<string> restoreIpAddresses(string s)
  4. {
  5. return insertDot(s, 0, 3);
  6. }
  7.  
  8. vector<string> insertDot(string s, int beginIndex, int countOfDot /*3, 2, 1, 0*/)
  9. {
  10. vector<string> result;
  11. if( (s.size() - beginIndex) < (countOfDot + 1) || (s.size() - beginIndex - 1) > (countOfDot +1) * 3)
  12. return result;
  13.  
  14. if(countOfDot == 0)
  15. {
  16. string partition = s.substr(beginIndex);
  17. if(partition.size() > 1 && partition[0] == '0') //Error 4: if the first char is 0, then no more chars, such as 1.00.1.1 is no valid;
  18. {
  19. return result;
  20. }
  21. int val = std::stoi(partition);
  22. if(val >= 0 && val <256)
  23. {
  24. result.push_back(partition);
  25. }
  26. return result;
  27. }
  28.  
  29. for(int i = beginIndex + 1; i< s.size();i++ ) //Error 1: i< s.size() -1
  30. {
  31. string partition = s.substr(beginIndex, i - beginIndex);
  32. if(partition.size() > 1 && partition[0] == '0') //Error 3: if the first char is 0, then no more chars, such as 1.00.1.1 is no valid;
  33. {
  34. break;
  35. }
  36. int val = std::stoi(partition);
  37. if(val > 255)
  38. break;
  39. if(val >= 0 && val <256)
  40. {
  41. vector<string> subresult = insertDot(s, i, countOfDot - 1);
  42. if(subresult.size() != 0)
  43. {
  44. for(int j = 0; j< subresult.size(); j++) //Error 2: j< s.size() -1
  45. {
  46. string onepartition = partition + "." + subresult[j];
  47. result.push_back(onepartition);
  48. }
  49. }
  50. }
  51. }
  52. return result;
  53. }
  54. };

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 回溯

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

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

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

  7. 【leetcode】Restore IP Addresses

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

  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. 个人评测——K米

    K米APP案例分析 关于 K米 -- 的案例分析 产品 K米的APP (全国KTV点歌,手机直播,互动,交友,预订)的Android客户端 第一部分 调研,评测 评测: 软件的bug,功能评测,黑箱测 ...

  2. restore database

    RESTORE DATABASE CTSDW FROM DISK = '\\detego-ctsetl\Backup\CTSDW\CTSDW_backup_20160722110003_Full.ba ...

  3. 【Duke-Image】Week_3 Spatial processing

    Chapter_3 Intensity Transsformations and Spatial Filtering 灰度变换与空间滤波 Intensity transformation functi ...

  4. mysql通用包安装

    mysql 版本: mysql-5.7.16-linux-glibc2.5-x86_64.tar.gz 解压后的初始化: mysqld --defaults-file=/etc/my.cnf --us ...

  5. JavaScript模块化

    1.commonjs 导入: var math = require('math'); math.add(2,3); // 5 导出: module.exports={} 应用会停止并等待加载 2.AM ...

  6. ionic 的下拉刷新 与 上拉加载

    <ion-view view-title="消息通知"> <ion-content class="padding"> <!-- & ...

  7. Android开发--ListView的应用

    1.简介 ListView用于以列表的形式展示数据.它在装载数据时,不能使用ListView类的add()等相关方法添加,而要借助Adapter对象进行添加.另外,由于 系统提供的Adapter往往不 ...

  8. 转:spl_autoload_register与autoload的区别详解

    转:http://www.poluoluo.com/jzxy/201306/209614.html spl_autoload_register(PHP 5 >= 5.1.2)spl_autolo ...

  9. jQuery.attr() 函数详解

    一,jQuery.attr()  函数详解: http://www.365mini.com/page/jquery-attr.htm 二,jQuery函数attr()和prop()的区别: http: ...

  10. JAVA动手动脑多态

    动手实验一:下列语句哪一个将引起编译错误?为什么?哪一个会引起运行时错误?为什么? m=d; d=m; d=(Dog)m; d=c; c=(Cat)m; 先进行自我判断,得出结论后,运行TestCas ...