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 ...
随机推荐
- 个人评测——K米
K米APP案例分析 关于 K米 -- 的案例分析 产品 K米的APP (全国KTV点歌,手机直播,互动,交友,预订)的Android客户端 第一部分 调研,评测 评测: 软件的bug,功能评测,黑箱测 ...
- restore database
RESTORE DATABASE CTSDW FROM DISK = '\\detego-ctsetl\Backup\CTSDW\CTSDW_backup_20160722110003_Full.ba ...
- 【Duke-Image】Week_3 Spatial processing
Chapter_3 Intensity Transsformations and Spatial Filtering 灰度变换与空间滤波 Intensity transformation functi ...
- mysql通用包安装
mysql 版本: mysql-5.7.16-linux-glibc2.5-x86_64.tar.gz 解压后的初始化: mysqld --defaults-file=/etc/my.cnf --us ...
- JavaScript模块化
1.commonjs 导入: var math = require('math'); math.add(2,3); // 5 导出: module.exports={} 应用会停止并等待加载 2.AM ...
- ionic 的下拉刷新 与 上拉加载
<ion-view view-title="消息通知"> <ion-content class="padding"> <!-- & ...
- Android开发--ListView的应用
1.简介 ListView用于以列表的形式展示数据.它在装载数据时,不能使用ListView类的add()等相关方法添加,而要借助Adapter对象进行添加.另外,由于 系统提供的Adapter往往不 ...
- 转:spl_autoload_register与autoload的区别详解
转:http://www.poluoluo.com/jzxy/201306/209614.html spl_autoload_register(PHP 5 >= 5.1.2)spl_autolo ...
- jQuery.attr() 函数详解
一,jQuery.attr() 函数详解: http://www.365mini.com/page/jquery-attr.htm 二,jQuery函数attr()和prop()的区别: http: ...
- JAVA动手动脑多态
动手实验一:下列语句哪一个将引起编译错误?为什么?哪一个会引起运行时错误?为什么? m=d; d=m; d=(Dog)m; d=c; c=(Cat)m; 先进行自我判断,得出结论后,运行TestCas ...