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

Example:

Input: "25525511135"
Output: ["255.255.11.135", "255.255.111.35"]
class Solution {
public List<String> restoreIpAddresses(String s) {
List<String> res = new ArrayList<>();
helper(res, 0, 0, s, "");
return res;
} private void helper(List<String> res, int count, int index, String s, String str) {
if (count > 4) {
return;
}
if (count == 4 && index == s.length()) {
res.add(str);
}
for (int i = 1; i < 4; i++) {
if (index + i > s.length()) {
break;
}
String cur = s.substring(index, index + i);
if (cur.charAt(0) == '0' && cur.length() > 1 || cur.length() == 3 && Integer.parseInt(cur) > 255) {
continue;
}
String signal = count == 3 ? "": ".";
helper(res, count + 1, index + i, s, str + cur + signal);
}
} }

[LC] 93. Restore IP Addresses的更多相关文章

  1. 93.Restore IP Addresses(M)

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

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

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

  3. 93. Restore IP Addresses

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

  4. 【LeetCode】93. Restore IP Addresses

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

  5. 93. Restore IP Addresses产生所有可能的ip地址

    [抄题]: Given a string containing only digits, restore it by returning all possible valid IP address c ...

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

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

  7. 【一天一道LeetCode】#93. Restore IP Addresses

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  8. LeetCode OJ 93. Restore IP Addresses

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

  9. 93. Restore IP Addresses(dfs)

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

随机推荐

  1. [BJDCTF2020]Mark loves cat

    0x00 知识点 GitHack读取源码 $$会导致变量覆盖漏洞 0x01解题 dirsearch扫描一下,发现/.git目录,用githack获取一下源码. <?php include 'fl ...

  2. 使用文件流创建File文件和目录以及其他的一些操作

    我们创建文件时可以直接通过File f=new File(path)来创建一个文件对象,然后再通过 f.createNewFile() 就创建出来了一个文件.比如设置 path 为 C:\Users\ ...

  3. unzip 小坑

    unzip test.zip 直接将zip解压到当前目录下,保留test级目录. unzip test.war 直接将.war解压到当前目录,不保留test级目录,所以建议使用 unzip test. ...

  4. LVS DR模式搭建、keepalived+LVS搭建介绍

    参考文献 http://blog.51cto.com/taoxie/2066993 疑问: 1.为什么要修改RealServer的返回arp响应和发送arp请求参数  echo "1&quo ...

  5. webview HttpClient 怎么保持会话session统一

      cookies session均为key---value的形式展示,  1.    session是存储在服务端,并有一块区域控件存储用户信息,主要是为了判断该用户是否登录,在客户端采用httpC ...

  6. 吴裕雄--天生自然 JAVASCRIPT开发学习:函数调用

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. MacOS常用快捷键

    command+空格 打开Spotlight command+m 最小化当前窗口 control+command+f     最大化当前窗口 command+q                    ...

  8. 201512-2 消除类游戏 Java

    思路: 用二维数组,对于每一个棋子,向右看三个,向下看三个,如果相等则置为负数,最后遍历输出. import java.util.Scanner; public class Main { public ...

  9. 01 语言基础+高级:1-5 常用API第二部分_day01.【Object类、常用API: Date类、System类、StringBuilder类】

    day01[Object类.常用API] 主要内容 Object类 Date类 DateFormat类 Calendar类 System类 StringBuilder类 包装类 java.lang.O ...

  10. android implementation 依赖第三方库

    依赖第三方库