首页
题库
面试
求职
学习
竞赛
More+
所有博客
搜索面经/职位/试题/公司
搜索
我要招人
去企业版
登录 / 注册
首页
>
试题广场
>
删除有序数组中重复的元素 ii
[编程题]删除有序数组中重复的元素 ii
热度指数:13042
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32M,其他语言64M
算法知识视频讲解
给定一个有序数组,删除其中部分元素,使得剩下的每个数最多出现2次。要求删除的数的数量尽可能少。
例如:
给出有序数组 A
=[1,1,1,2,2,3],
你给出的函数应该返回
length =5, A 变为[1,1,2,2,3].
马上挑战
算法知识视频讲解
提交运行
算法知识视频讲解
添加笔记
求解答(0)
邀请回答
收藏(87)
分享
提交结果有问题?
52个回答
3篇题解
开通博客
mythwind
发表于 2022-04-29 09:18:22
快慢双指针,快指针筛选,慢指针赋值。 int index = 2; for (int i = 0; i < A.length; i ++) { if (A[i] != A[index - 2]) { A[index++] = A[i]; } } return index;
华科不平凡
发表于 2020-09-24 17:45:14
一个通用的思路:用index记录新数组的下标,遍历旧数组,如果当前元素与A[index-2]的元素不相同,则表示这个数应该放入新数组。(其中2可以变为1,3,4...)代码如下: // // Created by jt on 2020/9/24. // class Solution { public
展开全文
魏北北
发表于 2023-08-01 21:40:24
/** * * @param A int整型一维数组 * @param ALen int A数组长度 * @return int整型 */ int removeDuplicates(int* A, int ALen) { // write code here int i
展开全文
问题信息
数组
双指针
难度:
52条回答
87收藏
18586浏览
热门推荐
通过挑战的用户
查看代码
牛客54825...
2023-02-10 09:40:56
牛客17704...
2022-10-23 16:59:12
许你在右的年华
2022-09-13 08:13:47
hllpj
2022-09-01 00:30:35
=120191...
2022-08-23 17:18:18
相关试题
神奇的数字
排序
双指针
评论
(46)
和为S的两个数字
数组
数学
双指针
评论
(1511)
来自
“一战通offer”互联...
最小面积子矩阵
动态规划
双指针
前缀和
评论
(46)
如图 1 表示使用快表(页表)的虚...
编程基础
评论
(1)
订单表order_table全部记...
查找
数据库
数据分析
SQL
评论
(1)
删除有序数组中重复的元素 ii
扫描二维码,关注牛客网
意见反馈
下载牛客APP,随时随地刷题
import java.util.*; public class Solution { public int removeDuplicates(int[] A) { } }
class Solution { public: int removeDuplicates(int A[], int n) { } };
# # # @param A int整型一维数组 # @return int # class Solution: def removeDuplicates(self , A ): # write code here
/** * * @param A int整型一维数组 * @return int整型 */ function removeDuplicates( A ) { // write code here } module.exports = { removeDuplicates : removeDuplicates };
# # # @param A int整型一维数组 # @return int # class Solution: def removeDuplicates(self , A ): # write code here
package main /** * * @param A int整型一维数组 * @return int整型 */ func removeDuplicates( A []int ) int { // write code here }
/** * * @param A int整型一维数组 * @param ALen int A数组长度 * @return int整型 */ int removeDuplicates(int* A, int ALen) { // write code here }