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. 【Hector slam】A Flexible and Scalable SLAM System with Full 3D Motion Estimation

    作者总结了SLAM前端和后端的区别 While SLAM frontends are used to estimate robot movement online in real-time, the ...

  2. java 反编译

    JavaDecompiler http://jd.benow.ca/jd-eclipse/update/

  3. lua特性纪要

    [局部变量] lua的局部变量通过local进行显示声明, 其作用域仅限于声明它的块block.这里的block分为三种类型: 1.控制结构的执行体 2.函数的执行体 3.chunk 比较容易引起混淆 ...

  4. Appium之python API

    webdriver contexts(self) 说明:返回多个会话内容 使用:driver.contexts current_context(self) 说明:返回单个会话的内容 使用:driver ...

  5. 即时聊天IM之四 Android客户端IM帮助类编写

    图文无关一起娱乐: 这一篇我们开始写Android端的Smack版主类,后面Android的IM功能都是通过这个帮助类实现的 引用类库: 因为我用的是IDE是Android Studio,所以我通过g ...

  6. Ubuntu: ImportError: No module named xgboost

    ImportError: No module named xgboost 解决办法: git clone --recursive https://github.com/dmlc/xgboost cd ...

  7. JavaScript的面向对象编程(OOP)(一)——类

    在学习JavaScript面向对象的编程之前,需要知道,并了解面向对象的一些基本的常识.初学者中大多数都以为面向对象中,面向对象的编程是很重要和占据很大一部分精力.笔者在之前也是认为OOP是面向对象的 ...

  8. c# DataGridView 的一些属性设置,序号,合并头

      this.dataGridView1.DataSource = this.dISASTERBindingSource;             this.dataGridView1.Locatio ...

  9. asp.net网站运行出错:the underlying provider failed on open的解决

    在登录系统,通过linq查询时发生错误,the underlying provider failed on open,如何解决,请看: Step 1:Open Internet Information ...

  10. Sql 获取向上取整、向下取整、四舍五入取整的实例

    [四舍五入取整截取] select round(54.56,0) [向下取整截取] SELECT FLOOR(54.56) [向上取整截取]  SELECT   CEILING(13.15) --MS ...