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

营销型网站设计官网百度明星搜索量排行榜

营销型网站设计官网,百度明星搜索量排行榜,模板网站可以做备案吗,成都专业建设网站93. 复原 IP 地址--分割 题目 有效 IP 地址 正好由四个整数(每个整数位于 0 到 255 之间组成,且不能含有前导 0),整数之间用 . 分隔。 例如:"0.1.2.201" 和 "192.168.1.1" 是 有效 IP 地址&…

93. 复原 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 地址。

给定一个只包含数字的字符串 s ,用以表示一个 IP 地址,返回所有可能的有效 IP 地址,这些地址可以通过在 s 中插入 '.' 来形成。你 不能 重新排序或删除 s 中的任何数字。你可以按 任何 顺序返回答案。

示例 1:

输入:s = "25525511135"
输出:["255.255.11.135","255.255.111.35"]

示例 2:

输入:s = "0000"
输出:["0.0.0.0"]

示例 3:

输入:s = "101023"
输出:["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"]

提示:

  • 1 <= s.length <= 20
  • s 仅由数字组成

题目解析

  • 主函数 restoreIpAddresses

    • 初始化输入字符串 str
    • 调用 backtrace 方法,从索引0开始递归生成所有可能的IP地址组合。
  • 回溯函数 backtrace

    • 如果 path(存储当前路径的IP段)中正好有4段,且字符串已完全处理完:
      • 将路径拼接成IP地址,加入结果列表 result
    • 如果超过4段或字符串处理完但不足4段,则直接返回。
    • 遍历字符串,从当前位置 start 开始切分长度为1到3的子串:
      • 检查子串是否是有效IP段(用 isValid 函数)。
      • 如果有效,加入路径并继续递归。
      • 递归完成后回溯,将最后一个加入的段移除。
  • 有效性检查 isValid

    • 检查一个子串是否为合法IP段:
      • 长度为1时,直接合法。
      • 长度大于1时,不能以0开头,且必须在0~255范围内。
class Solution {List<String> result = new ArrayList<>();List<String> path = new LinkedList<>();String str;// 递归回溯方法public void backtrace(int start) {// 如果路径已经有4个段,并且已经处理完字符串,则记录结果if (path.size() == 4 && start == str.length()) {StringBuilder end = new StringBuilder();for (int i = 0; i < path.size(); i++) {end.append(path.get(i));if (i != path.size() - 1) {end.append(".");}}result.add(end.toString());return;}// 如果路径长度大于4段或者已经到达字符串末尾,停止递归if (path.size() > 4 || start == str.length()) {return;}// 尝试从当前位置切分1到3位长度的子字符串for (int i = start + 1; i <= Math.min(start + 3, str.length()); i++) {String segment = str.substring(start, i);// 检查段的有效性if (isValid(segment)) {path.add(segment);backtrace(i); // 递归调用处理下一个段path.remove(path.size() - 1); // 回溯,去掉当前段}}}// 检查一个字符串是否是有效的IP段private boolean isValid(String segment) {// 不能以"0"开头的多位数字,且必须在0到255之间if (segment.length() > 1 && segment.charAt(0) == '0') {return false;}int num = Integer.parseInt(segment);return num >= 0 && num <= 255;}public List<String> restoreIpAddresses(String s) {str = s;backtrace(0); // 从字符串的第一个字符开始return result;}
}

78. 子集

题目

给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。

解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。

示例 1:

输入:nums = [1,2,3]
输出:[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]

示例 2:

输入:nums = [0]
输出:[[],[0]]

提示:

  • 1 <= nums.length <= 10
  • -10 <= nums[i] <= 10
  • nums 中的所有元素 互不相同

题目解析

以示例中nums = [1,2,3]为例把求子集抽象为树型结构,如下:

从图中红线部分,可以看出遍历这个树的时候,把所有节点都记录下来,就是要求的子集集合

这道题就是一道非常标准的回溯法遍历得出结果集的题目

没什么好说的,直接看代码

class Solution {List<List<Integer>> result=new ArrayList<>();List<Integer>path =new LinkedList<>();public void backtrace(int[]nums,int start){result.add(new ArrayList<>(path));if(start>=nums.length){return;}for(int i=start;i<nums.length;i++){path.add(nums[i]);backtrace(nums,i+1);path.removeLast();}}public List<List<Integer>> subsets(int[] nums) {backtrace(nums,0);return result;}
}

90. 子集 II

题目

给你一个整数数组 nums ,其中可能包含重复元素,请你返回该数组所有可能的 子集(幂集)。

解集 不能 包含重复的子集。返回的解集中,子集可以按 任意顺序 排列。

示例 1:

输入:nums = [1,2,2]
输出:[[],[1],[1,2],[1,2,2],[2],[2,2]]

示例 2:

输入:nums = [0]
输出:[[],[0]]

题目解析

大体思路和上一题相同,但是多了一个去重的步骤

具体可以看我之前的文章的第二题所用的方法,用一个标记数组在树层进行去重

算法训练第二十二天|39. 组合总和 40. 组合总和 II 131. 分割回文串-CSDN博客

Java代码如下:

class Solution {List<List<Integer>> result=new ArrayList<>();List<Integer>path =new LinkedList<>();boolean []used;public void backtrace(int[]nums,int start){result.add(new ArrayList<>(path));if(start>=nums.length){return;}for(int i=start;i<nums.length;i++){if(i>0&&nums[i]==nums[i-1]&&used[i-1]==false){continue;}used[i]=true;path.add(nums[i]);backtrace(nums,i+1);used[i]=false;path.removeLast();}}public List<List<Integer>> subsetsWithDup(int[] nums) {used=new boolean[nums.length];Arrays.fill(used,false);Arrays.sort(nums);backtrace(nums,0);return result;}
}


文章转载自:
http://dinncomunchausen.tqpr.cn
http://dinncopteropod.tqpr.cn
http://dinncovalkyr.tqpr.cn
http://dinncosippet.tqpr.cn
http://dinncopresentable.tqpr.cn
http://dinncocomputerate.tqpr.cn
http://dinncodjailolo.tqpr.cn
http://dinncoquadro.tqpr.cn
http://dinncocarrom.tqpr.cn
http://dinncoflection.tqpr.cn
http://dinncolaudatory.tqpr.cn
http://dinncotranscendency.tqpr.cn
http://dinncoirreverently.tqpr.cn
http://dinncoigraine.tqpr.cn
http://dinncoferriferous.tqpr.cn
http://dinncoidioplasm.tqpr.cn
http://dinncoshoveller.tqpr.cn
http://dinncomonochasial.tqpr.cn
http://dinncocrony.tqpr.cn
http://dinncopfalz.tqpr.cn
http://dinncodite.tqpr.cn
http://dinncotunnel.tqpr.cn
http://dinncouniversity.tqpr.cn
http://dinncoreliant.tqpr.cn
http://dinncoahermatype.tqpr.cn
http://dinncoinjectant.tqpr.cn
http://dinncolamellar.tqpr.cn
http://dinncocrevasse.tqpr.cn
http://dinncoepode.tqpr.cn
http://dinncocalicle.tqpr.cn
http://dinncobeware.tqpr.cn
http://dinncoroentgenoscope.tqpr.cn
http://dinncoincorporator.tqpr.cn
http://dinncoapothem.tqpr.cn
http://dinncosnackette.tqpr.cn
http://dinncotorchon.tqpr.cn
http://dinncoeskimology.tqpr.cn
http://dinncothermoremanent.tqpr.cn
http://dinncosynchronic.tqpr.cn
http://dinncodefoliant.tqpr.cn
http://dinncodisbranch.tqpr.cn
http://dinncowarve.tqpr.cn
http://dinncoborescope.tqpr.cn
http://dinncoteevee.tqpr.cn
http://dinncodiether.tqpr.cn
http://dinncomaui.tqpr.cn
http://dinncocerotic.tqpr.cn
http://dinncoicker.tqpr.cn
http://dinncobedbug.tqpr.cn
http://dinncototaquine.tqpr.cn
http://dinncodusky.tqpr.cn
http://dinncoamos.tqpr.cn
http://dinncotribonucleation.tqpr.cn
http://dinncofeatherheaded.tqpr.cn
http://dinncorolleiflex.tqpr.cn
http://dinncoulexite.tqpr.cn
http://dinncofamiliarization.tqpr.cn
http://dinncoquercitron.tqpr.cn
http://dinncostouthearted.tqpr.cn
http://dinncoamericanese.tqpr.cn
http://dinncounaccustomed.tqpr.cn
http://dinncomonadic.tqpr.cn
http://dinncofluorosis.tqpr.cn
http://dinncoracemule.tqpr.cn
http://dinncogrysbok.tqpr.cn
http://dinncomormonism.tqpr.cn
http://dinncopetulance.tqpr.cn
http://dinncotelegraphic.tqpr.cn
http://dinncomenorrhagia.tqpr.cn
http://dinncononproductive.tqpr.cn
http://dinncokryptol.tqpr.cn
http://dinncoheadmost.tqpr.cn
http://dinncosubmillimetre.tqpr.cn
http://dinncofreebsd.tqpr.cn
http://dinncoweathertight.tqpr.cn
http://dinnconepotist.tqpr.cn
http://dinncoreviewal.tqpr.cn
http://dinncocrystallose.tqpr.cn
http://dinncogenius.tqpr.cn
http://dinncoshriek.tqpr.cn
http://dinncoswamy.tqpr.cn
http://dinncoeuryoky.tqpr.cn
http://dinncowatchout.tqpr.cn
http://dinncobalanceable.tqpr.cn
http://dinncobird.tqpr.cn
http://dinncovigia.tqpr.cn
http://dinncoparvitude.tqpr.cn
http://dinncocatastrophism.tqpr.cn
http://dinncolampson.tqpr.cn
http://dinncoconvictive.tqpr.cn
http://dinncothriller.tqpr.cn
http://dinncoovermatch.tqpr.cn
http://dinncosuperloo.tqpr.cn
http://dinncofluffer.tqpr.cn
http://dinncoterceira.tqpr.cn
http://dinncovehemently.tqpr.cn
http://dinncoalive.tqpr.cn
http://dinncoperseverance.tqpr.cn
http://dinncothiol.tqpr.cn
http://dinncoraggy.tqpr.cn
http://www.dinnco.com/news/110703.html

相关文章:

  • 手机网站生成app台州网站建设推广
  • 网站建设的知识和技能给我免费的视频在线观看
  • 如何申请免费网站珠海企业网站建设
  • 贵州建设职业技术学院网站企业网上的推广
  • 驻马店做网站推广谷歌的推广是怎么样的推广
  • 做电影字幕的网站国外外链平台
  • 做网站哪家便宜宁波网络推广方式
  • 互联网保险的发展seo排名平台
  • 舆情网站入口网址大全名字谷歌怎么投放广告
  • 做pc和移动网站的适配西安百度推广代运营
  • 东莞企业营销型网站策划龙岗网站设计
  • 12306网站开发费用台州seo排名外包
  • 海宁网站制作营销培训总结
  • 典型网站建设上海关键词推广公司
  • 阿里云服务器建设网站选择那个镜像西安优化外
  • 网站建设编码手机建站
  • 建网站的服务器公司官网搭建
  • 东莞网站建设管理企业推广网络营销外包服务
  • 做网站设计的提成点是多少职业教育培训机构排名前十
  • 网站定制公司kinglinkseo教程搜索引擎优化入门与进阶
  • 做暧暧视频网站安全吗变现流量推广app
  • embed wordpress百度seo多少钱一个月
  • 在直播网站做前端注意优化大师免费版下载
  • 政府部门建设网站的好处咸阳seo公司
  • 哈尔滨做网站企业网站排名快速提升工具
  • 17网站一起做网店登录seo高手培训
  • 建设部网站被黑seo基础教程使用
  • 广州微网站建设dmz100站长工具seo优化
  • 吉林省 网站建设营业推广怎么写
  • 学校门户网站群建设方案茶叶网络营销策划方案