[LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
Example 1:
Given nums = [1,1,1,2,2,3], Your function should return length =5, with the first five elements ofnumsbeing1, 1, 2, 2and 3 respectively. It doesn't matter what you leave beyond the returned length.
Example 2:
Given nums = [0,0,1,1,1,1,2,3,3], Your function should return length =7, with the first seven elements ofnumsbeing modified to0, 0, 1, 1, 2, 3 and 3 respectively. It doesn't matter what values are set beyond the returned length.
Clarification:
Confused why the returned value is an integer but your answer is an array?
Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.
Internally you can think of this:
// nums is passed in by reference. (i.e., without making a copy)
int len = removeDuplicates(nums); // any modification to nums in your function would be known by the caller.
// using the length returned by your function, it prints the first len elements.
for (int i = 0; i < len; i++) {
print(nums[i]);
}
26. Remove Duplicates from Sorted Array 的拓展,如果允许最多两次重复,返回去除之后的长度。
解法:同样使用双指针,增加一个变量记录出现的字符数量。
Java:
public class Solution {
public int removeDuplicates(int[] A) {
if (A == null || A.length == 0)
return 0;
int pre = A[0];
boolean flag = false;
int count = 0;
// index for updating
int o = 1;
for (int i = 1; i < A.length; i++) {
int curr = A[i];
if (curr == pre) {
if (!flag) {
flag = true;
A[o++] = curr;
continue;
} else {
count++;
}
} else {
pre = curr;
A[o++] = curr;
flag = false;
}
}
return A.length - count;
}
}
Java:
public class Solution {
public int removeDuplicates(int[] A) {
if (A.length <= 2)
return A.length;
int prev = 1; // point to previous
int curr = 2; // point to current
while (curr < A.length) {
if (A[curr] == A[prev] && A[curr] == A[prev - 1]) {
curr++;
} else {
prev++;
A[prev] = A[curr];
curr++;
}
}
return prev + 1;
}
}
Python:
class Solution:
# @param a list of integers
# @return an integer
def removeDuplicates(self, A):
if not A:
return 0 last, i, same = 0, 1, False
while i < len(A):
if A[last] != A[i] or not same:
same = A[last] == A[i]
last += 1
A[last] = A[i]
i += 1 return last + 1
C++:
class Solution {
public:
int removeDuplicates(int A[], int n) {
if (n <= 2) return n;
int pre = 0, cur = 1, count = 1;
while (cur < n) {
if (A[pre] == A[cur] && count == 0) ++cur;
else {
if (A[pre] == A[cur]) --count;
else count = 1;
A[++pre] = A[cur++];
}
}
return pre + 1;
}
};
类似题目:
[LeetCode] 26. Remove Duplicates from Sorted Array 有序数组中去除重复项
All LeetCode Questions List 题目汇总
[LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II的更多相关文章
- [LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)
[LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...
- [LeetCode]26. Remove Duplicates from Sorted Array删除排序数组中的重复项
Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...
- 【LeetCode】Remove Duplicates from Sorted Array(删除排序数组中的重复项)
这道题是LeetCode里的第26道题. 题目描述: 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数 ...
- LeetCode#26 | Remove Duplicates from Sorted Array 删除有序数组中的重复元素
一.题目 Description Given a sorted array, remove the duplicates in-place such that each element appear ...
- [LeetCode] 83. Remove Duplicates from Sorted List ☆(从有序链表中删除重复项)
描述 Given a sorted linked list, delete all duplicates such that each element appear only once. Exampl ...
- 026 Remove Duplicates from Sorted Array 从排序数组中删除重复项
给定一个有序数组,你需要原地删除其中的重复内容,使每个元素只出现一次,并返回新的长度.不要另外定义一个数组,您必须通过用 O(1) 额外内存原地修改输入的数组来做到这一点.示例:给定数组: nums ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- lintcode :Remove Duplicates from Sorted Array 删除排序数组中的重复数字
题目: 删除排序数组中的重复数字 给定一个排序数组,在原数组中删除重复出现的数字,使得每个元素只出现一次,并且返回新的数组的长度. 不要使用额外的数组空间,必须在原地没有额外空间的条件下完成. 样例 ...
随机推荐
- ArcGIS pro 发布地图服务(一)动态地图服务
1.软件:arcgis pro 2.4 数据:.mxd文档. 2.导入mxd文档. 3.登录portal账号 4.分析—发布 5.在server中的地图服务 JavaScript api 查看 6. ...
- SQL进阶系列之3三值逻辑与NULL
写在前面 普通编程语言里的布尔型只有true和false两个值,这种逻辑体系被称为二值逻辑,而SQL语言里,还有第三个值unknown,因此SQL的逻辑体系被称为三值逻辑. Why SQL存在三值逻辑 ...
- 洛谷 P1816 忠诚题解
题目描述 老管家是一个聪明能干的人.他为财主工作了整整10年,财主为了让自已账目更加清楚.要求管家每天记k次账,由于管家聪明能干,因而管家总是让财主十分满意.但是由于一些人的挑拨,财主还是对管家产生了 ...
- P1941 飞扬的小鸟[dp]
题目描述 Flappy Bird是一款风靡一时的休闲手机游戏.玩家需要不断控制点击手机屏幕的频率来调节小鸟的飞行高度,让小鸟顺利通过画面右方的管道缝隙.如果小鸟一不小心撞到了水管或者掉在地上的话,便宣 ...
- *P3694 邦邦的大合唱站队[dp]
题目描述 N个偶像排成一列,他们来自M个不同的乐队.每个团队至少有一个偶像. 现在要求重新安排队列,使来自同一乐队的偶像连续的站在一起.重新安排的办法是,让若干偶像出列(剩下的偶像不动),然后让出列的 ...
- 《CoderXiaoban团队》实验十 团队作业6:团队项目系统设计改进与详细设计
实验十 团队作业6:团队项目系统设计改进与详细设计 项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 实验十 团队作业6:团队项目系统设计改进与详细设计 团队名称 Code ...
- Linux——CentOS7没有第二张网卡的配置信息
前言 为了一个实验做测试,在VMware中配置了环境,但是配置了双网卡后发现第二张网卡没有配置文件. 都是些基本命令就不写了,图里也有. 系统 : CentOS7.6 步骤 查看网卡信息 使用ip a ...
- windows 10 下使用Navicat for oracle 数据库还原
一.前期准备 1.安装windows 10系统 2.安装oracle 11g 数据库 3.安装PLsql(也不需要) 4.安装sqlplus(这个必须有) 5.使用下面这个东西新建数据库(不懂创建的话 ...
- LeetCode 881. Boats to Save People
原题链接在这里:https://leetcode.com/problems/boats-to-save-people/ 题目: The i-th person has weight people[i] ...
- LeetCode 979. Distribute Coins in Binary Tree
原题链接在这里:https://leetcode.com/problems/distribute-coins-in-binary-tree/ 题目: Given the root of a binar ...