LeetCode Restore IP Addresses
DFS
- class Solution {
- public:
- vector<string> restoreIpAddresses(string s)
- {
- return insertDot(s, 0, 3);
- }
- vector<string> insertDot(string s, int beginIndex, int countOfDot /*3, 2, 1, 0*/)
- {
- vector<string> result;
- if( (s.size() - beginIndex) < (countOfDot + 1) || (s.size() - beginIndex - 1) > (countOfDot +1) * 3)
- return result;
- if(countOfDot == 0)
- {
- string partition = s.substr(beginIndex);
- 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;
- {
- return result;
- }
- int val = std::stoi(partition);
- if(val >= 0 && val <256)
- {
- result.push_back(partition);
- }
- return result;
- }
- for(int i = beginIndex + 1; i< s.size();i++ ) //Error 1: i< s.size() -1
- {
- string partition = s.substr(beginIndex, i - beginIndex);
- 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;
- {
- break;
- }
- int val = std::stoi(partition);
- if(val > 255)
- break;
- if(val >= 0 && val <256)
- {
- vector<string> subresult = insertDot(s, i, countOfDot - 1);
- if(subresult.size() != 0)
- {
- for(int j = 0; j< subresult.size(); j++) //Error 2: j< s.size() -1
- {
- string onepartition = partition + "." + subresult[j];
- result.push_back(onepartition);
- }
- }
- }
- }
- return result;
- }
- };
LeetCode Restore IP Addresses的更多相关文章
- LeetCode: Restore IP Addresses 解题报告
Restore IP Addresses My Submissions Question Solution Given a string containing only digits, restore ...
- [LeetCode] Restore IP Addresses 复原IP地址
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- [leetcode]Restore IP Addresses @ Python
原题地址:https://oj.leetcode.com/problems/restore-ip-addresses/ 题意: Given a string containing only digit ...
- LeetCode——Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- [LeetCode] Restore IP Addresses 回溯
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- leetcode 93. Restore IP Addresses(DFS, 模拟)
题目链接 leetcode 93. Restore IP Addresses 题意 给定一段序列,判断可能组成ip数的所有可能集合 思路 可以采用模拟或者DFS的想法,把总的ip数分成四段,每段判断是 ...
- 【leetcode】Restore IP Addresses
Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...
- 【LeetCode】93. Restore IP Addresses
Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...
- 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 ...
随机推荐
- 【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 ...
- java 反编译
JavaDecompiler http://jd.benow.ca/jd-eclipse/update/
- lua特性纪要
[局部变量] lua的局部变量通过local进行显示声明, 其作用域仅限于声明它的块block.这里的block分为三种类型: 1.控制结构的执行体 2.函数的执行体 3.chunk 比较容易引起混淆 ...
- Appium之python API
webdriver contexts(self) 说明:返回多个会话内容 使用:driver.contexts current_context(self) 说明:返回单个会话的内容 使用:driver ...
- 即时聊天IM之四 Android客户端IM帮助类编写
图文无关一起娱乐: 这一篇我们开始写Android端的Smack版主类,后面Android的IM功能都是通过这个帮助类实现的 引用类库: 因为我用的是IDE是Android Studio,所以我通过g ...
- Ubuntu: ImportError: No module named xgboost
ImportError: No module named xgboost 解决办法: git clone --recursive https://github.com/dmlc/xgboost cd ...
- JavaScript的面向对象编程(OOP)(一)——类
在学习JavaScript面向对象的编程之前,需要知道,并了解面向对象的一些基本的常识.初学者中大多数都以为面向对象中,面向对象的编程是很重要和占据很大一部分精力.笔者在之前也是认为OOP是面向对象的 ...
- c# DataGridView 的一些属性设置,序号,合并头
this.dataGridView1.DataSource = this.dISASTERBindingSource; this.dataGridView1.Locatio ...
- asp.net网站运行出错:the underlying provider failed on open的解决
在登录系统,通过linq查询时发生错误,the underlying provider failed on open,如何解决,请看: Step 1:Open Internet Information ...
- Sql 获取向上取整、向下取整、四舍五入取整的实例
[四舍五入取整截取] select round(54.56,0) [向下取整截取] SELECT FLOOR(54.56) [向上取整截取] SELECT CEILING(13.15) --MS ...