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)

Solution:

注意这里if(tmp < 256 && tmp.toString().equals(s.substring(pos, pos + j)))

如果不加tmp.toString().equals(s.substring(pos, pos + j)), 则可能会出现001 这种情况, 即高位为0.

 public class Solution {
ArrayList<String> result = null;
public ArrayList<String> restoreIpAddresses(String s) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
result = new ArrayList<String>();
getAddress(s, 0, 4, new StringBuffer());
return result;
}
public void getAddress(String s, int pos, int num, StringBuffer sb){
if(num == 0 || pos == s.length()){
if(pos == s.length() && num == 0){
result.add(sb.substring(0, sb.length() - 1));
}
return;
}
for(int j = 1; j < 4 && pos + j <= s.length(); j ++){
Integer tmp = Integer.valueOf(s.substring(pos, pos + j));
if(tmp < 256 && tmp.toString().equals(s.substring(pos, pos + j))){
sb.append(s.substring(pos, pos + j));
sb.append(".");
getAddress(s, pos + j, num - 1, sb);
sb.delete(sb.length() - j - 1, sb.length());
}
}
}
}

Restore IP Addresses的更多相关文章

  1. 【leetcode】Restore IP Addresses

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

  2. 93. Restore IP Addresses

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

  3. 93.Restore IP Addresses(M)

    93.Restore IP Addresses Medium 617237FavoriteShare Given a string containing only digits, restore it ...

  4. 【LeetCode】93. Restore IP Addresses

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

  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 22. Generate Parentheses Restore IP Addresses (*) 131. Palindrome Partitioning

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

  8. 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' ...

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

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

  10. [LeetCode] Restore IP Addresses 复原IP地址

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

随机推荐

  1. ThinkPHP中的内置标签

    ThinkPHP中的内置标签 1.内置标签分类 闭合标签 <tag></tag> 开放标签 <tag /> 2.包含文件标签 主要功能:实现对文件的包含(类似于re ...

  2. 开始进入Windows Phone 8开发

    和大家一起分享从零开始,入手Windows Phone 8开发,大家一起来吧!

  3. javascript里面技巧整理

    web develop tools secrets: http://jinlong.github.io/blog/2013/08/29/devtoolsecrets/ 1.Date new Date( ...

  4. QQ互联OAuth2.0 .NET SDK 发布以及网站QQ登陆示例代码(转)

    OAuth: OAuth(开放授权)是一个开放标准,允许用户授权第三方网站访问他们存储在另外的服务提供者上的信息,而不需要将用户名和密码提供给第三方网站或分享他们数据的所有内容. QQ登录OAuth2 ...

  5. C#中Split函数的使用

    Split函数 描述 :返回一个下标从零开始的一维数组,它包含指定数目的子字符串. 语法 :Split(expression[,   delimiter[,   count[,   compare]] ...

  6. struts2 知识梳理

    写此文章时,最新struts2版本:2.3.6 一:struts.xml配置详解: 1.<include> 表示引入其他配置文件 2.<constant> 定义常量 3.< ...

  7. 【PHP】phpcms 关联连接修复

    function _keylinks($txt, $replacenum = '',$link_mode = 1) { $keywords = $this->data['keywords']; ...

  8. MAC上 nodejs express 安装

    最近在MAC上搭建 nodejs环境以及安装 express 框架,遇到了一些问题,不过最后总算还是安装成功了,下面是操作步骤 1.node js 安装 访问nodejs官网进入下载mac上的安装包 ...

  9. openerp - asterisk connector(转载)

    原文:http://www.akretion.com/open-source-contributions/openerp-asterisk-voip-connector OpenERP - Aster ...

  10. SQL Server 2014 Always on ON Microsoft Azure New Portal(1)

    以前假如需要在Azure IaaS 创建的SQL Server AlwaysOn 需要参考以下的步骤 From the MVPs: SQL Server High Availability in Wi ...