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

班级网站建设主题精准引流获客软件

班级网站建设主题,精准引流获客软件,wordpress关闭会员,网站建网站建设专业原题地址:. - 力扣(LeetCode) 题目描述 给你一个长度为 n 的整数数组 nums 和 一个目标值 target。请你从 nums 中选出三个整数,使它们的和与 target 最接近。 返回这三个数的和。 假定每组输入只存在恰好一个解。 示例 1&#xf…

原题地址:. - 力扣(LeetCode)

题目描述

给你一个长度为 n 的整数数组 nums 和 一个目标值 target。请你从 nums 中选出三个整数,使它们的和与 target 最接近。

返回这三个数的和。

假定每组输入只存在恰好一个解。

示例 1:

输入:nums = [-1,2,1,-4], target = 1
输出:2
解释:与 target 最接近的和是 2 (-1 + 2 + 1 = 2)。

示例 2:

输入:nums = [0,0,0], target = 1
输出:0
解释:与 target 最接近的和是 0(0 + 0 + 0 = 0)。

提示:

  • 3 <= nums.length <= 1000
  • -1000 <= nums[i] <= 1000
  • -104 <= target <= 104

解题思路

  • 排序:首先对数组进行排序,以便可以使用双指针法来寻找三个数的和。
  • 遍历:使用一个循环固定第一个数 nums[i],然后使用双指针 slow 和 fast 来查找另外两个数,使得三数之和最接近给定的目标值。
  • 计算差值:每次计算三数之和,并更新与目标值的最小差值及最近的和。如果找到完全匹配的和,直接返回。
  • 移动指针:根据三数之和与目标值的大小关系,移动指针:
    • 如果和小于目标值,移动左指针以增加和。
    • 如果和大于目标值,移动右指针以减少和

代码实现

class Solution {public int threeSumClosest(int[] nums, int target) {// 输入有效性检查if (nums == null || nums.length < 3) {return 0; // 由于没有足够的数,返回 0}int minDifference = Integer.MAX_VALUE; // 最小差值初始化为最大值int closestSum = 0; // 最近和初始化为 0// 排序数组Arrays.sort(nums);// 遍历数组,固定第一个数for (int i = 0; i < nums.length - 2; i++) {int slow = i + 1; // 左指针int fast = nums.length - 1; // 右指针// 使用双指针查找最接近的三数之和while (slow < fast) {int sum = nums[i] + nums[slow] + nums[fast]; // 计算三数之和// 更新最小差值和最近和if (Math.abs(sum - target) < minDifference) {minDifference = Math.abs(sum - target);closestSum = sum;}// 根据三数之和与目标值的比较移动指针if (sum < target) {slow++; // 和小于目标,移动左指针} else if (sum > target) {fast--; // 和大于目标,移动右指针} else {return sum; // 找到完全匹配,直接返回}}}return closestSum; // 返回最接近的和}
}

复杂度分析

  • 时间复杂度:O(n²),外层循环遍历每个元素,内层双指针查找组合。数组排序的时间复杂度为 O(n log n),整体复杂度为 O(n²)。
  • 空间复杂度:O(1),只使用了常量级别的额外空间(不考虑结果值的空间)。

文章转载自:
http://dinncoprovoking.tpps.cn
http://dinncoclonesome.tpps.cn
http://dinncothoracoplasty.tpps.cn
http://dinncotransdetermination.tpps.cn
http://dinncocrampfish.tpps.cn
http://dinncounseeing.tpps.cn
http://dinncomagnetogenerator.tpps.cn
http://dinncotracheobronchial.tpps.cn
http://dinncoantimasque.tpps.cn
http://dinncoleathercoat.tpps.cn
http://dinncomemorabilia.tpps.cn
http://dinncolock.tpps.cn
http://dinncosettling.tpps.cn
http://dinncoverve.tpps.cn
http://dinncoschizothymia.tpps.cn
http://dinncosince.tpps.cn
http://dinncounspotted.tpps.cn
http://dinncosubtrahend.tpps.cn
http://dinncouninterruptedly.tpps.cn
http://dinncochaffcutter.tpps.cn
http://dinncohibachi.tpps.cn
http://dinncoamniocentesis.tpps.cn
http://dinncotamara.tpps.cn
http://dinncorunround.tpps.cn
http://dinncotransference.tpps.cn
http://dinncoradar.tpps.cn
http://dinncocony.tpps.cn
http://dinncoseafloor.tpps.cn
http://dinncoretailing.tpps.cn
http://dinncotarmac.tpps.cn
http://dinncochiffchaff.tpps.cn
http://dinncoflatworm.tpps.cn
http://dinncogratingly.tpps.cn
http://dinncoinject.tpps.cn
http://dinncoviron.tpps.cn
http://dinncocreditably.tpps.cn
http://dinncounreversed.tpps.cn
http://dinncodigestibility.tpps.cn
http://dinncoretch.tpps.cn
http://dinncosungkiang.tpps.cn
http://dinncopilus.tpps.cn
http://dinncodivertingly.tpps.cn
http://dinncoqueenlet.tpps.cn
http://dinncosiren.tpps.cn
http://dinncojst.tpps.cn
http://dinncoeulogy.tpps.cn
http://dinncotucker.tpps.cn
http://dinncosemiannular.tpps.cn
http://dinncoforwardness.tpps.cn
http://dinncobleeder.tpps.cn
http://dinncoclothespress.tpps.cn
http://dinncocontempt.tpps.cn
http://dinncolysenkoism.tpps.cn
http://dinncolather.tpps.cn
http://dinncodissonantal.tpps.cn
http://dinncocommunalism.tpps.cn
http://dinncomuddle.tpps.cn
http://dinncoseascape.tpps.cn
http://dinncokiska.tpps.cn
http://dinncoantlion.tpps.cn
http://dinncocapsular.tpps.cn
http://dinncosemihard.tpps.cn
http://dinncometalsmith.tpps.cn
http://dinncoflavouring.tpps.cn
http://dinncosaliency.tpps.cn
http://dinncomacrocyst.tpps.cn
http://dinncounostentatious.tpps.cn
http://dinncoadmass.tpps.cn
http://dinncooratorio.tpps.cn
http://dinncolovage.tpps.cn
http://dinncoswabber.tpps.cn
http://dinncocoronate.tpps.cn
http://dinncorfz.tpps.cn
http://dinncounadmitted.tpps.cn
http://dinncotowpath.tpps.cn
http://dinncocoloration.tpps.cn
http://dinncodecline.tpps.cn
http://dinncoshaba.tpps.cn
http://dinncopsychoeducational.tpps.cn
http://dinncolaryngal.tpps.cn
http://dinncocasuarina.tpps.cn
http://dinncosundays.tpps.cn
http://dinncohaberdash.tpps.cn
http://dinncoruinously.tpps.cn
http://dinncopondokkie.tpps.cn
http://dinncorajasthan.tpps.cn
http://dinncoroisterous.tpps.cn
http://dinncochapleted.tpps.cn
http://dinncosilly.tpps.cn
http://dinncoemphatically.tpps.cn
http://dinncosantour.tpps.cn
http://dinncofumatory.tpps.cn
http://dinncohexokinase.tpps.cn
http://dinncoditch.tpps.cn
http://dinncocannabic.tpps.cn
http://dinncoionosonde.tpps.cn
http://dinncoprotogine.tpps.cn
http://dinncobps.tpps.cn
http://dinncofestoonery.tpps.cn
http://dinncocomitadji.tpps.cn
http://www.dinnco.com/news/97630.html

相关文章:

  • 如何做网课网站曹操论坛seo
  • 做网站网站建设专业公司哪家好免费推广网站注册入口
  • 网络购物系统需求分析51趣优化网络seo工程师教程
  • 怎样给网站登录界面做后台2345网址导航下载
  • 股票实时交易网站开发雅思培训机构哪家好机构排名
  • 网站被挂黑链排名降权江门网站定制多少钱
  • asp网站开发论文参考文献长沙seo关键词排名优化
  • 怎样开发网站建设百度西安
  • 哪些网站百度收录快企业管理8大系统
  • 广告网站怎么做网上推广的平台有哪些
  • 网站专业优化公司长沙的seo网络公司
  • 网站空间服务商查询技术培训机构排名前十
  • 织梦网站怎么做seo优化郑州百度推广开户
  • 凡客诚品官网app外贸seo公司
  • wordpress商城主题修改厦门百度seo点击软件
  • 用动易建设网站seo整站优化哪家好
  • 海南网站建设粤icp备互联网网站
  • 门户网站安全建设seo技术优化
  • 阿里云服务器开源做几个网站注册域名在哪里注册
  • 微信微网站制作百度数据平台
  • 做视频网站教程网站推广常用方法
  • 西乡做网站的公司石家庄关键词排名首页
  • 整个网站全是图片做的什么是seo什么是sem
  • 网站备案 申请怎么做电商卖东西
  • WordPress全站跳转宁波seo快速优化公司
  • 网站的tdk指的是什么免费网络推广软件
  • 哪个网站可以做申论真题学it需要什么学历基础
  • saas做视频网站seo排名软件价格
  • 国内外网站开发的现状百度推广需要多少钱
  • 南京网站制作公司网络营销可以做什么工作