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]
.
public int removeDuplicates(int[] A) { if(A==null || A.length==0) return 0; int size = 1; //remove后数组的长度,更重要的是它也是下一个元素应该移动的位置 int dupCount = 1; //前面数字出现的次数 for(int i=1; i<A.length; i++) { if(A[i] != A[i-1]) { //如果不相等,说明该数第一次出现,移动,并将dupCount重置为1 A[size++] = A[i]; //并将下个可以放置元素的位置加1 dupCount = 1; } else if(dupCount < 2){ //如果出现过一次,也满足要求,继续移动 A[size++] = A[i]; //并将下个可以放置元素的位置加1 dupCount++; } else { //dupCount++; //可要可不要 } } return size; }
相关推荐
LeetCode Remove Duplicates from Sorted Array解决方案
26.Remove_Duplicates_from_Sorted_Array删除有序数组中的重复项【LeetCode单题讲解系列
leetcode从重复数组中删除重复项 给定一个已排序的数组nums,就地删除重复项,以使每个元素仅出现一次并返回新的长度。 不要为另一个数组分配额外的空间,必须通过使用O(1)额外的内存就地修改输入数组来做到这...
remove-duplicates-from-sorted-array 移除元素 remove-element 搜索插入位置 search-insert-position 最大子序和 maximum-subarray 加一 plus-one 合并两个有序数组 merge-sorted-array 杨辉三角 pascals-triangle ...
颜色分类leetcode My Leetcode Problems Solutions Using javascript(ES6) 1 Two Sum 两数之和 5 Longest Palindromic Substring 最长回文子串 7 Reverse Integer 整数反转 9 Palindrome Number 回文数 11 Container...
26.Remove Duplicates from Sorted Array 53.Maximum Subarray 70.Climbing Stairs 121.Best Time to Buy and Sell Stock 122.Best Time to Buy and Sell Stock II 123.Best Time to Buy and Sell Stock III 141....
leetcode-goMy solution to LeetCode problems using GolangProblems 题库Array 数组NoTitle题名DifficultyStatus11Container With Most Water盛最多水的容器MediumSolved26Remove Duplicates from Sorted Array删除...
leetcode 浇花力扣解决方案 简单的 #0001 - Two Sum #0007 - Reverse Integer #0009 - Palindrome Number #0035 - Search Insert Position #0058 - Length of Last Word #0066 - Plus One #0083 - Remove Duplicates...
83.删除排序链表中的重复元素 (Remove Duplicates from Sorted List) 88.合并两个有序数组 (Merge Sorted Array) 100.相同的树 (Same Tree) 104.二叉树的最大深度 (Maximum Depth of Binary Tree) 118.杨辉三角 ...
leetcode LeetCode 这个库用于总结leetcode中遇到的习题 常用数据结构习题总结 1.线性表 解决进度 No. Describition mark 1 Remove Duplicates from Sorted Array 2 Remove Duplicates from Sorted Array II 3 ...
com.leetcode.array Search a 2D Matrix Spiral Matrix com.leetcode.list Linked List Cycle Linked List Cycle II Remove Duplicates from Sorted List com.leetcode.string Single Number com.leetcode.tree ...
O(m+n) time, O(m+n) sapce.*0026 Remove Duplicates from Sorted Array使用双指针,一个快指针,一个慢指针。开始时,两个指针都指向首元素。当两指针元素值相同时,快指针+1;当两指针元素不同时,慢
leetcode 答案LeetCode LeetCode in Swift 这个Repo 用来存下我在LeetCode 解题的原始档,包含解题中遇到的错误,也包含解出AC 的答案, 以下的清单将连结到我的Github Pages 中,皆有题目中文翻译与解题的过程。...
java lru leetcode :ice_cream: LeetCode Kindem 的个人 LeetCode ...Duplicates from Sorted Array 48 Rotate Image 53 Maximum Subarray 55 Jump Game 56 Merge Intervals 64 Minimum Path Sum 73
leetcode 1004 leetcode E:简单,M:中等,H:困难 数组和字符串 217. Contains Duplicate (E) 48. Rotate Image (M) -> 2 73. Set Matrix Zeroes (M) 1. Two Sum (E) 167. Two Sum II - Input array is sorted (E)...
leetcode 苹果 OnlineJudge Code for Online Judge. LeetCode # Title Solution Difficulty 1 Two Sum Easy 26-1 Remove Duplicates from Sorted Array Easy 26-2 Remove Duplicates from Unsorted Array Easy 27 ...
leetcode写题闪退 #*的多少代表此题的有意思程度 有几题第一次写的时候思绪比较混乱: *****Regular Expression Matching 2014.10.29 对于Find Minimum in Rotated Sorted Array II 和 Find Minimum in Rotated ...
26 | [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/)| [C++](./C++/remove-duplicates-from-sorted-array.cpp) [Python](./Python/remove-duplicates...
Leetcode经典01背包 algo 1. 数据结构与算法 数组,链表,(串和序列) 堆,栈,队列 树,图 排序,搜索 贪心,回溯,动态规划 堆:一种完全二叉树,任意节点大于左右孩子(大顶堆) 树:二叉树,DFS,BFS 1 排序与...
leetcode 代码Remove Duplicates from Sorted Array