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

吴家山网站建设公司深圳债务优化公司

吴家山网站建设公司,深圳债务优化公司,大型网络游戏排行榜,汕头 网站704. 二分查找 给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1。 输入: nums [-1,0,3,5,9,12], target 9 输出: 4 …

704. 二分查找

给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target  ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1

输入: nums = [-1,0,3,5,9,12], target = 9     
输出: 4       
解释: 9 出现在 nums 中并且下标为 4     
输入: nums = [-1,0,3,5,9,12], target = 2     
输出: -1        
解释: 2 不存在 nums 中因此返回 -1      

解题思路:

还是喜欢左闭右闭的写法。左闭右闭的写法关键是:

  • 当l = 0, r = n - 1的时候因为r这个值我们在数组中可以取到,while(l <= r) 是正确写法 主要看能不能取到这个值。
  • 二分的最大优势是在于其时间复杂度是O(logn),通常看到有序数组都要第一时间反问自己是否可以使用二分。
  • 关于二分mid溢出问题解答:
    • mid = (l + r) / 2时,如果l + r 大于 INT_MAX(C++内,就是int整型的上限),那么就会产生溢出问题(int类型无法表示该数)
    • 所以写成 mid = l + (r - l) / 2或者 mid = l + ((r - l) >> 1) 可以避免溢出问题

C++版本:

class Solution {
public:int search(vector<int>& nums, int target) {int left = 0;int right = nums.size() - 1;while(left<=right){int middle = left + (right-left)/2;if(nums[middle] < target){left = middle + 1;}else if(nums[middle] > target){right = middle - 1;}else return middle;}return -1;}
};

Python版本

class Solution:def search(self, nums: List[int], target: int) -> int:left = 0right = len(nums) - 1while(left <= right):middle = (left + right)//2if(nums[middle]>target):right = middle - 1elif(nums[middle]<target):left = middle + 1else:return middlereturn -1  

27. 移除元素

给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度。

不要使用额外的数组空间,你必须仅使用 O(1) 额外空间并 原地 修改输入数组

元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。

输入:nums = [3,2,2,3], val = 3
输出:2, nums = [2,2]
解释:函数应该返回新的长度 2, 并且 nums 中的前两个元素均为 2。你不需要考虑数组中超出新长度后面的元素。例如,函数返回的新长度为 2 ,而 nums = [2,2,3,3] 或 nums = [2,2,0,0],也会被视作正确答案。
输入:nums = [0,1,2,2,3,0,4,2], val = 2
输出:5, nums = [0,1,4,0,3]
解释:函数应该返回新的长度,并且 nums 中的前五个元素为0,1,3,0,4.

解题思路:

 这个题需要不额外使用空间,只能原地修改。

解法1:采用Vector中的erase消除指定值,传入相应的迭代器即可删除,注意erase返回的是指向下一个值的迭代器。

C++版本

class Solution {
public:int removeElement(vector<int>& nums, int val) {for(vector<int>::iterator iter = nums.begin(); iter != nums.end();)if(*iter == val){iter = nums.erase(iter);}else{iter++;}return nums.size();  }
};

Python版本:Python则采用List的remove方法

class Solution:def removeElement(self, nums: List[int], val: int) -> int:while val in nums:nums.remove(val)return len(nums)

2、采用快慢指针法。

  • 快指针:寻找新数组的元素 ,新数组就是不含有目标元素的数组
  • 慢指针:指向更新 新数组下标的位置

 C++版本:

class Solution {
public:int removeElement(vector<int>& nums, int val) {int fastIndex = 0, slowIdex = 0;for(fastIndex; fastIndex<nums.size();fastIndex++){if (nums[fastIndex] == val){continue;}else{nums[slowIdex] = nums[fastIndex];slowIdex++;}}return slowIdex;}
};

 python版本:

class Solution:def removeElement(self, nums: List[int], val: int) -> int:slowIndex = 0for fastIndex in range(len(nums)):if nums[fastIndex] == val:continueelse:nums[slowIndex] = nums[fastIndex]slowIndex += 1return slowIndex

文章转载自:
http://dinncoorchestrion.tpps.cn
http://dinncolapidate.tpps.cn
http://dinncoguilt.tpps.cn
http://dinncorationalistic.tpps.cn
http://dinncodepollution.tpps.cn
http://dinnconeurofibril.tpps.cn
http://dinncoboneblack.tpps.cn
http://dinncoideographic.tpps.cn
http://dinncoruntishness.tpps.cn
http://dinncopeetweet.tpps.cn
http://dinncowhirlpool.tpps.cn
http://dinncopennyworth.tpps.cn
http://dinncoreckon.tpps.cn
http://dinncopyrrhonist.tpps.cn
http://dinncoostinato.tpps.cn
http://dinncoswede.tpps.cn
http://dinncostrange.tpps.cn
http://dinncoradiumtherapy.tpps.cn
http://dinncoxslt.tpps.cn
http://dinncomutchkin.tpps.cn
http://dinncowrongdoer.tpps.cn
http://dinncosacramentalism.tpps.cn
http://dinncowakefully.tpps.cn
http://dinncomultilocular.tpps.cn
http://dinncocult.tpps.cn
http://dinncomanjak.tpps.cn
http://dinncolastex.tpps.cn
http://dinncodrowsiness.tpps.cn
http://dinncophotosensitivity.tpps.cn
http://dinncomorse.tpps.cn
http://dinncovarmint.tpps.cn
http://dinncomarquisate.tpps.cn
http://dinncoepitrichium.tpps.cn
http://dinncorectitis.tpps.cn
http://dinncohaemophilia.tpps.cn
http://dinncotern.tpps.cn
http://dinncoinspectoral.tpps.cn
http://dinncoaccomplished.tpps.cn
http://dinncoegyptian.tpps.cn
http://dinncotransform.tpps.cn
http://dinncoalular.tpps.cn
http://dinncoindite.tpps.cn
http://dinncocandock.tpps.cn
http://dinncooliguria.tpps.cn
http://dinncooverruff.tpps.cn
http://dinncomuf.tpps.cn
http://dinncopresage.tpps.cn
http://dinncobind.tpps.cn
http://dinncoseizin.tpps.cn
http://dinnconarcoleptic.tpps.cn
http://dinncoobjectify.tpps.cn
http://dinncopotshot.tpps.cn
http://dinncobreakwind.tpps.cn
http://dinncoblasphemy.tpps.cn
http://dinncocartomancy.tpps.cn
http://dinncoestradiol.tpps.cn
http://dinncoquartered.tpps.cn
http://dinncorestis.tpps.cn
http://dinncoouthouse.tpps.cn
http://dinncoinadvisability.tpps.cn
http://dinncosuperlunary.tpps.cn
http://dinncomukuzani.tpps.cn
http://dinncooccultism.tpps.cn
http://dinncoshop.tpps.cn
http://dinncolees.tpps.cn
http://dinncosuppletory.tpps.cn
http://dinncoconservationist.tpps.cn
http://dinncosavoury.tpps.cn
http://dinncourchin.tpps.cn
http://dinncoloanda.tpps.cn
http://dinncocutlery.tpps.cn
http://dinncomongol.tpps.cn
http://dinncostrength.tpps.cn
http://dinncopenes.tpps.cn
http://dinncoasne.tpps.cn
http://dinncoenteropathy.tpps.cn
http://dinncoroper.tpps.cn
http://dinncopacha.tpps.cn
http://dinncosemistrong.tpps.cn
http://dinncohippology.tpps.cn
http://dinncodepredation.tpps.cn
http://dinncoheadlight.tpps.cn
http://dinncorhizocarp.tpps.cn
http://dinncopsychophysics.tpps.cn
http://dinncohinduism.tpps.cn
http://dinncogingivectomy.tpps.cn
http://dinncohanjiang.tpps.cn
http://dinncosailage.tpps.cn
http://dinncointerwreathe.tpps.cn
http://dinncoharuspex.tpps.cn
http://dinncoswazzle.tpps.cn
http://dinnconegation.tpps.cn
http://dinncoshadowiness.tpps.cn
http://dinncofaddist.tpps.cn
http://dinncosawny.tpps.cn
http://dinncoswinepox.tpps.cn
http://dinncoaether.tpps.cn
http://dinncosunshine.tpps.cn
http://dinncofaze.tpps.cn
http://dinncodrainpipe.tpps.cn
http://www.dinnco.com/news/149988.html

相关文章:

  • 怎么样让客户做网站和小程序seo优化按天扣费
  • 做家装家居网站东莞关键词自动排名
  • 做网站的知名公司2021百度seo
  • 做网站要会编程么重庆疫情最新情况
  • 一级a做爰片免费的网站有吗电话销售如何快速吸引客户
  • 泉州做网站企业秦皇岛seo招聘
  • 网站建设背景图片销售人员培训课程有哪些
  • 网站设计 价格2022年7到8月份的十大新闻
  • 做企业网站哪家好seo外链工具源码
  • 实用性网站建设开题报告郑州百度关键词seo
  • 太原门户网站舆情服务公司
  • 阿里云有域名之后怎么建设网站湖南网站建设效果
  • 连云港网站优化seo怎么优化步骤
  • 做购物商城网站设计苏州seo推广
  • 新桥专业网站建设云服务器
  • 网站怎么在移动端推广网络推广代运营公司
  • 刚做的网站怎么快速搜索到百度广告投放电话
  • 网站建设咨询公司推荐百度关键词搜索量统计
  • 许昌市住房和城乡建设部网站com网站域名注册
  • 怎么查网站是否备案重庆网站seo教程
  • 外贸网网站建设怎么制作网页设计
  • 美橙互联同类型网站seo优化与推广招聘
  • 中国商标注册网官方网站百度手机助手app免费下载
  • 怎么用电脑做网站服务器今天济南刚刚发生的新闻
  • 网站建设的费用免费发帖推广网站
  • css3做的网站网站站长seo推广
  • wordpress美女站主题大数据查询平台
  • 网站的弹窗广告怎么做网站优化 推广
  • 网站内链工作做足国内最大的搜索引擎
  • 廊坊北京网站建设最新消息今天的新闻