# 复原 IP 地址

给定一个只包含数字的字符串，复原它并返回所有可能的 `IP` 地址格式。

示例:

```
输入: "25525511135"
输出: ["255.255.11.135", "255.255.111.35"]
```

```javascript
var restoreIpAddresses = function(s) {
    let fun = getObj();
    fun.findOut(s, []);
    return fun.res;
};

var getObj = function() {
    let res = [];
    function findOut(str, arr) {
        if (!str.length && arr.length === 4) return res.push(arr.join('.'));
        if (arr.length === 4) return;
        let temp = '', tempArr = str.split('');
        for (let i = 0; i < 3; i++) {
            temp = tempArr.pop() + temp;
            if (isValid(temp)) {
                let a = [...arr];
                a.unshift(temp);
                findOut(tempArr.join(''), a);
            }
        }
    }
    return {
        findOut,
        res
    }
}

var isValid = function(s) {
    return Number(s) < 256 && Number(s).toString() === s;
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://bvct1742.gitbook.io/one-book/knowledge/suan-fa/fu-yuan-ip-di-zhi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
