[LeetCode] 026. Remove Duplicates from Sorted Array (Easy) (C++/Java)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql)

Github: https://github.com/illuz/leetcode


026. Remove Duplicates from Sorted Array (Easy)

链接

题目:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/

代码(github):https://github.com/illuz/leetcode

题意

给一个有序数列,删重复的元素。

分析

如果可以开一个数组来存就非常容易。但是这题不让你用多余的空间。

不过也不难,只要维护一个新的坐标就行了。

用 C++ 的 STL 可以只要一句话:用 unique 实现功能,用 distance 计算大小。

Java 和 Python 的写法都和 C++ 的一样,这里就不写出来了。

代码

C++: (模拟)

class Solution {
public:
    int removeDuplicates(int A[], int n) {
		if (!n)
			return 0;
		int ret = 1;
		for (int i = 1; i < n; i++)
			if (A[i] != A[i - 1])
				A[ret++] = A[i];
		return ret;
	}
};

C++: (STL)

class Solution {
public:
    int removeDuplicates(int A[], int n) {
        return distance(A, unique(A, A + n));
    }
};
时间: 2024-10-27 18:14:27

[LeetCode] 026. Remove Duplicates from Sorted Array (Easy) (C++/Java)的相关文章

LeetCode 026 Remove Duplicates from Sorted Array

题目描述: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 me

Java for 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 nums

Leetcode 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 by modifying the input array in-place with O(1) extra memory. Exam

LeetCode 26 Remove Duplicates from Sorted Array (C,C++,Java,Python)

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 a

【LeetCode】Remove Duplicates from Sorted Array 解题报告

[LeetCode]Remove Duplicates from Sorted Array 解题报告 标签(空格分隔): LeetCode [LeetCode] https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Total Accepted: 129010 Total Submissions: 384622 Difficulty: Easy Question Given a sorted array, remov

LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] &lt;c++&gt;

LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数大于两次,删除多余的复制,返回删除后数组长度,要求不另开内存空间. C++ 献上自己丑陋无比的代码.相当于自己实现一个带计数器的unique函数 class Solution { public: int removeDuplicates(std::vector<int>& nums) {

[LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)

https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C%2B%2B-Java-Python-Ruby 描述 Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1

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

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 n

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