80. Remove Duplicates from Sorted Array II
来源:互联网 发布:p2p下载软件推荐 编辑:程序博客网 时间:2024/05/01 08:36
问题描述
Follow up for “Remove Duplicates”:
What if duplicates are allowed at most twice?For example,
Given sorted array nums = [1,1,1,2,2,3],Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn’t matter what you leave beyond the new length.
解决思路
很简单的ac了这一道题,但是因为其还要对数组进行改变,我生成了一个临时变量去存储新数组,然后再赋值给旧数组。但是看了别人的解答后觉得这种方法真的好蠢。直接贴大神的代码吧。看了也秒懂。代码
int removeDuplicates(vector<int>& nums) { int i = 0; for (int n : nums) if (i < 2 || n > nums[i-2]) nums[i++] = n; return i;}
0 0
- 80. Remove Duplicates from Sorted Array II
- 80.Remove Duplicates from Sorted Array II
- 80. Remove Duplicates from Sorted Array II
- 80. Remove Duplicates from Sorted Array II
- 80. Remove Duplicates from Sorted Array II
- 80. Remove Duplicates from Sorted Array II
- 80. Remove Duplicates from Sorted Array II
- 80. Remove Duplicates from Sorted Array II
- 80. Remove Duplicates from Sorted Array II
- 80. Remove Duplicates from Sorted Array II
- 80. Remove Duplicates from Sorted Array II
- 80. Remove Duplicates from Sorted Array II
- 80. Remove Duplicates from Sorted Array II
- 80. Remove Duplicates from Sorted Array II
- 80. Remove Duplicates from Sorted Array II
- 80. Remove Duplicates from Sorted Array II
- 80. Remove Duplicates from Sorted Array II
- 80. Remove Duplicates from Sorted Array II
- 2007年北理复试上机题
- C++之继承篇
- socket.io简单笔记1
- 简单的拍照dome
- Spring 源码分析(一) —— 迈向Spring之路
- 80. Remove Duplicates from Sorted Array II
- python遍历文件
- hdu 3790 最短路径&最低费用 题目不难,但要注意重边
- js 模拟键盘事件
- windows10下myeclipse配置maven
- ssh互信配置及简单流程分析
- android studio中导入RecyclerView
- C语言陷阱与缺陷学习之函数调用
- DISCUZ论坛积分充值和VIP购买二合一的支付插件,支持微信和支付宝