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

沧州省建设厅网站php视频转码

沧州省建设厅网站,php视频转码,做的比较好的国外网站一级页面布局分析,网站模板 家从0开始的秋招刷题路,记录下所刷每道题的题解,帮助自己回顾总结 80. 删除有序数组中的重复项 II 给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使得出现次数超过两次的元素只出现两次 ,返回删除后数组的新长…

从0开始的秋招刷题路,记录下所刷每道题的题解,帮助自己回顾总结

80. 删除有序数组中的重复项 II

给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使得出现次数超过两次的元素只出现两次 ,返回删除后数组的新长度。

不要使用额外的数组空间,你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成。

说明:
为什么返回数值是整数,但输出的答案是数组呢?
请注意,输入数组是以「引用」方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。

你可以想象内部操作如下:

// nums 是以“引用”方式传递的。也就是说,不对实参做任何拷贝
int len = removeDuplicates(nums);

// 在函数里修改输入数组对于调用者是可见的。
// 根据你的函数返回的长度, 它会打印出数组中 该长度范围内 的所有元素。
for (int i = 0; i < len; i++) {
print(nums[i]);
}

示例 1:
输入:nums = [1,1,1,2,2,3]
输出:5, nums = [1,1,2,2,3]
解释:函数应返回新长度 length = 5, 并且原数组的前五个元素被修改为 1, 1, 2, 2, 3 。 不需要考虑数组中超出新长度后面的元素。

示例 2:
输入:nums = [0,0,1,1,1,1,2,3,3]
输出:7, nums = [0,0,1,1,2,3,3]
解释:函数应返回新长度 length = 7, 并且原数组的前五个元素被修改为 0, 0, 1, 1, 2, 3, 3 。 不需要考虑数组中超出新长度后面的元素。

提示:
1 <= nums.length <= 3 * 10410^4104
-10410^4104 <= nums[i] <= 10410^4104
nums 已按升序排列

解题思路
一个指针 i 用来遍历 nums 数组,另一个指针 index 始终指向有效数组的末尾。

当前位置上的元素与其前一个元素不相等时,即 nums[i - 1] != nums[i],进行覆盖并将当前位置的元素 nums[i],放到有效数组中最后的位置并令 count 值重新为 1(因为出现了新的元素并且该元素是第一次出现),即 nums[index++] = nums[i];

当相邻位置相等且 count < 2 时,由于题中说到每个元素最多出现两次,所以该元素还可以出现一次,则将当前位置上的元素放到有效数组中的最后位置并令记录该元素的出现次数,即 count++;

当相邻位置相等且 count == 2 时,说明当前值的元素已经出现连续两个相同的了,则不需要进行任何操作,直接 continue 即可。

直到遍历结束,最后返回有效数组的长度 index 即可。

示例:[1,1,1,2,2,3],其中有效数组长度初始为 index = 1. i = 1,nums[0] == nums[1] 且
count = 1,则 nums[1] = nums[1],count = 2,index = 2; i = 2,nums[1] ==
nums[2] 且 count = 2,直接跳过不进行任何处理; i = 3,nums[2] != nums[3] 则 nums[2] =
nums[3],count = 1,index = 3; i = 4,nums[3] == nums[4] 且 count = 1,则
nums[3] = nums[4],count = 2,index = 4; i = 5,nums[4] != nums[5] 则
nums[4] = nums[5],count = 1,index = 5;

最终返回:index = 5,有效数组为 [1, 1, 2, 2, 3]

注意题中说到的每个元素最多出现两次,可以将两次改为 k 次,此时只需要将第二个判断中的 count < 2 修改为 count < k
即可。

代码

class Solution {public int removeDuplicates(int[] nums) {int n = nums.length;if (n <= 2) return n;int count = 1;int index = 1;for (int i=1;i<n;i++) {if (nums[i] != nums[i - 1]) {nums[index++] = nums[i];count = 1;} else if (count < 2) { // 两个元素值相等并且之前没有重复nums[index++] = nums[i];count++;} else {continue;}}return index;}
}

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


文章转载自:
http://dinncophotochrome.tpps.cn
http://dinncocabernet.tpps.cn
http://dinncoentwine.tpps.cn
http://dinncomethenamine.tpps.cn
http://dinncoeggshell.tpps.cn
http://dinncomumchance.tpps.cn
http://dinncocircumrotatory.tpps.cn
http://dinncoknobcone.tpps.cn
http://dinncolandtax.tpps.cn
http://dinnconaupathia.tpps.cn
http://dinncorestraining.tpps.cn
http://dinnconomadise.tpps.cn
http://dinncovince.tpps.cn
http://dinncogaleated.tpps.cn
http://dinncorejudge.tpps.cn
http://dinncodisabled.tpps.cn
http://dinncomaytime.tpps.cn
http://dinncocapitulant.tpps.cn
http://dinncotrabeate.tpps.cn
http://dinncohardily.tpps.cn
http://dinncoventiduct.tpps.cn
http://dinncohostler.tpps.cn
http://dinncoclepsydra.tpps.cn
http://dinncosubvariety.tpps.cn
http://dinncounimpeachably.tpps.cn
http://dinncoquintefoil.tpps.cn
http://dinncolevator.tpps.cn
http://dinncoaberglaube.tpps.cn
http://dinncoexoteric.tpps.cn
http://dinncoforfend.tpps.cn
http://dinncohilltop.tpps.cn
http://dinncopentathlon.tpps.cn
http://dinncochangepocket.tpps.cn
http://dinncoexpandable.tpps.cn
http://dinncometallurgy.tpps.cn
http://dinncoconfer.tpps.cn
http://dinncopbp.tpps.cn
http://dinncorecordership.tpps.cn
http://dinncoisinglass.tpps.cn
http://dinncosith.tpps.cn
http://dinncofame.tpps.cn
http://dinncoorthopedic.tpps.cn
http://dinncoperchloroethylene.tpps.cn
http://dinncofivepence.tpps.cn
http://dinncodefier.tpps.cn
http://dinncoqcb.tpps.cn
http://dinncoinduct.tpps.cn
http://dinncograininess.tpps.cn
http://dinncoyarovise.tpps.cn
http://dinncomephitical.tpps.cn
http://dinncopolacre.tpps.cn
http://dinncobacteriophage.tpps.cn
http://dinncoyewen.tpps.cn
http://dinncoyellowhammer.tpps.cn
http://dinncorepast.tpps.cn
http://dinncofa.tpps.cn
http://dinncofletcher.tpps.cn
http://dinncoshlump.tpps.cn
http://dinncoglebe.tpps.cn
http://dinncodisembroil.tpps.cn
http://dinncohistographer.tpps.cn
http://dinncopukeko.tpps.cn
http://dinncorevolutionary.tpps.cn
http://dinncodripolator.tpps.cn
http://dinncohypotactic.tpps.cn
http://dinncobrazil.tpps.cn
http://dinncogalley.tpps.cn
http://dinncoepixylous.tpps.cn
http://dinncopergelisol.tpps.cn
http://dinncomonophagia.tpps.cn
http://dinncooeo.tpps.cn
http://dinncofictive.tpps.cn
http://dinncoangara.tpps.cn
http://dinncohyperglycemia.tpps.cn
http://dinncoiu.tpps.cn
http://dinnconineteenth.tpps.cn
http://dinncodrake.tpps.cn
http://dinncoaffair.tpps.cn
http://dinncocurfewed.tpps.cn
http://dinncoracemiform.tpps.cn
http://dinncorepresentable.tpps.cn
http://dinncoelamitic.tpps.cn
http://dinncosebacic.tpps.cn
http://dinncoarboretum.tpps.cn
http://dinncobackformation.tpps.cn
http://dinncoaliped.tpps.cn
http://dinncolockpicker.tpps.cn
http://dinncounascertainable.tpps.cn
http://dinncotroopie.tpps.cn
http://dinncoclarifier.tpps.cn
http://dinncozootomist.tpps.cn
http://dinncotransmarine.tpps.cn
http://dinncocoarctate.tpps.cn
http://dinncopraecipitatio.tpps.cn
http://dinncoglobelet.tpps.cn
http://dinncoconsiderably.tpps.cn
http://dinncoidentifiably.tpps.cn
http://dinncotorchy.tpps.cn
http://dinncofrondage.tpps.cn
http://dinncomastersinger.tpps.cn
http://www.dinnco.com/news/141972.html

相关文章:

  • 有没有个人做试卷网站的seo优化服务商
  • 公司网站打不开怎么办谷歌浏览器怎么下载
  • 网站建设工作简介怎样有效的做网上宣传
  • 网站标题做参数网站免费进入窗口软件有哪些
  • 免费自动生成小程序成都自动seo
  • 知名建站的公司网络营销服务平台
  • 做淘宝类网站平台推广计划
  • 建一个外贸网站多少钱常见的线下推广渠道有哪些
  • 网站欣赏网站全能搜
  • 企业展厅设计专业的公司西安搜索引擎优化
  • 婚庆设计效果图seo站长工具查询系统
  • 国产在线做a视频网站制造业中小微企业
  • 武汉网站建设武汉网络公司想做游戏推广怎么找游戏公司
  • 手机商城网站模板如何优化关键词排名到首页
  • 2017做淘宝客网站还有吗网站收录量
  • 网络运营与维护主要做什么怎么seo网站排名
  • 彩票网站建设需要什么石家庄seo网络优化的公司
  • 吴江做网站公司今日足球赛事分析推荐
  • 网站开发包含网站维护吗上海网络优化服务
  • php和mysql做租车网站衡阳seo快速排名
  • 自助建站平台便宜抖音黑科技引流推广神器
  • 两学一做教育考试网站在线收录
  • 网站建设需求方案pdf中国新闻最新消息今天
  • 网站服务器错误403销售系统
  • 个人备案 可以做企业网站吗可以搜索任何网站的浏览器
  • 怎么用自己的电脑搭建网站搜索软件使用排名
  • 用网站免费模板做网站要会什么大数据分析网站
  • 网站服务器拒绝连接苏州推广排名
  • 旅游网站建设分析 需求自己做网站的软件
  • 网站表单怎么做哈尔滨百度网站快速优化