[Leetcode] Remove duplicates from sorted array 从已排序的数组中删除重复元素

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 A =[1,1,2],

Your function should return length =2, and A is now[1,2].

题意:除去重复的元素,若有重复只保留一个,返回长度。

思路:遍历数组,用不同的,覆盖相同的。过程:保存第一个元素的下标lo。遇到相等不管,继续遍历,遇到不等的,则将该值赋给lo的下一个,反复这样。代码如下:

 1 class Solution
 2 {
 3 public:
 4     int removeDuplicates(int A[],int n)
 5     {
 6         if(n<=0)    return 0;
 7         int lo=0;
 8         for(int i=1;i<=n-1;++i)
 9         {
10             if(A[lo] !=A[i])
11                 A[++lo]=A[i];
12         }
13         return lo+1;
14     }
15 };

思路相同,另一种写法:

 1 class Solution {
 2 public:
 3     int removeDuplicates(int A[], int n)
 4     {
 5         if(n<2) return n;
 6         int count=1;
 7         int flag=0;
 8         for(int i=0;i<n;++i)
 9         {
10             if(A[flag] !=A[i])
11             {
12                 A[++flag]=A[i];
13                 count++;
14             }
15         }
16         return count;
17     }
18 };
时间: 2024-12-15 21:38:33

[Leetcode] Remove duplicates from sorted array 从已排序的数组中删除重复元素的相关文章

[Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素

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]. 题意:可以保留一个重复的元素. 思路:第一种是和Remove duplicates from sorte

leetcode 题解:Remove Duplicates from Sorted Array(已排序数组去重)

题目: 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 A

LeetCode 26 Remove Duplicates from Sorted Array (移除有序数组中重复数字)

题目链接: https://leetcode.com/problems/remove-duplicates-from-sorted-array/?tab=Description 从有序数组中移除重复数字,并且返回不重复数字的个数 遍历操作: 可以使用新的for循环 for (int n : nums){} 每次进行对比,并且更新第一个遇到不相等的元素的下标为i 对数组进行重新赋值操作 当数组长度大于1时,ans初值为1,当数组长度为0时,返回0 参考代码 : package leetcode_5

Leetcode | Remove Duplicates from Sorted Array I &amp;&amp; II

Remove Duplicates from Sorted Array I 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 memor

LeetCode:Remove Duplicates from Sorted Array &amp;&amp; Remove Element

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 A =

LeetCode——Remove Duplicates from Sorted Array

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 A = 

[LeetCode] Remove Duplicates from Sorted Array II [27]

题目 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]. 原题链接(点我) 解题思路 移除数组中重复次数超过2次以上出现的数,但是可以允许重复2次

LeetCode: Remove Duplicates from Sorted Array II [080]

[题目] 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]. [题意] 给定一个有序数组,给数组去重,和Remove Duplicates fro

LeetCode: Remove Duplicates from Sorted Array [025]

[题目] 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