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. 8款耀眼的jQuery/HTML5焦点图滑块插件

    1.HTML5/CSS3超酷焦点图特效 带前后翻页按钮 今天要分享的这款HTML5/CSS3焦点图插件切换效果比较简单,但是外观和功能却十分强大.该CSS3焦点图在切换图片时,图片以淡入淡出的方式缩小 ...

  2. 济南学习 Day 4 T2 am

    LYK 与实验室(lab)Time Limit:5000ms Memory Limit:64MB题目描述LYK 在一幢大楼里,这幢大楼共有 n 层,LYK 初始时在第 a 层上.这幢大楼有一个秘密实验 ...

  3. thymeleaf 局部变量、属性优先级、注释

    九.局部变量(local variable) 之前在th:each中遇到过局部变量 <tr th:each="prod : ${prods}"> ... </tr ...

  4. CentOS如何分区

    安装Linux CentOS时,需要在硬盘建立CentOS使用的分区,在大多情况下,至少需要为CentOS建立以下3个分区. (1)/boot分区(不是必须的):/boot分区用于引导系统,它包含了操 ...

  5. Python开发【第一篇】Python模块中特殊变量

    模块中特殊变量 生产环境中,常用的就是__name__和__file__ __doc__ __package__ __cached__ __name__ __file__ 一. __doc__  #获 ...

  6. 记一次Surface Pro 2还原操作

    因为要做Azure的一个case,对自己的域环境下直接进行了捕获.结果导致机器直接crash. 重启后使用本地账号登陆后发现所有Win 8 的App都无法使用,包括进入设置中还原方式也无法使用. 可以 ...

  7. Java eclipse export jar包 包括第三方引入的jar

    1.安装fatjar插件 2.export jar 说明:安装后,操作说明以官网为准,不同的版本会有不同的右键菜单,我的版本(Eclipse Java EE IDE for Web Developer ...

  8. MySQL 多实例数据库还原脚本-备份集与端口对应

    版本:5.5.14 OS: ConetOS 6.3 1.创建recover.sh [root@yoon  export]# vi  recover.sh #!/bin/bash bakdir=/exp ...

  9. Eclipse C/C++开发环境搭建

    1 Eclipse的安装 到http://java.sun.com/j2se/1.5.0/download.jsp 下载JRE安装: 到http://eclipse.org下载Eclipse安装.(这 ...

  10. nginx error_page 404 用 php header 无法跳转

    nginx error_page 404 用 php header 无法跳转 之前用Apache的时候,只需要设置 ErrorDocument 404 /404.php 就可以在 404.php 中根 ...