C++ 中的 IP 地址还原
假设我们有一个只包含数字的字符串,我们需要通过返回所有可能的有效 IP 地址组合来还原它。我们知道有效的 IP 地址由四个整数(每个整数的范围为 0 到 255)组成,并用单个点隔开。
因此,如果输入类似于“25525511135”,则输出将为 ["255.255.11.135", "255.255.111.35"]
为了解决这个问题,我们将遵循以下步骤:
定义一个函数 `convertToNum()`,它将接收 `s`、`start` 和 `end`。
num := 0
for i := start to end do −
num := (num * 10) + (s[i] - '0' 的 ASCII 码)
if num > 255 then −
return 10000
return num
定义一个函数 `addDots()`,它将接收 `positions`。
res := 空字符串
x := 0, posIndex := 0
for i := 0 to positions 的大小 -1 do −
num := positions[i]
创建一个字符串 str1
temp := num 转换为字符串
res := res + temp
if i < positions 的大小 -1 then −
res := res + "."
return res
定义一个函数 `solve()`,它将接收 `s`、一个字符串数组 `result`、一个数组 `positions`、`dotCount`(初始化为 3)、`startIndex`(初始化为 0)。
if dotCount != 0 and ((s 的大小 - 1) - startIndex + 1) >= dotCount + 1 then −
temp := convertToNum(s, startIndex, s 的大小 -1)
if temp >= 0 and temp <= 255 then−
将 temp 添加到 positions 的末尾
res := addDots(positions)
if res 的大小 == s 的大小 then −
将 res 添加到 result 的末尾
return
for i := startIndex to s 的大小 -1 do −
temp := convertToNum(s, startIndex, i)
if temp >= 0 and temp <= 255 then −
将 temp 添加到 positions 的末尾
solve(s, result, positions, dotCount - 1, i + 1)
从 positions 中删除最后一个元素
定义一个函数 `genIp()`,它将接收一个字符串 `s`。
定义一个数组 `result`
定义一个数组 `position`
solve(s, result, position)
return result
从主方法调用 `genIp(A)`
示例
让我们看看下面的实现来更好地理解:
#include <bits/stdc++.h>
using namespace std;
void print_vector(vector<auto> v){
cout << "[";
for(int i = 0; i<v.size(); i++){
cout << v[i] << ", ";
}
cout << "]"<<endl;
}
typedef long long int lli;
class Solution {
public:
lli convertToNum(string s,int start, int end){
lli num = 0;
for (int i = start; i <= end; i++) {
num = (num * 10) + (s[i] - '0');
if (num > 255)
return 10000;
}
return num;
}
string addDots(vector <int> positions){
string res = "";
int x = 0;
int posIndex = 0;
for (int i = 0; i < positions.size(); i++) {
int num = positions[i];
ostringstream str1;
str1 << num;
string temp = str1.str();
res += temp;
if (i < positions.size() - 1)
res += ".";
}
return res;
}
void solve(string s, vector <string> &result,vector <int> positions, int dotCount = 3, int startIndex = 0){
if (!dotCount && ((s.size() - 1) - startIndex + 1) >= 1) {
int temp = convertToNum(s, startIndex, s.size() - 1);
if (temp >= 0 && temp <= 255) {
positions.push_back(temp);
string res = addDots(positions);
if (res.size() - 3 == s.size()) {
result.push_back(res);
}
}
return;
}
for (int i = startIndex; i < s.size(); i++) {
int temp = convertToNum(s, startIndex, i);
if (temp >= 0 && temp <= 255) {
positions.push_back(temp);
solve(s, result, positions, dotCount - 1, i + 1);
positions.pop_back();
}
}
}
vector<string> genIp(string s){
vector<string> result;
vector<int> position;
solve(s, result, position);
return result;
}
vector<string> restoreIpAddresses(string A) {
return genIp(A);
}};
main(){
Solution ob;
print_vector(ob.restoreIpAddresses("25525511135"));
}输入
"25525511135"
输出
[255.255.11.135, 255.255.111.35, ]
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP