leetcode:Remove Duplicates from Sorted Array II (允许重复一次,去掉数组多余数字)【面试算法题】
来源:互联网 发布:sqlserver日期格式化 编辑:程序博客网 时间:2024/04/30 01:02
题目:
Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice?
For example,
Given sorted array A = [1,1,1,2,2,3]
,
Your function should return length = 5
, and A is now [1,1,2,2,3]
.
应为允许一个重复,所以判断就不是相邻而是相隔一个的数字是否不一样。
不一样就需要更新值,但注意到这题更新数组不能是当前的状态,而需要是更新前一个不同的数,用temp存当前值以便于下一次赋值。
因为如果更新当前的数,下一次判断相隔的数是否不同的时候,访问的就是覆盖后的数组值,就不对了。
class Solution {public: int removeDuplicates(int A[], int n) { if(n==0)return 0; if(n==1)return 1; int num=1,i,temp=A[1]; for(i=2;i<n;++i) if(A[i]!=A[i-2]) { A[num++]=temp; temp=A[i]; } A[num++]=temp; return num; }};// blog.csdn.net/havenoidea
- leetcode:Remove Duplicates from Sorted Array II (允许重复一次,去掉数组多余数字)【面试算法题】
- leetcode:Remove Duplicates from Sorted Array(去掉数组重复数字,常数空间限制)【面试算法题】
- 101.Remove Duplicates from Sorted Array II-删除排序数组中的重复数字 II(容易题)
- LeetCode--Remove Duplicates from Sorted Array II (数组操作)
- LeetCode: Remove Duplicates from Sorted Array II(在排序数组中删除重复元素)
- 【LeetCode-面试算法经典-Java实现】【026-Remove Duplicates from Sorted Array(删除排序数组中的重复元素)】
- Remove Duplicates from Sorted Array II 允许重复2次数组
- Remove Duplicates from Sorted Array(去掉重复的元素)leetcode
- LintCode Remove Duplicates from Sorted Array II 删除排序数组中的重复数字 II
- leetcode:Remove Duplicates from Sorted List II (留下链表中只出现一次的元素)【面试算法题】
- (每日算法)LeetCode --- Remove Duplicates from Sorted Array II (删除重复元素II)
- leetcode:Remove Duplicates from Sorted Array去掉排序数组中重复的元素
- 【leetcode 移除有序序列重复数字】Remove Duplicates from Sorted Array(List) I(II)
- 100.Remove Duplicates from Sorted Array-删除排序数组中的重复数字(容易题)
- 【leetcode 数组】Remove Duplicates from Sorted Array II
- [leetcode]【数组】80. Remove Duplicates from Sorted Array II
- LeetCode OJ算法题(八十):Remove Duplicates from Sorted Array II
- LeetCode算法题目:Remove Duplicates from Sorted Array II
- 如何实现GridView表格数据限制标题换行
- 详解JavaScript中的this
- (学习)C++
- 英伟达笔试总结
- So cute are you Python 10
- leetcode:Remove Duplicates from Sorted Array II (允许重复一次,去掉数组多余数字)【面试算法题】
- 查看代码发现的问题
- Fedora 18 构建Samba 服务器
- SVNServe一些权限配置
- POJ 3641 Oulipo KMP 水题
- Ext.grid.EditorGridPanel单元格内套用下拉列表,且列表数据为动态加载,支持模糊查询,有描述信息
- 【五子棋AI】一、AI概述 .
- Java中 子、父类加载的顺序问题
- 【PAT】1066. Root of AVL Tree (25)