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].

说明:

1)无

实现:

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

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

时间: 2024-08-11 09:56:34

leetcode 题解:Remove Duplicates from Sorted Array(已排序数组去重)的相关文章

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

[LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, 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

[LeetCode]80. Remove Duplicates from Sorted Array II删除数组中的重复值

和第一题不同的地方是,容忍两次重复 虽然题目上说只需要长度,但是否检测的时候如果数组不跟着改变也是不行的 没说清楚题意 自己是用双指针做的,看了大神的答案更简单 public int removeDuplicates(int[] nums) { int i = 0; for (int n : nums) if (i < 2 || n > nums[i-2]) nums[i++] = n; return i; } 原文地址:https://www.cnblogs.com/stAr-1/p/843

[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 题解]: Remove Duplicates from Sorted List

Given a sorted linked list, delete all duplicates such that each element appear only once. For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3. 题意:给定一个已排序的链表,删除其中重复的元素,保证每个元素有且仅有一个. 使用两个指针,分别指向前一个元素,和当前元素,

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

【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】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

[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