Leetcode OJ : Restore IP Addresses backtrack暴搜 C++ solution
class Solution {
public:
vector<string> ret;
string src;
int len;
unordered_set<string> added;
vector<string> restoreIpAddresses(string s) {
if ( s.empty() ) return ret;
src = s;
len = src.size();
backTrack(,, "");
return ret;
}
void backTrack(int pos, int dot_cnt, string substr) {
if ( dot_cnt > ) return;
if ( pos >= len && dot_cnt == ) {
substr.pop_back();
if ( added.find(substr) == added.end() ) {
ret.push_back( substr );
added.insert( substr );
}
return;
}
char buffer[];
int tx = len - pos > ? : len - pos;
for ( int i = ; i <= tx; i++ ) {
string temp = src.substr( pos, i );
int number = atoi( temp.c_str() );
sprintf( buffer, "%d", number );
string str(buffer);
if ( str != temp ) continue;
string newsub = substr + temp + ".";
if ( number >= && number <= ) {
backTrack( pos + i, dot_cnt + , newsub );
}
}
}
};
Leetcode OJ : Restore IP Addresses backtrack暴搜 C++ solution的更多相关文章
- 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 复原IP地址
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- 【leetcode】Restore IP Addresses (middle)
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- leetcode 93 Restore IP Addresses ----- java
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- Java for LeetCode 093 Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- LeetCode : 93. Restore IP Addresses
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABZ4AAAHUCAYAAAC6Zj2HAAAMFGlDQ1BJQ0MgUHJvZmlsZQAASImVlw
- 【LeetCode】93. Restore IP Addresses
Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...
- LeetCode: Restore IP Addresses 解题报告
Restore IP Addresses My Submissions Question Solution Given a string containing only digits, restore ...
随机推荐
- svn is already locked解决方案
在出错文件夹下,鼠标右键TortoiseSVN->Clean up. SVN错误:Attempted to lock an already-locked dir 1.出现这个问题后使用“清理”功 ...
- ExtJS4.2学习(13)基于表格的扩展插件---rowEditing
鸣谢:http://www.shuyangyang.com.cn/jishuliangongfang/qianduanjishu/2013-11-24/182.html --------------- ...
- PAT-乙级-1034. 有理数四则运算(20)
1034. 有理数四则运算(20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 本题要求编写程序,计算2个有理 ...
- leetcode3 Two Sum III – Data structure design
Question: Design and implement a TwoSum class. It should support the following operations: add and f ...
- hdu 3682
将每个格子标记为 x*n*n+y*n+z 每个格子会有一个独特的编号 将它放入vector中 去重 我一开始用 set 超时 #include <cstdio> #include ...
- Jenkins任务启动的后台进程被自动kill
在Jenkins的使用中,遇到过的一个场景是:在web代码更改之后,能自动的部署到测试服务器,我们写了run.sh脚本来重启服务,在使用Jenkins的任务自动跑这个脚本后发现,服务没有起来.开始以为 ...
- jquery 请求jsp传递json数据的方法
$(function() { $("a[name=hrefID]").click(function() { var id = $(this).attr("id" ...
- MySql Error: Can't update table in stored function/trigger
MySql Error: Can't update table in stored function/trigger because it is already used by statement w ...
- DLL搜索路径和DLL劫持
DLL搜索路径和DLL劫持 环境:XP SP3 VS2005 作者:magictong 为什么要把DLL搜索路径(DLL ORDER)和DLL劫持(DLL Hajack)拿到一起讲呢?呵呵,其实没啥深 ...
- DJANGO的ORM的Q查询作多字段外键的模糊查询样码
工作中用到的,存照一下. from django.db.models import Q if self.kwargs.has_key('search_pk'): search_pk = self.kw ...