zoukankan      html  css  js  c++  java
  • Restore IP Addresses

     1 public class Solution {
     2     public static ArrayList<String> restoreIpAddresses(String s) {
     3         // Start typing your Java solution below
     4         // DO NOT write main() function
     5         ArrayList<String> result = new ArrayList<String>();
     6         if (s == null || s.length() == 0) {
     7             return result;
     8         }
     9         int depth = 0, start = 0;
    10         String ip = "";
    11         generate(s, start, depth, result, ip);
    12 
    13         return result;
    14     }
    15 
    16     private static void generate(String s, int start, int depth,
    17             ArrayList<String> result, String ip) {
    18         // max = 3 check it
    19         if ((s.length() - start) > (4 - depth) * 3) {
    20             return;
    21         }
    22         // min = 1 check it
    23         if (s.length() - start < 4 - depth) {
    24             return;
    25         }
    26         if (depth == 4) {
    27             ip = ip.substring(0, ip.length() - 1);
    28             if(!result.contains(ip))
    29                 result.add(ip);
    30             return;
    31         }
    32 
    33         int num = 0;
    34         for (int i = start; i < Math.min(start + 3, s.length()); i++) {
    35             num = num * 10 + (s.charAt(i) - '0');
    36             if (num <= 255) {
    37                 generate(s, i + 1, depth + 1, result, ip + num + ".");
    38             }
    39             if(num == 0){
    40                 break;
    41             }
    42         }
    43     }
    44 }
  • 相关阅读:
    寻找重复数
    除自身以外数组的乘积
    汇总区间
    Atlas 分表功能
    Atlas 读写分离 & Atlas + MHA 故障自动恢复
    MHA 的 Binlog Server & VIP 漂移
    MHA 高可用介绍
    MySQL 主从复制(下)
    MySQL 基础面试题
    MySQL 主从复制(上)
  • 原文地址:https://www.cnblogs.com/jasonC/p/3432845.html
Copyright © 2011-2022 走看看