leetcode_26_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 memory.

For example,

Given input array A = [1,1,2],

Your function should return length = 2, and A is now [1,2].

//vs2012测试代码
#include<iostream>

using namespace std;

#define n 5

int main()
{
	int A[n];
	for(int i=0; i<n; i++)
		cin>>A[i];

	//要考虑空数组的情况
	if(n==0)
		return 0;

	int start=0;
	for(int i=0; i<n-1; i++)
	{
		if( A[i]!=A[i+1])
		{
			A[start] = A[i];
			start++;
		}
	}
	A[start] = A[n-1];
	start++;

	//for(int j=start; j<n; j++)
	//	A[j]=0;

	cout<<start<<endl;
	for(int j=0; j<start; j++)
		cout<<A[j];

	return start;
}
//方法一:自测accepted
class Solution {
public:
    int removeDuplicates(int A[], int n) {
	//要考虑空数组的情况
	if(n==0)
		return 0;

	int start=0;
	for(int i=0; i<n-1; i++)
	{
		if( A[i]!=A[i+1])
		{
			A[start] = A[i];
			start++;
		}
	}
	A[start] = A[n-1];
	start++;

	//for(int j=start; j<n; j++)
	//	A[j]=0;

	cout<<start<<endl;
	for(int j=0; j<start; j++)
		cout<<A[j];

	return start;
    }
};
//方法二:参考其他
class Solution {
public:
	int removeDuplicates(int A[], int n) {
		// Start typing your C/C++ solution below
		// DO NOT write int main() function
		if(0 == n) return 0;
		int len = 1;
		for (int i = 1; i < n; ++i)
		{
			if(A[i] != A[len-1])
				A[len++] = A[i];
		}
		return len;
	}
};

时间: 2024-12-25 04:03:21

leetcode_26_Remove Duplicates from Sorted Array的相关文章

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'

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

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 consta

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

leetCode 26.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 memory.

【leetcode刷题笔记】Remove Duplicates from Sorted Array II

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]. 题解: 设置两个变量:右边kepler和前向游标forward.如果当前kepeler所指的元素和

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

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

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