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 ...
随机推荐
- 你不需要jQuery
http://www.webhek.com/you-do-not-need-jquery
- PAT-乙级-1001. 害死人不偿命的(3n+1)猜想 (15)
1001. 害死人不偿命的(3n+1)猜想 (15) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 卡拉兹(Ca ...
- Bypass Preventing CSRF
CSRF在过去的n年(n>2)一直都火,在bh/defcon/owasp等会议上多次探讨CSRF的攻防[具体你可以看看以往的那些pp].前 段时间PLAYHACK.net上发表了一个总结性的pp ...
- android中的category静态值(转)
提供将要执行的action的额外信息,一般在隐式地启动activity时需要用到.常见的category如下 CATEGORY_ALTERNATIVE 设置这个activity是否可以被认为是用户正在 ...
- spoj 416
又臭又长的烂代码 ...... #include <iostream> #include <cstdio> #include <cstring> #include ...
- 重新学struct,边界对齐,声明……与Union的区别
在内存中,编译器按照成员列表顺序分别为每个结构体变量成员分配内存,当存储过程中需要满足边界对齐的要求时,编译器会在成员之间留下额外的内存空间. 如果想确认结构体占多少存储空间,则使用关键字sizeof ...
- IDS 日志分析
[http://blog.csdn.net/cnbird2008/article/details/5792626] General Approach通用方法1. Identify which log ...
- ZOJ 3778 Talented Chef
题目链接 题意 : 这个人需要做n道菜,每道菜Ai步,他可以同时做M道不同的菜的其中一步,问你最少需要多少时间能做完所有的菜. 思路 : 这个题比赛的时候禁锢思路了,根本没想出来,就是当M > ...
- hdu 4336 Card Collector 容斥原理
读完题目就知道要使用容斥原理做! 下面用的是二进制实现的容斥原理,详见:http://www.cnblogs.com/xin-hua/p/3213050.html 代码如下: #include< ...
- Java中List的排序
第一种方法,就是list中对象实现Comparable接口,代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 2 ...