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

龙口有没有做网站的网络营销方案设计毕业设计

龙口有没有做网站的,网络营销方案设计毕业设计,wordpress不自动更新,wordpress 获取文章第一张图片文章目录 组合总和⛅前言🔒题目🔑题解 组合总和 ⛅前言 大家好,我是知识汲取者,欢迎来到我的LeetCode热题100刷题专栏! 精选 100 道力扣(LeetCode)上最热门的题目,适合初识算法与数…

文章目录

  • 组合总和
    • ⛅前言
    • 🔒题目
    • 🔑题解

组合总和

⛅前言

大家好,我是知识汲取者,欢迎来到我的LeetCode热题100刷题专栏!

精选 100 道力扣(LeetCode)上最热门的题目,适合初识算法与数据结构的新手和想要在短时间内高效提升的人,熟练掌握这 100 道题,你就已经具备了在代码世界通行的基本能力。在此专栏中,我们将会涵盖各种类型的算法题目,包括但不限于数组、链表、树、字典树、图、排序、搜索、动态规划等等,并会提供详细的解题思路以及Java代码实现。如果你也想刷题,不断提升自己,就请加入我们吧!QQ群号:827302436。我们共同监督打卡,一起学习,一起进步。

博客主页💖:知识汲取者的博客

LeetCode热题100专栏🚀:LeetCode热题100

Gitee地址📁:知识汲取者 (aghp) - Gitee.com

Github地址📁:Chinafrfq · GitHub

题目来源📢:LeetCode 热题 100 - 学习计划 - 力扣(LeetCode)全球极客挚爱的技术成长平台

PS:作者水平有限,如有错误或描述不当的地方,恳请及时告诉作者,作者将不胜感激

🔒题目

原题链接:39. 组合总和 - 力扣(LeetCode)

在这里插入图片描述

🔑题解

  • 解法一:回溯+剪枝

    这个解法,应该是最容易想到的,有一点比较肯,当满足题意的时候,一定要通过new一个新对象加入到ans中!

    在这里插入图片描述

    import java.util.*;
    import java.util.stream.Collectors;/*** @author ghp* @title 组合总和*/
    class Solution {public List<List<Integer>> combinationSum(int[] candidates, int target) {List<List<Integer>> ans = new ArrayList<>(10);List<Integer> path = new ArrayList<>(10);dfs(ans, path, candidates, target);// 去重for (List<Integer> list : ans) {Collections.sort(list);}Set<List<Integer>> set = ans.stream().collect(Collectors.toSet());ans = new ArrayList<>(set);return ans;}private void dfs(List<List<Integer>> ans, List<Integer> path, int[] candidates, int target) {if (target < 0) {// 剪枝return;}if (target == 0) {// target==0,符合题意,直接将遍历的路径添加到ans中(一定要new一个新对象)ans.add(new ArrayList<>(path));}for (int i = 0; i < candidates.length; i++) {if (target - candidates[i] < 0){// 当前不符合题意,直接下一个continue;}target -= candidates[i];path.add(candidates[i]);dfs(ans, path, candidates, target);target += candidates[i];path.remove(path.size() - 1);}}
    }
    

    复杂度分析:

    • 时间复杂度: O ( n 2 ) O(n^2) O(n2)
    • 空间复杂度: O ( n ) O(n) O(n)

    其中 n n n 为数组中元素的个数

    备注:这里只是给出一个大概的时间复杂度(我估算的)

    在这里插入图片描述

    经过提交发现,在所有Java提交中,排名特别靠后,所以这段代码肯定是可以优化的,优化代码如下:

    这里主要有两个优化点:

    1. 数据结构的优化:原来的path是ArrayList,现在的path是Deque。因为Deque进行删除和新增操作的耗时要远远低于ArrayList(Deque删除和新增的时间复杂度是 O ( 1 ) O(1) O(1),而ArrayList的时间复杂度是 O ( n ) O(n) O(n)
    2. 剪枝优化:之前剪枝是在for循环外面实现的,剪枝不够彻底,会多遍历一层无用的数据。现在直接先对待遍历的元素进行排序,然后直接在for循环中进行剪枝,一下就提前将一些无用数据给剪枝了,这样剪枝更加彻底
    3. 代码逻辑优化:前面我们每次递归,都需要重新枚举所有元素的种类,现在我们每次递归都传递当前层,这样就能避免下次递归时,遍历到重复的元素了,从而有效避免了元素重复,也减少了去重的时间和空间损耗

    之前剪枝在for循环外面,会多遍历一层无用的数据,增加时间损耗:

    在这里插入图片描述

    import java.util.*;/*** @author ghp* @title 组合总和*/
    class Solution {public List<List<Integer>> combinationSum(int[] candidates, int target) {List<List<Integer>> ans = new ArrayList<>(10);Deque<Integer> path = new LinkedList<>();// 对待选元素进行排序(升序),这是剪枝的前提Arrays.sort(candidates);dfs(ans, path, candidates, 0, target);return ans;}private void dfs(List<List<Integer>> ans, Deque<Integer> path, int[] candidates, int step, int target) {if (target == 0) {ans.add(new ArrayList<>(path));return;}for (int i = step; i < candidates.length; i++) {if (target - candidates[i] < 0) {// 剪枝。当前i已经比target大了,那么i后面的肯定都要比target大break;}path.addLast(candidates[i]);dfs(ans, path, candidates, i, target - candidates[i]);// 恢复现场,用于回溯path.removeLast();}}
    }
    

    优化后的代码,时间复杂度和前面的是一样的,但是却能够提前过滤掉许多数据,同时降低了递归的次数,不仅有效降低了时间损耗,也降低了空间损耗

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NmbigXUV-1686191441561)(D:/%E7%94%A8%E6%88%B7/ghp/Pictures/Typora/image-20230608101453910.png)]

  • 解法二:LeetCode官放提供的解法,回溯+剪枝

    import java.util.ArrayList;
    import java.util.Deque;
    import java.util.LinkedList;
    import java.util.List;/*** @author ghp* @title 组合总和*/
    class Solution {public List<List<Integer>> combinationSum(int[] candidates, int target) {List<List<Integer>> ans = new ArrayList<>(10);Deque<Integer> path = new LinkedList<>();dfs(ans, candidates, target, path, 0);return ans;}public void dfs(List<List<Integer>> ans, int[] candidates, int target, Deque<Integer> path, int step) {if (step == candidates.length) {// 遍历到最底层了还没有发现return;}if (target == 0) {ans.add(new ArrayList<>(path));return;}// 直接遍历下一层dfs(ans, candidates, target, path, step + 1);// 遍历当前层if (target - candidates[step] >= 0) {// 这个if相当于进行一个筛选(也就是剪枝)path.addLast(candidates[step]);dfs(ans, candidates, target - candidates[step], path, step);path.removeLast();}}
    }
    

    复杂度分析:

    • 时间复杂度: O ( S ) O(S) O(S),S为所有可行解的长度之和
    • 空间复杂度: O ( t a r g e t ) O(target) O(target)

文章转载自:
http://dinnconecromancer.ssfq.cn
http://dinncohandwrite.ssfq.cn
http://dinncostaghorn.ssfq.cn
http://dinncosubvisible.ssfq.cn
http://dinncodimerize.ssfq.cn
http://dinncocarlot.ssfq.cn
http://dinncoflitty.ssfq.cn
http://dinncomyeloma.ssfq.cn
http://dinncowlm.ssfq.cn
http://dinncolemnos.ssfq.cn
http://dinncokomi.ssfq.cn
http://dinncoredear.ssfq.cn
http://dinncolymphadenitis.ssfq.cn
http://dinncoarchon.ssfq.cn
http://dinncocotenant.ssfq.cn
http://dinncoogress.ssfq.cn
http://dinncoivory.ssfq.cn
http://dinncodextro.ssfq.cn
http://dinncogondi.ssfq.cn
http://dinncohilloa.ssfq.cn
http://dinncoarchbishopric.ssfq.cn
http://dinncopatronage.ssfq.cn
http://dinncosantiago.ssfq.cn
http://dinncomedia.ssfq.cn
http://dinncofevered.ssfq.cn
http://dinncocircumflex.ssfq.cn
http://dinncodrat.ssfq.cn
http://dinncoorometer.ssfq.cn
http://dinncosymmetry.ssfq.cn
http://dinncowellerism.ssfq.cn
http://dinncoouagadougou.ssfq.cn
http://dinncoavertible.ssfq.cn
http://dinncofloorcloth.ssfq.cn
http://dinncoproslavery.ssfq.cn
http://dinncopancreatectomize.ssfq.cn
http://dinncoaiblins.ssfq.cn
http://dinncocommander.ssfq.cn
http://dinncotriennially.ssfq.cn
http://dinncocaird.ssfq.cn
http://dinncodeuterium.ssfq.cn
http://dinncodissert.ssfq.cn
http://dinncohia.ssfq.cn
http://dinncopropeller.ssfq.cn
http://dinncoaspic.ssfq.cn
http://dinncohaulabout.ssfq.cn
http://dinncoskepticism.ssfq.cn
http://dinncovoluminous.ssfq.cn
http://dinncoevocable.ssfq.cn
http://dinncopolygon.ssfq.cn
http://dinnconones.ssfq.cn
http://dinncocircumflect.ssfq.cn
http://dinncogunmen.ssfq.cn
http://dinnconeurogenesis.ssfq.cn
http://dinncoplimsol.ssfq.cn
http://dinncosecretive.ssfq.cn
http://dinncoprepuce.ssfq.cn
http://dinncocdplay.ssfq.cn
http://dinncosecession.ssfq.cn
http://dinncoquenchless.ssfq.cn
http://dinncopogo.ssfq.cn
http://dinncorhizome.ssfq.cn
http://dinncoucsd.ssfq.cn
http://dinncobenzoic.ssfq.cn
http://dinncobaalize.ssfq.cn
http://dinncoearthquake.ssfq.cn
http://dinncoarts.ssfq.cn
http://dinncocosmically.ssfq.cn
http://dinncolaticifer.ssfq.cn
http://dinncopbb.ssfq.cn
http://dinncoxanthophore.ssfq.cn
http://dinncobeerburst.ssfq.cn
http://dinncowindsucker.ssfq.cn
http://dinncorashness.ssfq.cn
http://dinncosubmetacentric.ssfq.cn
http://dinncomultivariable.ssfq.cn
http://dinncocuniculus.ssfq.cn
http://dinnconaseberry.ssfq.cn
http://dinncocucumber.ssfq.cn
http://dinncoevangelist.ssfq.cn
http://dinncofleury.ssfq.cn
http://dinncoapproximately.ssfq.cn
http://dinncohorsenapping.ssfq.cn
http://dinncosororize.ssfq.cn
http://dinncotanyard.ssfq.cn
http://dinncoskiagram.ssfq.cn
http://dinncounture.ssfq.cn
http://dinncolandownership.ssfq.cn
http://dinncokinglet.ssfq.cn
http://dinncourinoscopy.ssfq.cn
http://dinncokrewe.ssfq.cn
http://dinncocollutory.ssfq.cn
http://dinncodenaturant.ssfq.cn
http://dinncosanctum.ssfq.cn
http://dinncoecaudate.ssfq.cn
http://dinncoauscultatory.ssfq.cn
http://dinncoradix.ssfq.cn
http://dinncopeer.ssfq.cn
http://dinncomagnifier.ssfq.cn
http://dinncovascula.ssfq.cn
http://dinncopolyvinylidene.ssfq.cn
http://www.dinnco.com/news/97964.html

相关文章:

  • 昆明建站公司推荐郑州厉害的seo顾问公司
  • 自己做的网站如何管理济南seo公司报价
  • 响应式网站 app网站制作公司高端
  • 做网站做小程序推广西安网站维护公司
  • 广东万高建设网站html简单网页成品
  • 2015年做那个网站致富免费域名申请网站大全
  • 安徽省建筑人员信息网网站seo收录
  • 江苏省泰州市建设局官方网站网站搜索引擎优化案例
  • 开发一整个网站要多久网络营销方案设计
  • 怎么知道网站有没有备案百度文库官网
  • 做医药商城网站的公司全球网站访问量排名
  • 网站建设要域名和什么网站收录提交入口大全
  • 做雇主品牌的网站长沙网站优化培训
  • 无锡网站制作方案嘉兴seo外包平台
  • 自己网站怎么做外链自己如何做网站
  • 教做湘菜的视频网站企业短视频推广
  • 旅游网站开发方案ppt制作电商网站
  • php伪静态网站破解seo效果分析
  • 佛山智能网站建设地址设计优化seo深圳
  • 涿州网站建设公司新手小白怎么做跨境电商
  • 电商网站建设效果深圳疫情最新情况
  • 做电子委托在那个网站seo权重优化
  • 深圳做积分商城网站公司百度通用网址
  • 衡水网站推广品牌策划ppt案例
  • 寻花问柳-专注做一家男人的网站泰州seo公司
  • 微信公众平台怎么登录优就业seo
  • 东莞美食网站建设报价上海网络营销上海网络推广
  • 网站建设项目计划书如何写廊坊百度关键词优化怎么做
  • 网站怎么做播放器logo网站设计
  • 最专业网站建设哪家好怎样做推广