# [93. Restore IP Addresses (Medium)](https://leetcode.com/problems/restore-ip-addresses)
<p>A <strong>valid IP address</strong> consists of exactly four integers separated by single dots. Each integer is between <code>0</code> and <code>255</code> (<strong>inclusive</strong>) and cannot have leading zeros.</p>
<ul>
<li>For example, <code>"0.1.2.201"</code> and <code>"192.168.1.1"</code> are <strong>valid</strong> IP addresses, but <code>"0.011.255.245"</code>, <code>"192.168.1.312"</code> and <code>"192.168@1.1"</code> are <strong>invalid</strong> IP addresses.</li>
</ul>
<p>Given a string <code>s</code> containing only digits, return <em>all possible valid IP addresses that can be formed by inserting dots into </em><code>s</code>. You are <strong>not</strong> allowed to reorder or remove any digits in <code>s</code>. You may return the valid IP addresses in <strong>any</strong> order.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "25525511135"
<strong>Output:</strong> ["255.255.11.135","255.255.111.35"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "0000"
<strong>Output:</strong> ["0.0.0.0"]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "101023"
<strong>Output:</strong> ["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>s</code> consists of digits only.</li>
</ul>
**Companies**:
[Yandex](https://leetcode.com/company/yandex), [TikTok](https://leetcode.com/company/tiktok), [Oracle](https://leetcode.com/company/oracle), [Amazon](https://leetcode.com/company/amazon), [Microsoft](https://leetcode.com/company/microsoft), [Yahoo](https://leetcode.com/company/yahoo), [Google](https://leetcode.com/company/google), [Facebook](https://leetcode.com/company/facebook), [Apple](https://leetcode.com/company/apple), [ByteDance](https://leetcode.com/company/bytedance), [Cisco](https://leetcode.com/company/cisco), [Arista Networks](https://leetcode.com/company/arista-networks), [Verkada](https://leetcode.com/company/verkada)
**Related Topics**:
[String](https://leetcode.com/tag/string), [Backtracking](https://leetcode.com/tag/backtracking)
**Similar Questions**:
* [IP to CIDR (Medium)](https://leetcode.com/problems/ip-to-cidr)
## Solution 1. Backtracking
Plain backtracking. Maximum depth is 4. Be aware of the cases where ip segment starts with `0` or ip segment is greater than `255`.
```cpp
// OJ: https://leetcode.com/problems/restore-ip-addresses
// Author: github.com/lzl124631x
// Time: O(1)
// Space: O(1)
class Solution {
public:
vector<string> restoreIpAddresses(string s) {
vector<string> ans;
vector<int> tmp;
function<void(int)> dfs = [&](int i) {
if (i == s.size()) {
if (tmp.size() == 4) {
string ip;
for (int n : tmp) {
if (ip.size()) ip += '.';
ip += to_string(n);
}
ans.push_back(ip);
}
return;
}
for (int j = 0, n = 0; j < (s[i] == '0' ? 1 : 3) && i + j < s.size(); ++j) {
n = n * 10 + s[i + j] - '0';
if (n > 255) break;
tmp.push_back(n);
dfs(i + j + 1);
tmp.pop_back();
}
};
dfs(0);
return ans;
}
};
```
Mopes__
- 粉丝: 2993
- 资源: 648
最新资源
- 基于FPGA的占空比测量模块-verilog语言
- c语言一个简单的线程池,实现不超过100行.zip
- C语言- 简易三子棋.zip
- 计算机网络课设-协议及流程分析
- COStream 工具在动态类型语言 js 上部署,目的是代码定制 & 易读 & 易测试.zip
- COIMS是应西安某救助站需求,义务为其开发的简易流浪者病历管理软件,基于GTK+2.24版本、SQLite3并采用C语言开发 不以成败论英雄 .zip
- CN编程语言,一个快速、便捷、易学的中文编程语言.zip
- 迈微88NV1120量产工具
- 伺服电机选型的技术指导及其应用场景
- CMM(C语言的一个简单版本)语言的解释器(用C++编写).zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈