# 代码

class Solution {
public:
vector<string> result;
small(s, "", 1, result);
return result;
}

void small(const string & remain, const string & prefix, int part, vector<string> & result) {
if (remain.size() == 0) {
return;
}
if (part == 4) {
if (remain.size() > 3) {
return;
}
if (!is_valid(remain)) {
return;
}
result.push_back(prefix + remain);
return;
}

for (int i = 1; i <= 3 && i < remain.size(); ++i) {
string prefix_tmp = remain.substr(0, i);
if (!is_valid(prefix_tmp)) {
break;
}
prefix_tmp = prefix + prefix_tmp + ".";
string remain_tmp = remain.substr(i);
small(remain_tmp, prefix_tmp, part + 1, result);
}
return;
}

bool is_valid(const string & s) {
int tmp = stoi(s);
if (tmp < 0 || tmp > 255) {
return false;
}
if (tmp != 0 && s[0] == '0') {
return false;
}
if (tmp == 0 && s.size() != 1) {
return false;
}
return true;
}
};

• 本文已收录于以下专栏：

## LeetCode 93. Restore IP Addresses

1. 题目描述 Given a string containing only digits, restore it by returning all possible valid IP addre...

## LeetCode 93. Restore IP Addresses 解题报告

LeetCode 93. Restore IP Addresses 解题报告 题目描述 Given a string containing only digits, restore it by r...

## Leetcode 93. Restore IP Addresses

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