https://oj.leetcode.com/problems/restore-ip-addresses/

string到int的ip地址格式化。

分别用 i+1,j+1,k+1,表示前三个地址段的数位的个数。之后判断每个地址段是否数字个数都是从1到3,并且数是从0到255,符合条件的即可。

如果一个地址段的第一位是0,则这个地址段只能是0.比如 010010,表示成0.10.0.10或者0.100.1.0

class Solution {
public:
int str2int( string str)
{
if(str[]==''&& str.size()>)
return ; //this is 01,010 and so on , no allowed
// if the first bit is 0 ,then it no allowed second bit
int ans = ;
for(int i = ;i<str.size();i++)
{
ans = ans*;
ans += str[i] - '';
}
return ans;
}
vector<string> restoreIpAddresses(string s) {
string strAnsPiece;
vector<string> ans;
if(s.size()> || s.size()<)
return ans; for(int i = ;i<=;i++)
{
string str1 = s.substr(,i+);
if(s.size()-i->)
continue;
if(str2int(str1)>)
break; for(int j = ;j<=;j++)
{
if( s.size() - i - j - >)
continue;
string str2 = s.substr(i+,j+);
if(str2int(str2)>)
break; for(int k = ;k<=;k++)
{
string str3 = s.substr(i+j+,k+);
if(str2int(str3)>)
break;
if((s.size() - i - j - k - >= )&& (s.size() - i - j - k - <= ))
{
string str4 = s.substr(i+j+k+,s.size()-i-j-k-);
if(str2int(str4)<=)
{
string ansPiece = str1;
ansPiece.append( ".");
ansPiece.append(str2);
ansPiece.append( ".");
ansPiece.append(str3);
ansPiece.append(".");
ansPiece.append(str4);
ans.push_back(ansPiece);
}
} }
}
}
return ans;
}
};

LeetCode OJ-- Restore IP Addresses的更多相关文章

  1. Leetcode OJ : Restore IP Addresses backtrack暴搜 C++ solution

    class Solution { public: vector<string> ret; string src; int len; unordered_set<string> ...

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

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

  3. 【leetcode】Restore IP Addresses

    Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...

  4. [LeetCode] 93. Restore IP Addresses 复原IP地址

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

  5. 【leetcode】Restore IP Addresses (middle)

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

  6. leetcode 93 Restore IP Addresses ----- java

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

  7. Java for LeetCode 093 Restore IP Addresses

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

  8. LeetCode : 93. Restore IP Addresses

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABZ4AAAHUCAYAAAC6Zj2HAAAMFGlDQ1BJQ0MgUHJvZmlsZQAASImVlw

  9. 【LeetCode】93. Restore IP Addresses

    Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...

  10. LeetCode: Restore IP Addresses 解题报告

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

随机推荐

  1. Vue基础指令集锦

    v-model双向绑定数据 <input type="text" v-model="msg"> {{msg}} ###v-on 事件 <div ...

  2. 使用GD库做图片水印

    png图片作为水印加到其他类型图片后,背景变黑色 原因: imagecopy函数拷贝时可以保留png图像的原透明信息,而imagecopymerge却不支持图片的本身的透明拷贝. 然后直接上代码: / ...

  3. 【php】命名空间的影响

    命名空间对代码的影响 类(包含抽象类和traits) 接口 常量 函数 ​

  4. Statues CodeForces - 129C(bfs)

    In this task Anna and Maria play a game with a very unpleasant rival. Anna and Maria are in the oppo ...

  5. JBuilder生成Exe

    首先保证工程可以通过绿箭头执行 然后在File菜单中选择New,先建立Archive下的Application 接下来的界面中大部分可以直接选择“Next”,除了下面的第3步,会询问是否需要将工程引用 ...

  6. poj2449 Remmarguts' Date K短路 A*

    K短路裸题. #include <algorithm> #include <iostream> #include <cstring> #include <cs ...

  7. 24、AES RSA加密处理记录

    一.加密过程解释 前提:发送方为A,接受方为B牢记:RSA为非对称加密,AES为对称加密.对称加密,属于传统的加密技术,加密和解密的秘钥都是相同的,AES的秘钥长度有128.192.256三种.非对称 ...

  8. Leetcode39--->Combination Sum(在数组中找出和为target的组合)

    题目: 给定一个数组candidates和一个目标值target,求出数组中相加结果为target的数字组合: 举例: For example, given candidate set [2, 3, ...

  9. PHP-7.1 源代码学习:字节码生成 之 "$a = 1"

    前言 本文通过分析 "$a=1" 这个 PHP 语句的编译和执行来窥探 php-cli 解释执行逻辑 准备 参考之前的系列文章,在 ubuntu 环境下下载,编译 PHP 源代码 ...

  10. Goal Oriented Action Planning for a Smarter AI

    Goal Oriented Action Planning for a Smarter AI by Brent Owens23 Apr 2014 Goal Oriented Action Planni ...