LeetCode(26)题解:Remove Duplicates from Sorted Array

https://leetcode.com/problems/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 nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn‘t matter what you leave beyond the new length.

思路:

也是指针思想,利用新array任意元素一定不会比旧array该元素所在对应位置index大的原理(也可以看作栈),通过两个指针表征新旧array。

遍历一遍array,只有当遇到新元素时,压入新array。

AC代码:

 1 class Solution {
 2 public:
 3     int removeDuplicates(vector<int>& nums) {
 4         if(nums.size()==0)
 5             return 0;
 6         int p=0,n=nums.size();
 7         for(int i=1;i<n;i++){
 8             if (nums[i]!=nums[p]){
 9                 nums[++p]=nums[i];
10             }
11         }
12         return p+1;
13     }
14 };
时间: 2024-10-19 01:10:47

LeetCode(26)题解:Remove Duplicates from Sorted Array的相关文章

Leetcode 线性表 Remove Duplicates from Sorted Array II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Duplicates from Sorted Array II Total Accepted: 10649 Total Submissions: 35325 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorte

Leetcode 线性表 Remove Duplicates from Sorted Array

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Duplicates from Sorted Array Total Accepted: 14789 Total Submissions: 46601 Given a sorted array, remove the duplicates in place such that each element appear only once and return the new l

【26】Remove Duplicates from Sorted Array

[26]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 mem

leetcode笔记:Remove Duplicates from Sorted Array II

一.题目描述 二.解题技巧 这道题和Remove Duplicates from Sorted Array这道题是类似的,只不过这里允许出现重复的数字而已,可以采用二分搜索的变种算法,只不过加入了剔除和第一个元素相同的元素的过程.另一个思路是加入一个变量,用于记录元素出现的次数.这题因为是已经排序的数组,所以一个变量即可解决.如果是没有排序的数组,则需要引入一个hash表来记录出现次数. 三.示例代码 class Solution { public: int RemoveDuplicatesFr

LeetCode(26) 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 n

leetcode第26题--Remove Duplicates from Sorted Array

problem: 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 ar

【LeetCode】026. 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 n

LeetCode OJ:Remove Duplicates from Sorted Array(排好序的vector去重)

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 nums 

LeetCode 80:Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn'