Remove Duplicates from Sorted Array I&&II——怎样防超时
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
For example,
Given input array nums = [1,1,2],
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length.
class Solution {
public:
int removeDuplicates(int A[], int n) {
if(!n)return NULL;
int num=1,i;
for(i=1;i<n;++i)
if(A[i]!=A[i-1])
A[num++]=A[i];
return num;
}
};
II 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.
ii.接上题,增加一个count变量来记录key出现的次数。
class Solution {
public:
int removeDuplicates(int A[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (n == 0)
return 0;
int key = A[0];
int count = 0;
int start = 0;
for(int i = 0; i < n; i++)
if (key == A[i])
count++;
else
{
for(int j = 0; j < min(2, count); j++)
A[start++] = key;
key = A[i];
count = 1;
}
for(int j = 0; j < min(2, count); j++)
A[start++] = key;
return start;
}
};
Remove Duplicates from Sorted Array I&&II——怎样防超时的更多相关文章
- LeetCode:Remove Duplicates from Sorted Array I II
LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...
- 【leetcode】Remove Duplicates from Sorted Array I & II(middle)
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element
Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...
- Remove Element,Remove Duplicates from Sorted Array,Remove Duplicates from Sorted Array II
以下三个问题的典型的两个指针处理数组的问题,一个指针用于遍历,一个指针用于指向当前处理到位置 一:Remove Element Given an array and a value, remove a ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- [leetcode] 80. Remove Duplicates from Sorted Array II (Medium)
排序数组去重题,保留重复两个次数以内的元素,不申请新的空间. 解法一: 因为已经排好序,所以出现重复的话只能是连续着,所以利用个变量存储出现次数,借此判断. Runtime: 20 ms, faste ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
随机推荐
- 《Java程序设计》第九周学习总结 20165218 2017-2018-2
20165218 2017-2018-2 <Java程序设计>第9周学习总结 教材学习内容总结 第13章 Java网络编程 URL类 位于java.net包,使用URL创建对象的应用程序称 ...
- 解题:USACO06DEC Milk Patterns
题面 初见SA 用了一个常见的按$height$分组的操作:二分答案,然后按$height$分组,遇到一个$height$小于$mid$的就丢进下一组并更新答案,如果最多的那组不少于$k$个说明可行 ...
- c++多态性详解(转)
什么是多态? 多态一词最初来源于希腊语,意思是具有多种形式或形态的情形,当然这只是字面意思,它在C++语言中多态有着更广泛的含义. 这要先从对象的类型说起!对象的类型有两种: 实例:Derived1类 ...
- Redundant data in update statements
Q: Hibernate generates UPDATE statements, which include all columns, regardless of whether I'm cha ...
- eclipse中配置jbpm3.2插件
1.什么是jbpm?为什么要使用jbpm呢? 通俗一点讲,jbpm是一个负责管理工作流的一个产品,那么什么是工作流呢,所谓的工作流就是在办公自动化系统中,提交申请,申请经过多个部门领导审批,完成该流程 ...
- 009.C++ const使用
1.引例 class complex { public: complex(, ) : re (r), im (i) {} complex& operator += (const complex ...
- HDU 4352 数位dp
XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- centos7安装ZABBIX 3.0+ 邮件报警【OK】
设置主机名: vi /etc/hosts 10.0.0.252 zabbix-server hostnamectl set-hostname 关闭防火墙: systemctl stop firew ...
- Oracle用imp导入dmp文件记录一下
---------------------------------------------------------------------------------------------------- ...
- 算法专题-STL篇
这篇文章着重记录c++中STL的用法.主要粗略的介绍其用法,以知识点的形式呈现其功能,不会深入源码分析其工作原理. 排序和检索. sort(a,a+n),对a[0]往后的n个元素(包括a[0])进行排 ...