zoukankan      html  css  js  c++  java
  • [LC] 93. Restore IP Addresses

    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);
            }
        }
        
    }
  • 相关阅读:
    浏览器切换窗口事件
    icheck的使用
    datetimepicker —— 日期选择控件
    apktool
    生成证书
    Jadx
    js call、apply和bind
    1号台风来了
    Linux内核编译和测试
    华中科技大学电子地图
  • 原文地址:https://www.cnblogs.com/xuanlu/p/12321237.html
Copyright © 2011-2022 走看看