国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LintCode/LeetCode] Restore IP Addresses

bingo / 3007人閱讀

摘要:第一個分割點第二個分割點第三個分割點

Problem

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

Example

Given "25525511135", return

[
  "255.255.11.135",
  "255.255.111.35"
]

Order does not matter.

Solution

Ordinary DFS Method

public class Solution {
    public ArrayList restoreIpAddresses(String s) {
        ArrayList res = new ArrayList();
        if (s.length() < 4 || s.length() > 12) {
            return res;
        }
        dfs(s,"", res, 0);
        return res;
    }
    public void dfs(String s, String temp, ArrayList res, int count) {
       if (count == 3 && isvalid(s)) {
           res.add(temp + s);
           return;
       }
       for (int i = 1; i < 4 && i < s.length(); i++) {
           String substr = s.substring(0, i);
           if (isvalid(substr)) {
               dfs(s.substring(i), temp + substr + ".", res, count+1);
           }
       }
    }
    public boolean isvalid(String s) {
        if (s.charAt(0) == "0") {
            return s.equals("0");
        }
        int num = Integer.parseInt(s);
        return num > 0 && num <= 255;
    }
}

Advanced DFS Method

public class Solution {
    private List res;
    public List restoreIpAddresses(String s) {
        res = new ArrayList();
        String cur = "";
        dfs(s, cur, -1, 0);
        return res;
    }
    public void dfs(String s, String cur, int index, int len) {
        if (len == 4 && index == s.length()-1) {
            res.add(cur.substring(0, cur.length()-1));
            return;
        }
        int temp = 0;
        if (s.length()-1-index > 3*(4-len)) return;
        for (int i = 1; i <= 3; i++) {
            if (index+i >= s.length()) break;
            if (i == 1 && s.charAt(index+1) == "0") {
                dfs(s, cur+"0.", index+1, len+1);
                break;
            }
            temp = temp*10 + s.charAt(index+i)-"0";
            if (temp <= 255) dfs(s, cur + temp + ".", index+i, len+1);
        }
    }
}

Optimized Above DFS Method

public class Solution {
    public List restoreIpAddresses(String s) {
        List res = new ArrayList<>();
        dfs(s, 0, "", res);
        return res;
    }
    private void dfs(String s, int index, String cur, List res) {
        if (index == 4) res.add(cur.substring(0, cur.length() - 1));
        else {
            for (int i = 0; i < 3; i++) {
                if (s.length() - i - 1 < 3 - index || s.length() - i - 1 > (3 - index) * 3) continue;
                if (i > 0 && s.charAt(0) == "0") break;
                if (i == 2 && s.substring(0, 3).compareTo("255") > 0) break;
                dfs(s.substring(i + 1), index + 1, cur + s.substring(0, i + 1) + ".", res);
            }
        }
    }
}

A simple way... O(n^3)

public class Solution {
    public List restoreIpAddresses(String s) {
        List res = new LinkedList();
        int len = s.length();
        // 第一個分割點
        for(int i = 1; i < 4 && i < len - 2; i++){
            // 第二個分割點
            for(int j = i + 1; j < i + 4 && j < len - 1; j++){
                // 第三個分割點
                for(int k = j + 1; k < j + 4 && k < len ; k++){
                    String s1 = s.substring(0,i), s2 = s.substring(i, j), s3 = s.substring(j, k), s4 = s.substring(k, s.length());
                    if(isValid(s1)&&isValid(s2)&&isValid(s3)&&isValid(s4)) res.add(s1+"."+s2+"."+s3+"."+s4);
                }
            }
        }
        return res;
    }
    private boolean isValid(String sub){
        return sub.length()<=3 && ((sub.charAt(0) != "0" && Integer.valueOf(sub) <=255) || sub.equals("0"));
    }
}

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/65470.html

相關文章

  • LeetCode: 93. Restore IP Addresses

    摘要:以剩下的字符串,當前字符串,剩余單元數傳入下一次遞歸。結束條件字符串長度為,并且剩余單元數為 Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example:Given 25525511135, return [2...

    Shisui 評論0 收藏0
  • 93. Restore IP Addresses

    摘要:第一種解法,找出第一部分合法的剩余部分變成相似子問題。這里的特性是最大數字不能超過。比上個方法好的地方在于才會判斷數字是否合法,避免了很多這種不需要檢查的情況。 Given a string containing only digits, restore it by returning all possible valid IP address combinations. For e...

    andong777 評論0 收藏0
  • leetcode93. Restore IP Addresses

    摘要:題目要求返回字符串能夠組成的所有地址。思路與代碼地址由位二進制數字構成,一共分為個區間,每個區間位。那么我們只要劃分出這四個區間,然后判斷這四個區間的值是否符合標準即可。 題目要求 Given a string containing only digits, restore it by returning all possible valid IP address combinatio...

    chenjiang3 評論0 收藏0
  • [LeetCode] 93. Restore IP Addresses

    Problem Given a string containing only digits, restore it by returning all possible valid IP address combinations. Example: Input: 25525511135Output: [255.255.11.135, 255.255.111.35] Solution class So...

    xingqiba 評論0 收藏0
  • leetcode-93-Restore IP Addresses

    摘要:題目描述題目理解將一段字符廣度搜索截取,分別有種組合形式,添加限制條件,過濾掉不適合的組合元素。長度,大小,首字母應用如果進行字符串的子元素組合窮舉,可以應用。所有的循環,利用到前一個狀態,都可以理解為動態規劃的一種分支 題目描述:Given a string containing only digits, restore it by returning all possible va...

    wmui 評論0 收藏0

發表評論

0條評論

bingo

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<