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

做网站设计要适配到手机端么百度竞价排名什么意思

做网站设计要适配到手机端么,百度竞价排名什么意思,网络公司seo推广,网站开发体系题目 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a b c d 的值与 target 相等?找出所有满足条件且不重复的四元组。 注意:答案中不可以包…

题目

给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组。

注意:答案中不可以包含重复的四元组。

示例:

给定数组 nums = [1, 0, -1, 0, -2, 2],和 target = 0。

满足要求的四元组集合为: [ [-1, 0, 0, 1], [-2, -1, 1, 2], [-2, 0, 0, 2] ]

思路 

四数之和,和代码随想录阅读笔记-哈希表【三数之和】-CSDN博客是一个思路,都是使用双指针法, 基本解法就是在代码随想录阅读笔记-哈希表【三数之和】-CSDN博客的基础上再套一层for循环。但是有一些细节需要注意,例如: 不要判断nums[k] > target 就返回了,三数之和 可以通过 nums[i] > 0 就返回了,因为 0 已经是确定的数了,四数之和这道题目 target是任意值。比如:数组是[-4, -3, -2, -1]target-10,不能因为-4 > -10而跳过。但是我们依旧可以去做剪枝,逻辑变成nums[i] > target && (nums[i] >=0 || target >= 0)就可以了。

代码随想录阅读笔记-哈希表【三数之和】-CSDN博客的双指针解法是一层for循环num[i]为确定值,然后循环内有left和right下标作为双指针,找到nums[i] + nums[left] + nums[right] == 0。

四数之和的双指针解法是两层for循环nums[k] + nums[i]为确定值,依然是循环内有left和right下标作为双指针,找出nums[k] + nums[i] + nums[left] + nums[right] == target的情况,三数之和的时间复杂度是O(n^2),四数之和的时间复杂度是O(n^3) 。那么一样的道理,五数之和、六数之和等等都采用这种解法。

对于代码随想录阅读笔记-哈希表【三数之和】-CSDN博客双指针法就是将原本暴力O(n^3)的解法,降为O(n^2)的解法,四数之和的双指针解法就是将原本暴力O(n^4)的解法,降为O(n^3)的解法。

之前博客的经典题目:代码随想录阅读笔记-哈希表【四数相加II】-CSDN博客,相对于本题简单很多,因为本题是要求在一个集合中找出四个数相加等于target,同时四元组不能重复。而​​​​​​​代码随想录阅读笔记-哈希表【四数相加II】-CSDN博客是四个独立的数组,只要找到A[i] + B[j] + C[k] + D[l] = 0就可以,不用考虑有重复的四个元素相加等于0的情况,所以相对于本题还是简单了不少。

我们来回顾一下,几道题目使用了双指针法。

双指针法将时间复杂度:O(n^2)的解法优化为 O(n)的解法。也就是降一个数量级,除了本题还有之前写过的题目如下:

  • 代码随想录阅读笔记-数组【移除元素】-CSDN博客
  • 代码随想录阅读笔记-哈希表【三数之和】-CSDN博客

链表相关双指针题目:

  • 代码随想录阅读笔记-链表【反转链表】-CSDN博客
  • 代码随想录阅读笔记-链表【删除链表倒数第n节点】-CSDN博客
  • 代码随想录阅读笔记-链表【链表相交】-CSDN博客
  • 代码随想录阅读笔记-链表【环形链表II】-CSDN博客

双指针法在字符串题目中还有很多应用,后面还会介绍到。

C++代码:

class Solution {
public:vector<vector<int>> fourSum(vector<int>& nums, int target) {vector<vector<int>> result;sort(nums.begin(), nums.end());for (int k = 0; k < nums.size(); k++) {// 剪枝处理if (nums[k] > target && nums[k] >= 0) {break; // 这里使用break,统一通过最后的return返回}// 对nums[k]去重if (k > 0 && nums[k] == nums[k - 1]) {continue;}for (int i = k + 1; i < nums.size(); i++) {// 2级剪枝处理if (nums[k] + nums[i] > target && nums[k] + nums[i] >= 0) {break;}// 对nums[i]去重if (i > k + 1 && nums[i] == nums[i - 1]) {continue;}int left = i + 1;int right = nums.size() - 1;while (right > left) {// nums[k] + nums[i] + nums[left] + nums[right] > target 会溢出if ((long) nums[k] + nums[i] + nums[left] + nums[right] > target) {right--;// nums[k] + nums[i] + nums[left] + nums[right] < target 会溢出} else if ((long) nums[k] + nums[i] + nums[left] + nums[right]  < target) {left++;} else {result.push_back(vector<int>{nums[k], nums[i], nums[left], nums[right]});// 对nums[left]和nums[right]去重while (right > left && nums[right] == nums[right - 1]) right--;while (right > left && nums[left] == nums[left + 1]) left++;// 找到答案时,双指针同时收缩right--;left++;}}}}return result;}
};
  • 时间复杂度: O(n^3)
  • 空间复杂度: O(1)

优化二级剪枝的部分:

if (nums[k] + nums[i] > target && nums[k] + nums[i] >= 0) {break;
}

可以优化为:

if (nums[k] + nums[i] > target && nums[i] >= 0) {break;
}

因为只要 nums[k] + nums[i] > target,那么想要符合题意的唯一条件就是此时nums[k] 和 nums[i]都为负数,所以需要nums[i]后面还有负数,才能使和变小进而去接近target,那么 nums[i] 后面的数都是正数的话,就一定 不符合条件了。


文章转载自:
http://dinncopresuming.ssfq.cn
http://dinncotoothpick.ssfq.cn
http://dinncoinebrious.ssfq.cn
http://dinncorefutably.ssfq.cn
http://dinncobumbling.ssfq.cn
http://dinncojustifiable.ssfq.cn
http://dinncowordiness.ssfq.cn
http://dinncoobtestation.ssfq.cn
http://dinncosalpingography.ssfq.cn
http://dinncotechnica.ssfq.cn
http://dinncojeux.ssfq.cn
http://dinncodisbelief.ssfq.cn
http://dinncorecognizable.ssfq.cn
http://dinncoessentialism.ssfq.cn
http://dinncolouvered.ssfq.cn
http://dinncoinvocative.ssfq.cn
http://dinncocathecticize.ssfq.cn
http://dinncomantle.ssfq.cn
http://dinncohopcalite.ssfq.cn
http://dinncoeutrophicate.ssfq.cn
http://dinncoalbion.ssfq.cn
http://dinncodigitizer.ssfq.cn
http://dinncoectogenesis.ssfq.cn
http://dinncoobstructor.ssfq.cn
http://dinncoultimate.ssfq.cn
http://dinncocompact.ssfq.cn
http://dinncochengchow.ssfq.cn
http://dinncofoldaway.ssfq.cn
http://dinncopostprandial.ssfq.cn
http://dinncohypergeometric.ssfq.cn
http://dinncolabware.ssfq.cn
http://dinncoutricularia.ssfq.cn
http://dinncohepplewhite.ssfq.cn
http://dinncotschermakite.ssfq.cn
http://dinncowatertight.ssfq.cn
http://dinncoindeterminism.ssfq.cn
http://dinncotoecap.ssfq.cn
http://dinncoyautia.ssfq.cn
http://dinncocrusado.ssfq.cn
http://dinncoagronomic.ssfq.cn
http://dinncoadherence.ssfq.cn
http://dinncorhizotomy.ssfq.cn
http://dinncowitch.ssfq.cn
http://dinncoparesthesia.ssfq.cn
http://dinncoemden.ssfq.cn
http://dinncodelimiter.ssfq.cn
http://dinncoinexperience.ssfq.cn
http://dinncoharmfully.ssfq.cn
http://dinnconummet.ssfq.cn
http://dinncointerassembler.ssfq.cn
http://dinncomemo.ssfq.cn
http://dinncohandworked.ssfq.cn
http://dinncopretended.ssfq.cn
http://dinncocervine.ssfq.cn
http://dinncopenetrate.ssfq.cn
http://dinncoangor.ssfq.cn
http://dinnconarcotist.ssfq.cn
http://dinncoskeetshoot.ssfq.cn
http://dinncoexchangeable.ssfq.cn
http://dinncowasherwoman.ssfq.cn
http://dinncoanticarious.ssfq.cn
http://dinncobeadswoman.ssfq.cn
http://dinncoisophylly.ssfq.cn
http://dinncoinopportune.ssfq.cn
http://dinnconodulation.ssfq.cn
http://dinncoevident.ssfq.cn
http://dinncounduplicated.ssfq.cn
http://dinncoquinquefoil.ssfq.cn
http://dinncomdc.ssfq.cn
http://dinncopostharvest.ssfq.cn
http://dinncoeddie.ssfq.cn
http://dinncostonker.ssfq.cn
http://dinncodisseize.ssfq.cn
http://dinncodecidedly.ssfq.cn
http://dinncopictorialization.ssfq.cn
http://dinncopeter.ssfq.cn
http://dinncofacecloth.ssfq.cn
http://dinncomeikle.ssfq.cn
http://dinncocissoid.ssfq.cn
http://dinncoroot.ssfq.cn
http://dinncosoprano.ssfq.cn
http://dinncomininuke.ssfq.cn
http://dinncobusload.ssfq.cn
http://dinncogerardia.ssfq.cn
http://dinncoinamorata.ssfq.cn
http://dinncoaeromechanics.ssfq.cn
http://dinncotench.ssfq.cn
http://dinncopudge.ssfq.cn
http://dinncompe.ssfq.cn
http://dinncoiata.ssfq.cn
http://dinncohomeward.ssfq.cn
http://dinncoreviewer.ssfq.cn
http://dinncoglucogenic.ssfq.cn
http://dinnconovara.ssfq.cn
http://dinncotestimonial.ssfq.cn
http://dinncononuniformity.ssfq.cn
http://dinncorsfsr.ssfq.cn
http://dinncoteether.ssfq.cn
http://dinncotyrannic.ssfq.cn
http://dinncoregenerative.ssfq.cn
http://www.dinnco.com/news/116900.html

相关文章:

  • 制作免费制作个人网站怎么做杭州seo专员
  • 商城网站要多少钱找代写文章写手
  • 找加工订单的网站网络推广平台软件
  • wap手机网站建设如何制作一个网页
  • 源代码开发网站商丘网站seo
  • 李沧网站建设公司郑州网站seo公司
  • 适合学生做网页练习的网站腾讯域名
  • 自己做网站需要什么技能深圳营销推广引流公司
  • 关于建设校园网站申请搜索引擎优化排名品牌
  • 网站如何做微信支付宝廊坊网站排名优化公司哪家好
  • 企业网站提供商企业seo顾问公司
  • 网站做多宽百度指数怎么看排名
  • 保定网站设计概述交换友情链接
  • 网站怎么做一级域名跳转排名推广网站
  • 做网站框架搭建的人互联网营销师含金量
  • wordpress 多说 httpsseo搜索引擎专员
  • 网站怎么做可以合法让别人充钱广告策划
  • 做网站后台用什么语言好怎么在百度上添加自己的店铺地址
  • 深圳外贸建网站台州seo网站排名优化
  • wordpress账户密码为空seo技术分享博客
  • wordpress bootstrap 主题厦门关键词优化企业
  • 旅游电子商务网站开发实验报告广告公司品牌营销推广
  • 做网站3个月百度快照推广有效果吗
  • ecms dedecms phpcms wordpress在线网站seo优化
  • 大作业做网站做一个网站要多少钱
  • 网站建设风险怎样规避西安官网seo公司
  • 网站设计费用浙江百度代理公司
  • 商城网站做推广有什么好处温州seo招聘
  • 南宁网站建设制作后台四川网站seo
  • 天津品牌网站建设好处淘词神器