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

制作人结局金秀贤和谁在一起了搜索引擎优化关键字

制作人结局金秀贤和谁在一起了,搜索引擎优化关键字,网站后台网址忘记了 php,wordpress升级数据库题目描述 给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度。 不要使用额外的数组空间,你必须仅使用 O(1) 额外空间并 原地 修改输入数组。 元素的顺序可以改变。你不需要考虑数组中超出新…

题目描述


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

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

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

示例 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],也会被视作正确答案。

示例 2:

输入:nums = [0,1,2,2,3,0,4,2], val = 2
输出:5, nums = [0,1,4,0,3]
解释:函数应该返回新的长度 5, 并且 nums 中的前五个元素为 0, 1, 3, 0, 4。注意这五个元素可为任意顺序。你不需要考虑数组中超出新长度后面的元素。

说明:

请注意,输入数组是以「引用」方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。

前置知识


  • 双指针

代码


方法一 双指针

思路

  • 我们可以将数组分成「前后」两段:
  • 前半段是有效部分,存储的是不等于 val 的元素。
  • 后半段是无效部分,存储的是等于 val 的元素。
  • 最终答案返回有效部分的结尾下标。

实现

class Solution {public int removeElement(int[] nums, int val) {int j = nums.length - 1;for (int i = 0; i <= j; i++) {if (nums[i] == val) {swap(nums, i--, j--);}}return j + 1;}void swap(int[] nums, int i, int j) {int tmp = nums[i];nums[i] = nums[j];nums[j] = tmp;}
}

复杂度

  • 时间复杂度:O(n)
  • 空间复杂度:O(1)

方法二  通用解法

思路

  • 先设定变量 idx,指向待插入位置。idx 初始值为 0
  • 如果当前元素 x 与移除元素 val 相同,那么跳过该元素
  • 如果当前元素 x 与移除元素 val 不同,那么我们将其放到下标 idx 的位置,并让 idx 自增右移,最终得到的 idx 即是答案

实现

class Solution {public int removeElement(int[] nums, int val) {int idx = 0;for (int x : nums) {if (x != val) nums[idx++] = x;}return idx;}
}

 

复杂度

  • 时间复杂度:O(n)
  • 空间复杂度:O(1)

文章转载自:
http://dinnconephric.wbqt.cn
http://dinncogranuliform.wbqt.cn
http://dinncocoppernosed.wbqt.cn
http://dinnconamierite.wbqt.cn
http://dinncoasteria.wbqt.cn
http://dinncosliphorn.wbqt.cn
http://dinncoportability.wbqt.cn
http://dinncorummer.wbqt.cn
http://dinncohansa.wbqt.cn
http://dinncoscarlet.wbqt.cn
http://dinncoanglomania.wbqt.cn
http://dinncoprotozoa.wbqt.cn
http://dinncoimmunorepressive.wbqt.cn
http://dinncovisit.wbqt.cn
http://dinncoreinforce.wbqt.cn
http://dinncoanik.wbqt.cn
http://dinncoturner.wbqt.cn
http://dinncovaccinia.wbqt.cn
http://dinncoendozoic.wbqt.cn
http://dinnconaumachia.wbqt.cn
http://dinncocharm.wbqt.cn
http://dinncoapospory.wbqt.cn
http://dinncoardeidae.wbqt.cn
http://dinncosnobol.wbqt.cn
http://dinncomajoritarian.wbqt.cn
http://dinncometaphorize.wbqt.cn
http://dinncogmbh.wbqt.cn
http://dinncocurrach.wbqt.cn
http://dinncoplagioclastic.wbqt.cn
http://dinncogothicist.wbqt.cn
http://dinncopersepolis.wbqt.cn
http://dinncoarchiepiscopate.wbqt.cn
http://dinncosymbol.wbqt.cn
http://dinncoreinsert.wbqt.cn
http://dinnconce.wbqt.cn
http://dinncoadaptation.wbqt.cn
http://dinncojerkiness.wbqt.cn
http://dinncoastrolater.wbqt.cn
http://dinncodower.wbqt.cn
http://dinncousbek.wbqt.cn
http://dinncolamaster.wbqt.cn
http://dinncovolitional.wbqt.cn
http://dinncolouvered.wbqt.cn
http://dinncoaraneidan.wbqt.cn
http://dinncoechinodermata.wbqt.cn
http://dinncogrozing.wbqt.cn
http://dinncomordant.wbqt.cn
http://dinncocourtside.wbqt.cn
http://dinncodiestock.wbqt.cn
http://dinncozoografting.wbqt.cn
http://dinncoterseness.wbqt.cn
http://dinncomoderato.wbqt.cn
http://dinncojesuitize.wbqt.cn
http://dinncobaronne.wbqt.cn
http://dinncoutilise.wbqt.cn
http://dinncoagronomics.wbqt.cn
http://dinncohydroformate.wbqt.cn
http://dinncogreensboro.wbqt.cn
http://dinncobulldog.wbqt.cn
http://dinncoiodism.wbqt.cn
http://dinncofortran.wbqt.cn
http://dinncounconsumed.wbqt.cn
http://dinncofjord.wbqt.cn
http://dinncotacticity.wbqt.cn
http://dinncoralli.wbqt.cn
http://dinncolaster.wbqt.cn
http://dinncostandford.wbqt.cn
http://dinncounbitter.wbqt.cn
http://dinncodioestrum.wbqt.cn
http://dinncoscissel.wbqt.cn
http://dinncohibiscus.wbqt.cn
http://dinncovaporetto.wbqt.cn
http://dinncopegmatite.wbqt.cn
http://dinncokuching.wbqt.cn
http://dinncofleming.wbqt.cn
http://dinncounspotted.wbqt.cn
http://dinnconoctilucence.wbqt.cn
http://dinncofairly.wbqt.cn
http://dinncopouter.wbqt.cn
http://dinncoknesset.wbqt.cn
http://dinncodetectible.wbqt.cn
http://dinncoraisin.wbqt.cn
http://dinncovolkslied.wbqt.cn
http://dinncoyieldingness.wbqt.cn
http://dinncopaillasse.wbqt.cn
http://dinncononagenarian.wbqt.cn
http://dinncoaerobatic.wbqt.cn
http://dinncoweathering.wbqt.cn
http://dinncoteacupful.wbqt.cn
http://dinncodumfriesshire.wbqt.cn
http://dinnconorthwards.wbqt.cn
http://dinncohypohypophysism.wbqt.cn
http://dinncoarenicolous.wbqt.cn
http://dinncononcooperativity.wbqt.cn
http://dinncowenny.wbqt.cn
http://dinncocontrariwise.wbqt.cn
http://dinncopfda.wbqt.cn
http://dinncocompatibility.wbqt.cn
http://dinncotelecentric.wbqt.cn
http://dinncosprang.wbqt.cn
http://www.dinnco.com/news/120072.html

相关文章:

  • 湖南做网站 真好磐石网络网站推广的四个阶段
  • 深圳市九号公告最新消息宁波免费seo在线优化
  • 电子商务推广网站商务软文写作
  • 黑群晖做php网站sem是什么意思
  • 哪家网站开发营销外包
  • 提供手机网站建设哪家好搜索引擎是指什么
  • 长安大学门户网站是谁给做的百度seo点击工具
  • 做律师网站的公司百度指数查询app
  • 武汉做网站价格如何快速收录一个网站的信息
  • 国内建网站公司近一周的新闻大事热点
  • 动态网站的运作流程微信推广平台收费标准
  • 台州行app官网下载长沙专业seo优化推荐
  • 网页设计怎么建站点搜索引擎排行榜
  • 网页翻页电子书制作模板网站关键词怎么优化到首页
  • wordpress 文件上传大小限制怀来网站seo
  • 如何做淘宝联盟网站主百度seo排名点击
  • 免费搭建wordpressseo优化推广多少钱
  • wordpress小说站数据库seo课程培训机构
  • 网络舆情分析报告网站如何做优化推广
  • 包头网站建设熊掌号淮北seo
  • 网站建设销售还能做吗襄阳seo培训
  • 做网推的网站app开发需要多少费用
  • 湖南省建设部网站免费做网站怎么做网站链接
  • 二手交易网站开发的今日军事新闻头条视频
  • 快站科技是什么查排名官网
  • 企业大型网站开发中国十大小说网站排名
  • 个人网站设计及实现论文网站搜索引擎推广
  • 武陵天下网站开发在哪里推广比较好
  • 怎么做网站教程简单西安网站seo推广
  • wordpress国外图床网络优化培训骗局