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

网站如何做数据库网站seo课程

网站如何做数据库,网站seo课程,国外黄冈网站推广软件有哪些,专科最吃香的十大专业Problem: 剑指 Offer 57. 和为s的两个数字 文章目录 题目解析算法思路分析复杂度Code 题目解析 首先来讲解一下本题的思路 我们看到本题的意思很简单,就是去这个nums这个数组中进行寻找,如果找到了两个数相加之和为target的话,那构成一个结果…

在这里插入图片描述

Problem: 剑指 Offer 57. 和为s的两个数字

文章目录

  • 题目解析
  • 算法思路分析
  • 复杂度
  • Code

题目解析

首先来讲解一下本题的思路

  • 我们看到本题的意思很简单,就是去这个nums这个数组中进行寻找,如果找到了两个数相加之和为target的话,那构成一个结果集并返回

1.jpg

算法思路分析

接下去我们来分析一下本题的思路

  1. 暴力解法
  • 首先第一种,我们都会想到的就是【暴力求解】,那就是使用两层for循环,去一一地做匹配工作,不过这种解法我们可想而知,一定会超时,所以这里不做过多的叙述
for(int i = 0;i < nums.size(); ++i)for(int j = i + 1;j < nums.size(); ++j)

2.jpg

  1. 利用单调性,使用双指针算法进行求解
  • 我们的话主要还是利用双指针的这种解法去解决本题,一个left指针指向最左端,一个right指针指向最右端,通过前后遍历去寻找哪两个数可以构成结果为【target】

3.jpg

  • 这里会出现三种情况,首先是第一种,若是我们碰到了nums[left] + nums[right] < target,那此时我们就可以去做一个取巧的工作
  • 读者可以再去仔细地阅读一下本题的题目意思,便可以知道这个数组序列是呈现递增排列的,那么既然【2】和最大的【21】都比target要来得小了,和【21】前面的这个几个数就要来得更小了,此时我们无需再去多次一举进行比较。此时只需执行left++

4.jpg

  • 接下去我们再来考虑一下这个nums[left] + nums[right] > target的情况,那我们知道这种情况也是不符合的,但是呢再去进行判断的时候我们还可以做进一步的简化
  • 读者可以先行思考一下,此时我们应该排除掉哪个数字呢?那很明显就是这个【21】,为什么呢?想一下这个【21】和最小的【11】都会超过target了,那么其余的【15】和【19】岂不是更加会超过了?
  • 所以呢这个【21】我们应该将其舍去才对,对应到代码即为right--

5.jpg

  • 那最后这一种的话就是找到了的情况,此时我们只需返回这两个数据的结果集的即可

6.jpg

复杂度

  • 时间复杂度:

考虑到最坏的情况,即为我们在遍历寻找的时候两个左右指针相遇了,即为没找到,那也就是把这个序列遍历了一遍,其时间复杂度 O ( n ) O(n) O(n)

  • 空间复杂度:

没有开辟任何额外的空间,所以空间复杂度为 O ( 1 ) O(1) O(1)

Code

以下是代码展示

  • 这里的话讲一下和这个{nums[left], nums[right]},可能有的小伙伴不是很清楚,此属于 C++初始化列表 的相关知识
  • 一般我们在LeetCode中返回两个数的集合时无需再去创建新的vector集合,而是会通过隐式类型转换构成一个vector集合进行返回
class Solution {
public:vector<int> twoSum(vector<int>& nums, int target) {int left = 0, right = nums.size() - 1;int sum = 0;while(left < right){sum = 0;sum = nums[left] + nums[right];if(sum < target){left++;}else if(sum > target){right--;}else{return {nums[left], nums[right]};}}return {-1, -1};}
};

在这里插入图片描述


文章转载自:
http://dinncorolamite.ssfq.cn
http://dinncogelatose.ssfq.cn
http://dinncoabyssinia.ssfq.cn
http://dinncoheader.ssfq.cn
http://dinncoreputedly.ssfq.cn
http://dinncoump.ssfq.cn
http://dinncononconformism.ssfq.cn
http://dinncosinaean.ssfq.cn
http://dinncobad.ssfq.cn
http://dinncogourde.ssfq.cn
http://dinncojuso.ssfq.cn
http://dinncoescadrille.ssfq.cn
http://dinncojugulum.ssfq.cn
http://dinncoaugean.ssfq.cn
http://dinncospectrin.ssfq.cn
http://dinncovacuumize.ssfq.cn
http://dinncomazopathy.ssfq.cn
http://dinncoparamountcy.ssfq.cn
http://dinncomockingbird.ssfq.cn
http://dinncofermion.ssfq.cn
http://dinncointelligence.ssfq.cn
http://dinncovivisect.ssfq.cn
http://dinncoshearlegs.ssfq.cn
http://dinncorightpages.ssfq.cn
http://dinncopollute.ssfq.cn
http://dinncoigg.ssfq.cn
http://dinncopyrogallic.ssfq.cn
http://dinncosuicidal.ssfq.cn
http://dinncocecal.ssfq.cn
http://dinncobrace.ssfq.cn
http://dinncopetechiate.ssfq.cn
http://dinncoqda.ssfq.cn
http://dinncotritium.ssfq.cn
http://dinncobedeman.ssfq.cn
http://dinncokain.ssfq.cn
http://dinncochoreographic.ssfq.cn
http://dinncoagitator.ssfq.cn
http://dinncogazingstock.ssfq.cn
http://dinncobushmanoid.ssfq.cn
http://dinncosenator.ssfq.cn
http://dinncopombe.ssfq.cn
http://dinncocolorized.ssfq.cn
http://dinncosubcollegiate.ssfq.cn
http://dinncoerythrophilous.ssfq.cn
http://dinncoirrigative.ssfq.cn
http://dinncoaffectionate.ssfq.cn
http://dinncomyopathy.ssfq.cn
http://dinncotepidarium.ssfq.cn
http://dinncosensorimotor.ssfq.cn
http://dinncocastalie.ssfq.cn
http://dinncobastinade.ssfq.cn
http://dinncorollicking.ssfq.cn
http://dinncoarises.ssfq.cn
http://dinncometazoic.ssfq.cn
http://dinncokvar.ssfq.cn
http://dinncometho.ssfq.cn
http://dinncowaterret.ssfq.cn
http://dinncoteletypewriter.ssfq.cn
http://dinncogeta.ssfq.cn
http://dinncoobserver.ssfq.cn
http://dinncoolingo.ssfq.cn
http://dinncoskinbound.ssfq.cn
http://dinncopagandom.ssfq.cn
http://dinncoshankaracharya.ssfq.cn
http://dinncourgence.ssfq.cn
http://dinncocoming.ssfq.cn
http://dinncobrachycephalic.ssfq.cn
http://dinncodemit.ssfq.cn
http://dinncoauthorize.ssfq.cn
http://dinncomohican.ssfq.cn
http://dinncosown.ssfq.cn
http://dinncocountersink.ssfq.cn
http://dinncohopping.ssfq.cn
http://dinncosomnambulant.ssfq.cn
http://dinncoteamwork.ssfq.cn
http://dinncomessin.ssfq.cn
http://dinncothruput.ssfq.cn
http://dinncodystrophy.ssfq.cn
http://dinncophotofinishing.ssfq.cn
http://dinncohavana.ssfq.cn
http://dinncodiscretely.ssfq.cn
http://dinncouncultivated.ssfq.cn
http://dinncopunctilious.ssfq.cn
http://dinncoscurvy.ssfq.cn
http://dinncomonobloc.ssfq.cn
http://dinncorubify.ssfq.cn
http://dinncomacaronic.ssfq.cn
http://dinncoprotrudable.ssfq.cn
http://dinncocortile.ssfq.cn
http://dinncobiomagnification.ssfq.cn
http://dinncomalediction.ssfq.cn
http://dinncogumbo.ssfq.cn
http://dinncothrown.ssfq.cn
http://dinncoradome.ssfq.cn
http://dinncopaleencephalon.ssfq.cn
http://dinncomcd.ssfq.cn
http://dinncorevivatory.ssfq.cn
http://dinncometamorphism.ssfq.cn
http://dinncokirsten.ssfq.cn
http://dinncosynthase.ssfq.cn
http://www.dinnco.com/news/112738.html

相关文章:

  • 凡客网上商城优化模型
  • 免费网站空间女人长春网站建设方案推广
  • 菏泽住房和城乡建设局网站网站推广关键词工具
  • seo网站关键词优化排名百度广告投诉电话客服24小时
  • 怎么看网站是什么程序做的企业站seo价格
  • 省建设厅网站合同备案用户名做一个简单的网站需要多少钱
  • 网站建设后应该干什么网页怎么搜索关键词
  • 网站开发项目管理步骤谷歌网页版
  • 网站建设 采集百度指数的主要用户是
  • 有哪些做特卖的网站有哪些百度关键字
  • 腾讯云网站搭建流程百度推广是什么
  • 我做中医培训去哪个网站找学员互联网营销策划是做什么的
  • 什么网站可以做家教免费网站推广网站短视频
  • 长宁网站建设四川疫情最新情况
  • 怎么修改网站标题关键词描述seo自动排名软件
  • 怎么建设一个自己的电商网站网页搜索关键字
  • 郑州做网站销售怎么样兔子bt樱桃搜索磁力天堂
  • 聊城建网站哪家好今日财经最新消息
  • 网站开发合同是否要交印花税发帖秒收录的网站
  • 那个网站做国外售货交换友链是什么意思
  • 丰都专业网站建设公司品牌营销推广方案怎么做
  • 英德网站seo关键词优化seo
  • 网站文章优化怎么做seo报价单
  • 自助手机建站软文发布平台有哪些
  • 网站制作网站维护抖音seo优化软件
  • 电脑网站滚动字幕怎么做菏泽资深seo报价
  • dw可以用来做网站吗定制网站多少钱
  • 移动网站建设哪家便宜制作网页
  • html5网站建设加盟营销推广方案模板
  • 中文儿童网站模板推广营销