Given a string containing only digits, restore it by returning all possible valid IP address combinations.

For example:
Given "25525511135",

return ["255.255.11.135", "255.255.111.35"]. (Order does not matter)

我的答案如下:

 class Solution {
public: vector<string> restoreIpAddresses(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int i=,j,k;
int len = s.size();
vector<string> result;
if (len> || len< )
return result;
for(i;i<len-;i++){
for(j=i+;j<len-;j++){
for(k=j+;k<len-;k++){
string s1 = s.substr(,i+);
string s2 = s.substr(i+,j-i);
string s3 = s.substr(j+,k-j);
string s4 = s.substr(k+);
if(isOK(s1) && isOK(s2) && isOK(s3) && isOK(s4)){
string s5 = s1 + "." + s2 + "." + s3 + "." + s4;
result.push_back(s5);
}
}
}
}
return result;
} bool isOK(string s){
int len = s.size();
if (len> || len <)
return false;
else if (==len)
return true;
else if (==len){
return (''!=s.at());
}else{//3==len
int a = (s.at()-'')* + (s.at()-'')*+(s.at()-'');
return (a>= && a<=);
}
}
};

我的答案

思路:遍历所有的可能性,因为最多也就是个长度为12的字符串,三层循环也不用考虑复杂度的问题。把parse出来的三个string检查一下是否为0至255之间的整数,如果四个都满足,那就是这个题目的一个解。主要首位数不能为0,例如03,012这样的不算是在0至255之间。

[leetcode.com]算法题目 - Restore IP Addresses的更多相关文章

  1. Leetcode 22. Generate Parentheses Restore IP Addresses (*) 131. Palindrome Partitioning

    backtracking and invariant during generating the parathese righjt > left  (open bracket and cloas ...

  2. LeetCode(93) Restore IP Addresses

    题目 Given a string containing only digits, restore it by returning all possible valid IP address comb ...

  3. LeetCode之“字符串”:Restore IP Addresses

    题目链接 题目要求: Given a string containing only digits, restore it by returning all possible valid IP addr ...

  4. leetcode 93. Restore IP Addresses(DFS, 模拟)

    题目链接 leetcode 93. Restore IP Addresses 题意 给定一段序列,判断可能组成ip数的所有可能集合 思路 可以采用模拟或者DFS的想法,把总的ip数分成四段,每段判断是 ...

  5. LeetCode: Restore IP Addresses 解题报告

    Restore IP Addresses My Submissions Question Solution Given a string containing only digits, restore ...

  6. 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 ...

  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 -day29 Binary Tree Inorder Traversal &amp; Restore IP Addresses

    1.  Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' ...

随机推荐

  1. 移动开发学习touchmove

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  2. python中的open( )函数

    函数原型 open(file, mode=‘r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True) buff ...

  3. 【Java】Maven Tomcat插件使用

    本例是用的是tomcat7-maven-plugin 插件 依赖 tomcat7-maven-plugin 插件的pom.xml依赖为 <dependency> <groupId&g ...

  4. 导入mysql报错问题

    今天数据导入报错:Got a packet bigger than‘max_allowed_packet’bytes的问题 2个解决方法: 1.临时修改:mysql>set global max ...

  5. sql在最后一行添加合计

    select nvl(sno,'合计') sno,sum(score) score from sc group by rollup(sno);

  6. 使用Hadoop API 压缩HDFS文件

    下篇解压缩:使用Hadoop API 解压缩 HDFS文件 起因: 集群磁盘剩余空间不足. 删除了存储在HDFS上的,一定时间之前的中间结果,发现并不能释放太多空间,查看计算业务,发现,每天的日志存在 ...

  7. shell 报错 /bin/bash^M: bad interpreter: No such file or directory

    shell 执行报错: ./test.sh: /bin/bash^M: bad interpreter: No such file or directory 原因:window和linux 的文件格式 ...

  8. 解决css3不支持同时缩放和旋转的办法

    设置两个div,外层scale,内层rotate.

  9. (16)The beauty of what we'll never know

    https://www.ted.com/talks/pico_iyer_the_beauty_of_what_we_ll_never_know/transcript 00:13One hot Octo ...

  10. ip route 解释

    [root@localhost ~]# ip route default via 172.16.0.1 dev ens192 proto static metric 100 172.16.0.0/16 ...