leetcode_1线性表_1数组_1&2Remove Duplicates from Sorted Array I & II

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

分析:

时间复杂度 O(n),空间复杂度 O(1),因为是排好序的。后一个和前一个比较,相同则不要(跳过),不同则放。一个的变量不断加,一直到数组长度不断取数,一个变量指向欲存数组的地址量。

以下是C++和PYTHON实现的代码:

 1 class Solution {
 2 public:
 3     int removeDuplicates(int A[], int n) {
 4         if(n == 0)  return 0;
 5
 6         int p = 0;
 7         for(int i = 0; i < n; i++)
 8         {
 9             if(A[p] != A[i])
10             {
11                 A[++p] = A[i];
12
13             }
14         }
15         return p + 1;
16     }
17 };
 1 class Solution:
 2     # @param a list of integers
 3     # @return an integer
 4     def removeDuplicates(self, A):
 5         if len(A) == 0:
 6             return 0
 7
 8         p = 0
 9         for i in A:
10             if A[p] != i:
11                 p += 1
12                 A[p] = i
13         return p + 1

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

分析:

因为排好序的,类似上体,只要可以允许连续两个相同元素存在,所以将待比的与已存好的倒数第二个去比。如果是3,或n,以此类推。

以下是JAVE和PYTHON的代码:

 1 public class Solution {
 2     public int removeDuplicates(int[] A) {
 3         if (A.length <= 2)    return A.length;
 4
 5         int p = 2;
 6         for (int i=2; i < A.length; i++)
 7             if (A[i] != A[p -2])
 8                 A[p++] = A[i];
 9         return p;
10     }
11 }
 1 class Solution:
 2     # @param A a list of integers
 3     # @return an integer
 4     def removeDuplicates(self, A):
 5         if (len(A) <= 2):   return len(A)
 6
 7         p = 2
 8         for i in range(2,len(A)):
 9             if (A[i] != A[p - 2]):
10                 A[p] = A[i]
11                 p += 1
12         return p

小结:

JAVE中,A.length 后没有()

注意 p++和++p的区别

注意初始条件

PYTHON中没有++,——,得 p += 1之类的用

时间: 2024-10-11 21:31:29

leetcode_1线性表_1数组_1&2Remove Duplicates from Sorted Array I & II的相关文章

有序数组去重2--Remove Duplicates from Sorted Array II

https://leetcode.com/problems/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], You

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

第14&amp;15题 Remove Duplicates from Sorted Array I&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

Remove Duplicates from Sorted Array I&amp;&amp;II

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 = [

怎样防超时——Remove Duplicates from Sorted Array I&amp;&amp;II

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 线性表 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

线性表之数组描述

1.线性表的描述可以分为两种 数组描述:StaticList和DynamicList(动态数组) 链式描述 2.数组描述 数组描述方法将元素存储在一个数组中,通过一个索引以确定每个元素存储的位置 所有元素依次存储在一片连续的存储空间中 线性表中的每一个元素对应数组中的一个位置 3.创建一个数组类 要创建一个数组类必须确定数组类型和确定数组长度,所以针对以上两个问题有以下解决: 模板类 动态数组(数组空间不够的情况下动态增加数组长度) 4. StaticList类实现 使用原生数组作为顺序存储空间

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