Given a sorted array, remove the duplicates in place such that each element appear onlyonceand 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 A =[1,1,2]
,
Your function should return length =2
, and A is now[1,2]
.
小心令人抓狂的下标!
方法一:
class Solution {
public:
int removeDuplicates(int A[], int n) {
if(n<=1) return n;
int j = 0;
for (int i = 1; i < n; ++i) {
if (A[i] != A[j]) {
A[++j] = A[i];
}
}
return ++j;
}
};
方法二:
class Solution {
public:
int removeDuplicates(int A[], int n) {
if(n<=1) return n;
int dup = 0;
int i = 1;
for (; i < n; i++)
{
if(A[i] == A[i-1]) dup++;
else if(dup != 0)
{
for (int j = i; j < n; j++)
{
A[j-dup] = A[j];
}
n -= dup;
i -= dup;
dup = 0;
}
}
if(dup != 0)
{
A[n-dup-1] = A[n-1];
n -= dup;
dup = 0;
}
return n;
}
};
C++ STL
template <class ForwardIterator>
ForwardIterator unique (ForwardIterator first, ForwardIterator last)
{
if (first==last) return last;
ForwardIterator result = first;
while (++first != last)
{
if (!(*result == *first)) // or: if (!pred(*result,*first)) for version (2)
*(++result)=*first;
}
return ++result;
}
//2014-1-25
class Solution {
public:
int removeDuplicates(int A[], int n)
{
int i = 1, j = 1;
for ( ; j < n; j++)
{
if (A[j] != A[j-1]) A[i++] = A[j];
}
return n? i:0;
}
};
分享到:
相关推荐
LeetCode Remove Duplicates from Sorted Array解决方案
26.Remove_Duplicates_from_Sorted_Array删除有序数组中的重复项【LeetCode单题讲解系列
leetcode LeetCode 这个库用于总结leetcode中遇到的习题 常用数据结构习题总结 1.线性表 解决进度 No. Describition mark 1 Remove Duplicates from Sorted Array 2 Remove Duplicates from Sorted Array II 3 ...
leetcode 答案LeetCode LeetCode in Swift 这个Repo 用来存下我在LeetCode 解题的原始档,包含解题中遇到的错误,也包含解出AC 的答案, 以下的清单将连结到我的Github Pages 中,皆有题目中文翻译与解题的过程。...
Merge Sorted Array 合并 排序 数组 leetcode
Remove Duplicates from Sorted Array iii. Plus One iv. Pascal's Triangle v. Merge Sorted Array vi. Sum vii. Find Minimum in Rotated Sorted Array viii. Largest Rectangle in Histogram ix. Maximal ...
颜色分类leetcode My Leetcode Problems Solutions Using javascript(ES6) 1 Two Sum 两数之和 5 Longest Palindromic Substring 最长回文子串 7 Reverse Integer 整数反转 9 Palindrome Number 回文数 11 Container...
leetcode从重复数组中删除重复项 给定一个已排序的数组nums,就地删除重复项,以使每个元素仅出现一次并返回新的长度。 不要为另一个数组分配额外的空间,必须通过使用O(1)额外的内存就地修改输入数组来做到这...
leetcode算法题主函数如何写 leetcode 每日一题 从今天开始,每天必须完成至少2题。即为保持写代码的手感,也巩固一下编码的基础知识。 2018.1.23 题目:Remove Duplicates from Sorted Array Given a sorted array,...
Leetcode经典01背包 algo 1. 数据结构与算法 数组,链表,(串和序列) 堆,栈,队列 树,图 排序,搜索 贪心,回溯,动态规划 堆:一种完全二叉树,任意节点大于左右孩子(大顶堆) 树:二叉树,DFS,BFS 1 排序与...
leetcode 代码Remove Duplicates from Sorted Array
lru缓存leetcode leetcode 1. Two Sum 2. Add Two Numbers 3. Longest Substring Without Repeating Characters 4. Median of Two Sorted Arrays 5. Longest Palindromic Substring 7. Reverse Integer 9. ...
goMy solution to LeetCode problems using GolangProblems 题库Array 数组NoTitle题名DifficultyStatus11Container With Most Water盛最多水的容器MediumSolved26Remove Duplicates from Sorted Array删除有序数组...
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)...
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 python 001 LeetCode 建立两个个主要资料夹(题目收集、学习内容)+一个玩整的README整理 题目 主要以Python记录于VScode上 先记录头150题 学习 以JupyterNotebook为主 纪录各种资料结构、演算法等 ...
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 ...
O(m+n) time, O(m+n) sapce.*0026 Remove Duplicates from Sorted Array使用双指针,一个快指针,一个慢指针。开始时,两个指针都指向首元素。当两指针元素值相同时,快指针+1;当两指针元素不同时,慢
python python_leetcode面试题解之合并两个有序链接
34.Find_First_and_Last_Position_of_Element_in_Sorted_Array在排序数组中