26. Remove Duplicates from Sorted Array【easy】

26. Remove Duplicates from Sorted Array【easy】

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.

解法一:

 1 class Solution {
 2 public:
 3     int removeDuplicates(vector<int>& nums) {
 4         if (nums.empty() || nums.size() == 1)
 5         {
 6             return nums.size();
 7         }
 8
 9         int i = 0;
10         int j = 0;
11         nums[j++] = nums[i++];
12
13         while (i < nums.size() && j < nums.size())
14         {
15             if (nums[i] != nums[i - 1])
16             {
17                 nums[j++] = nums[i++];
18             }
19             else
20             {
21                 ++i;
22             }
23         }
24
25         return j;
26     }
27 };

思路:双指针,注意边界条件的判断

解法二:

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

可读性一般

解法三:

 1 class Solution {
 2 public:
 3     int removeDuplicates(vector<int>& nums) {
 4         int count = 0;
 5         for(int i = 1; i < nums.size(); i++){
 6             if(nums[i] == nums[i-1]) count++;
 7             else nums[i-count] = nums[i];
 8         }
 9         return nums.size()-count;
10     }
11 };
时间: 2024-10-10 08:28:07

26. Remove Duplicates from Sorted Array【easy】的相关文章

26. Remove Duplicates from Sorted Array【leetcode】,数组,array,java,算法

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

Leet Code OJ 26. Remove Duplicates from Sorted Array [Difficulty: Easy]

题目: 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:26. Remove Duplicates from Sorted Array(Easy)

1. 原题链接 https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/ 2. 题目要求 给定一个已经排序的整数数组nums[ ],返回除去重复元素后的数组长度 注意:不能重新创建一个数组,空间复杂度为O(1) 3. 解题思路 使用指针j来遍历数组,i用来计数.初始时,i指向nums[0],j指向nums[1]. 当nums[i] != nums[j]时,i++,且nums[i]=nums[j],

&lt;LeetCode OJ&gt; 26. Remove Duplicates from Sorted Array

26. Remove Duplicates from Sorted Array My Submissions Question Total Accepted: 104150 Total Submissions: 322188 Difficulty: Easy Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.

26. Remove Duplicates from Sorted Array &amp;&amp; 80. Remove Duplicates from Sorted Array II

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记录之26——Remove Duplicates from Sorted Array

国外的表达思维跟咱们有很大差别,做这道题的时候很明显.简单说本题就是让你把有序数组中的重复项给换成正常有序的.比如 1 2 2 3换成 1 2 3 3,根本不需要考虑重复的怎么办,怎么删除重复项等等.拿起键盘干就行了.然后返回有序项的下标就可以. Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not

leetCode 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 26. Remove Duplicates from Sorted Array 、80. Remove Duplicates from Sorted Array II

两个题类似,第一个题是不允许有重复的数字,第二个题是允许每个数字最多重复两个,两个题目都要求在原数组上进行操作,并返回生成数组的长度,即空间复杂度为O(1). 两个题都是使用双指针,第一个指针指向生成新的数组的最后一个位置,第二个指针指向当前进行判断的位置. 唯一不同的是, 第二个题需要设置一个变量来控制重复的个数是否超过2 26. Remove Duplicates from Sorted Array class Solution { public: int removeDuplicates(

[Leetcode][Python]26: Remove Duplicates from Sorted Array

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 26: Remove Duplicates from Sorted Arrayhttps://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove the duplicates in place such that each element appear