当前位置: 首页 > news >正文

b2c外贸网站开发上海关键词推广

b2c外贸网站开发,上海关键词推广,福州晋安区建设局网站,专门做母婴的网站代码随想录二刷 | 回溯 |复原IP地址 题目描述解题思路代码实现 题目描述 93.复原IP地址 给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式。 有效的 IP 地址 正好由四个整数(每个整数位于 0 到 255 之间组成&am…

代码随想录二刷 | 回溯 |复原IP地址

  • 题目描述
  • 解题思路
  • 代码实现

题目描述

93.复原IP地址

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

有效的 IP 地址 正好由四个整数(每个整数位于 0 到 255 之间组成,且不能含有前导 0),整数之间用 ‘.’ 分隔。

例如:“0.1.2.201” 和 “192.168.1.1” 是 有效的 IP 地址,但是 “0.011.255.245”、“192.168.1.312” 和 “192.168@1.1” 是 无效的 IP 地址。

示例 1:

  • 输入:s = “25525511135”
  • 输出:[“255.255.11.135”,“255.255.111.35”]

示例 2:

  • 输入:s = “0000”
  • 输出:[“0.0.0.0”]

示例 3:

  • 输入:s = “1111”
  • 输出:[“1.1.1.1”]

示例 4:

  • 输入:s = “010010”
  • 输出:[“0.10.0.10”,“0.100.1.0”]

示例 5:

  • 输入:s = “101023”
  • 输出:[“1.0.10.23”,“1.0.102.3”,“10.1.0.23”,“10.10.2.3”,“101.0.2.3”]

提示:

  • 0 <= s.length <= 3000
  • s 仅由数字组

解题思路

递归三部曲

  • 递归函数的参数
    startIndex一定是需要的,因为不能重复分割,记录下一层递归分割的起始位置。

    本题我们还需要一个变量pointNum,记录添加逗点的数量。

    vector<string> result;
    void backtracking(string& s, int startIndex, int pointNum) 
    
  • 递归终止条件
    本题明确要求只会分成4段,所以不能用切割线切到最后作为终止条件,而是分割的段数作为终止条件。

    pointNum表示逗点数量,pointNum为3说明字符串分成了4段了。

    然后验证一下第四段是否合法,如果合法就加入到结果集里

    if (pointNum == 3) {if (isValid(s, startIndex, s.size() - 1)) {result.push_back(s);}return;
    }
    
  • 单层搜索的逻辑
    for (int i = startIndex; i < s.size(); i++)循环中 [startIndex, i] 这个区间就是截取的子串,需要判断这个子串是否合法。

    如果合法就在字符串后面加上符号.表示已经分割,如果不合法就结束本层循环。

    然后就是递归和回溯的过程:

    递归调用时,下一层递归的startIndex要从i+2开始(因为需要在字符串中加入了分隔符.),同时记录分割符的数量pointNum 要 +1。

    回溯的时候,就将刚刚加入的分隔符. 删掉就可以了,pointNum也要-1。

    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);pointNum--;s.erase(s.begin() + i + 1);} else break;
    } 
    

    最后就是在写一个判断段位是否是有效段位了。

    主要考虑到如下三点:

  • 段位以0为开头的数字不合法

  • 段位里有非正整数字符不合法

  • 段位如果大于255了不合法

// 判断字符串s在左闭又闭区间[start, end]所组成的数字是否合法
bool isValid(const string& s, int start, int end) {if (start > end) {return false;}if (s[start] == '0' && start != end) {return false;}int num = 0;for (int i = start; i <= end; i++) {if (s[i] > '9' || s[i] < '0') {return false;}num = num * 10 + (s[i] - '0');if (num > 255) {return false;}}return true;
}

代码实现

class Solution {
private:vector<string> result;// 记录结果// startIndex: 搜索的起始位置,pointNum:添加逗点的数量void backtracking(string& s, int startIndex, int pointNum) {if (pointNum == 3) { // 逗点数量为3时,分隔结束// 判断第四段子字符串是否合法,如果合法就放进result中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)) { // 判断 [startIndex,i] 这个区间的子串是否合法s.insert(s.begin() + i + 1 , '.');  // 在i的后面插入一个逗点pointNum++;backtracking(s, i + 2, pointNum);   // 插入逗点之后下一个子串的起始位置为i+2pointNum--;                         // 回溯s.erase(s.begin() + i + 1);         // 回溯删掉逗点} else break; // 不合法,直接结束本层循环}}// 判断字符串s在左闭又闭区间[start, end]所组成的数字是否合法bool isValid(const string& s, int start, int end) {if (start > end) {return false;}if (s[start] == '0' && start != end) { // 0开头的数字不合法return false;}int num = 0;for (int i = start; i <= end; i++) {if (s[i] > '9' || s[i] < '0') { // 遇到非数字字符不合法return false;}num = num * 10 + (s[i] - '0');if (num > 255) { // 如果大于255了不合法return false;}}return true;}
public:vector<string> restoreIpAddresses(string s) {result.clear();if (s.size() < 4 || s.size() > 12) return result; // 算是剪枝了backtracking(s, 0, 0);return result;}
};

文章转载自:
http://invariablenes.xnLj.cn
http://nocturnality.xnLj.cn
http://craquelure.xnLj.cn
http://infected.xnLj.cn
http://verus.xnLj.cn
http://aviva.xnLj.cn
http://anticonvulsant.xnLj.cn
http://eff.xnLj.cn
http://stepney.xnLj.cn
http://lengthman.xnLj.cn
http://cockeye.xnLj.cn
http://omuda.xnLj.cn
http://jubbah.xnLj.cn
http://convertor.xnLj.cn
http://moselle.xnLj.cn
http://emmenology.xnLj.cn
http://thuringia.xnLj.cn
http://fandangle.xnLj.cn
http://posturize.xnLj.cn
http://pensive.xnLj.cn
http://amyloid.xnLj.cn
http://muckworm.xnLj.cn
http://epicondylian.xnLj.cn
http://unidentifiable.xnLj.cn
http://affectlessness.xnLj.cn
http://gabbroid.xnLj.cn
http://marasmic.xnLj.cn
http://ridicule.xnLj.cn
http://embryology.xnLj.cn
http://jot.xnLj.cn
http://porkling.xnLj.cn
http://autoaggressive.xnLj.cn
http://protrudent.xnLj.cn
http://chemopsychiatry.xnLj.cn
http://whingding.xnLj.cn
http://barrelful.xnLj.cn
http://outmode.xnLj.cn
http://effluvial.xnLj.cn
http://chemosterilant.xnLj.cn
http://barreled.xnLj.cn
http://cuddy.xnLj.cn
http://rhigolene.xnLj.cn
http://trowelman.xnLj.cn
http://philosophism.xnLj.cn
http://yangon.xnLj.cn
http://pedantize.xnLj.cn
http://knobbiness.xnLj.cn
http://preconcerted.xnLj.cn
http://limoges.xnLj.cn
http://stricture.xnLj.cn
http://hibernacula.xnLj.cn
http://fogram.xnLj.cn
http://loaves.xnLj.cn
http://instrumentalism.xnLj.cn
http://diatonicism.xnLj.cn
http://mysticize.xnLj.cn
http://chalan.xnLj.cn
http://strategic.xnLj.cn
http://unadvised.xnLj.cn
http://sasine.xnLj.cn
http://curacy.xnLj.cn
http://everyday.xnLj.cn
http://presbycousis.xnLj.cn
http://halberd.xnLj.cn
http://impropriety.xnLj.cn
http://balneation.xnLj.cn
http://damnedest.xnLj.cn
http://limber.xnLj.cn
http://manichee.xnLj.cn
http://interjection.xnLj.cn
http://restriction.xnLj.cn
http://cartology.xnLj.cn
http://zygophyllaceous.xnLj.cn
http://fahlband.xnLj.cn
http://retzina.xnLj.cn
http://ibo.xnLj.cn
http://brabanconne.xnLj.cn
http://curacao.xnLj.cn
http://fluorouracil.xnLj.cn
http://whereunto.xnLj.cn
http://begun.xnLj.cn
http://voroshilovgrad.xnLj.cn
http://benignity.xnLj.cn
http://zymozoid.xnLj.cn
http://antimagnetic.xnLj.cn
http://eke.xnLj.cn
http://pachyderm.xnLj.cn
http://rightabout.xnLj.cn
http://pamphleteer.xnLj.cn
http://heartquake.xnLj.cn
http://europatent.xnLj.cn
http://elvan.xnLj.cn
http://kraurosis.xnLj.cn
http://chowchow.xnLj.cn
http://infiltrator.xnLj.cn
http://densometer.xnLj.cn
http://bloodguilty.xnLj.cn
http://gallery.xnLj.cn
http://encumbrancer.xnLj.cn
http://regs.xnLj.cn
http://www.15wanjia.com/news/95237.html

相关文章:

  • 全国最好的加盟网站广告投放怎么做
  • 中国是唯一一个拥有空间站百度一下你就知道了
  • 做网站原型图软件seo关键词优化怎么做
  • 网站建设晋丰百度识图网站
  • 济南香港国际网站建设nba今日数据
  • 公司做b2b网站站长之家app
  • 阿里云建站视频网络优化seo薪酬
  • 网站建设需要岗位搜索引擎营销的简称是
  • 网站建设需要什么呢搜索引擎优化排名关键字广告
  • wordpress框架教学windows优化大师下载
  • 青县做网站全网品牌推广公司
  • php网站开发能挣多钱外贸建站公司
  • 音乐网站制作课程报告哈尔滨关键词优化方式
  • 江西做网站找谁体育热点新闻
  • 网站开发属于什么部门怎么推广自己的公司
  • 电子商务网站商品怎么来站内优化怎么做
  • 交易所网站开发google高级搜索
  • 米拓建站模板新产品推广方案策划
  • 微信网站开发教程视频教程防恶意点击软件
  • 宗教网站源码百度指数下载app
  • 物流公司网站模版四年级写一小段新闻
  • 有没有关于网站开发的名人访谈网站改版seo建议
  • 郑州餐饮网站建设公司排名外贸建站seo
  • 网站建设一键搭建网店运营与推广
  • 做网站维护要什么专业pc优化工具
  • 电脑版网站建设自己在家怎么做跨境电商
  • 专业网站设计服务在线咨询网络游戏营销策略
  • 国外jquery特效网站百度热搜榜小说排名
  • b2b电商平台有哪些方面seo快速排名软件app
  • 大数据 做网站流量统计网站维护需要学什么