【算法与数据结构】93、LeetCode复原 IP 地址

所有的LeetCode题解索引,可以看这篇文章——【算法和数据结构】LeetCode题解

一、题目

在这里插入图片描述

二、解法

  思路分析:参照【算法与数据结构】131、LeetCode分割回文串的思路,需要将IP字符串进行分割,同时要对分割字符串的合法性进行判断。IP字符串一共有四个子串,前三个子串在for循环中找到,最后咋终止条件中判断第四个子串是否合法,如果合法则加入结果数组。
  程序如下

class Solution {
private:
	vector<string> result;
	int PointNum = 0;
	bool isValid(const string& s, int start, int end) {
		if (start > end) return false;	// start>end的数字不合法
		if (s[start] == '0' && start!=end) return false;	// 0开头的数字不合法		
		int num = 0;
		for (int i = start; i <= end; i++) {
			if (s[i] < '0' || s[i]>'9') return false;
			num = num * 10 + (s[i] - '0');
			if (num > 255) return false;
		}
		return true;
	}
	void backtracking(string& s, int startIndex) {
		if (PointNum == 3) {
			if(isValid(s, startIndex, s.size()-1)) result.push_back(s);	// 判断最后一个子串是否合法,如果合法直接加入结果数组	
			return;
		}
		for (int i = startIndex; i < s.size(); i++) { 
			if (isValid(s, startIndex, i)) {	// 判断子串是否合法
				s.insert(s.begin() + i + 1, '.');	// 插入分隔符
				PointNum++;
				backtracking(s, i + 2);			// 递归
				PointNum--;
				s.erase(s.begin() + i + 1);	// 回溯
			}
			else break;			
		}
	}
public:
	vector<string> restoreIpAddresses(string s) {
		backtracking(s, 0);
		return result;
	}
};

复杂度分析:

  • 时间复杂度: O ( 3 4 ) O(3^4) O(34), IP地址一共包含四个子串,相当于递归的深度,每个子串有三种分割方式,因此最终时间复杂度为 O ( 3 4 ) O(3^4) O(34)
  • 空间复杂度: O ( n ) O(n) O(n)

三、完整代码

# include <iostream>
# include <string>
# include <vector>
using namespace std;

class Solution {
private:
	vector<string> result;
	int PointNum = 0;
	bool isValid(const string& s, int start, int end) {
		if (start > end) return false;	// start>end的数字不合法
		if (s[start] == '0' && start!=end) return false;	// 0开头的数字不合法		
		int num = 0;
		for (int i = start; i <= end; i++) {
			if (s[i] < '0' || s[i]>'9') return false;
			num = num * 10 + (s[i] - '0');
			if (num > 255) return false;
		}
		return true;
	}
	void backtracking(string& s, int startIndex) {
		if (PointNum == 3) {
			if(isValid(s, startIndex, s.size()-1)) result.push_back(s);	// 判断最后一个子串是否合法,如果合法直接加入结果数组	
			return;
		}
		for (int i = startIndex; i < s.size(); i++) { 
			if (isValid(s, startIndex, i)) {	// 判断子串是否合法
				s.insert(s.begin() + i + 1, '.');	// 插入分隔符
				PointNum++;
				backtracking(s, i + 2);			// 递归
				PointNum--;
				s.erase(s.begin() + i + 1);	// 回溯
			}
			else break;			
		}
	}
public:
	vector<string> restoreIpAddresses(string s) {
		backtracking(s, 0);
		return result;
	}
};

int main() {
	Solution s1;
	string s = "25525511135";
	vector<string> result = s1.restoreIpAddresses(s);
	for (vector<string>::iterator jt = result.begin(); jt != result.end(); jt++) {
		cout << *jt << endl;
	}
	cout << endl;
	system("pause");
	return 0;
}

end

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晚安66

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值