LeetCode Remove Duplicates from Sorted Array删除整型数组中的重复元素并返回剩下元素个数

 1 class Solution {
 2 public:
 3 int removeDuplicates(int A[], int n) {
 4     int *s=&A[0],*e=&A[0];    //s指向开头第一个,e往后遍历相同的
 5     int t,i,j=n;
 6     for(i=1;i<n;i++){
 7         e++;
 8         if(*s==*e)
 9             j--;
10         else{
11             s++;
12             *s=*e;
13         }
14     }
15     return j;
16 }
17 };

题意:给一个整型有序数组,将其中重复的元素删除,几个相同的元素只留下一个即可,并返回共有多少种不同的元素。

思路:这是数组,所以有重复的地方就要移动后面的元素,用两个指针s和e,e用来遍历所有元素,s用来指向当前已处理过的地方,最终e会遍历到第n个,s会指向新数组的最后一个元素。

注意:没什么好注意的,这么短的代码。指针不要越界。

时间: 2024-08-05 19:10:59

LeetCode Remove Duplicates from Sorted Array删除整型数组中的重复元素并返回剩下元素个数的相关文章

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 [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 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 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 [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

[LeetCode] Remove Duplicates from Sorted Array [19]

题目 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 (删除重复元素II)

Remove Duplicates from Sorted Array II Leetcode 题目: 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