leetcode 4. 移除有序数组中的重复元素 Remove Duplicates from Sorted Array

问题:Remove Duplicates from Sorted Array II 难度:medium

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

解答:

两种思路:

(1)两个参考指针,逐步后移,当第3个与前2个相等时,跳过

(2)一个参考指针,用第3个和第1个做比较,相等时,跳过;不等赋值

显然第(1)容易想到(直观),但是第(2)比较高效,而且扩展性比较强(如果要求最多出现3次,只需要比较A[i]和A[index-3])。

因此在构思时应该注意转换成等价问题,有序时,比较和前n个是否相等,只需要比较当前是否和第(i-n)个相等。

class Solution {
public:
        //比较1次,赋值1次
        int removeDuplicates(int A[], int n) {
            if (n <= 2)
                return n;
            int index = 2;
            for (int i = 2; i < n; i++) {
                if (A[i] == A[index-2])
                    continue;
                A[index++] = A[i];
            }
            return index;
        }

        //比较2次,赋值2次
        int removeDuplicates2(int A[], int n) {
            if (n <= 2)
                return n;
            int j = 0;
            int k = j+1;
            A[j] = A[0];
            A[k] = A[1];
            for (int i = 2; i < n; i++) {
                if (A[i] == A[k] && A[j] == A[k])
                    continue;
                A[++j] = A[k];
                A[++k] = A[i];
            }
            return k+1;
        }
    };

};
时间: 2024-10-29 15:30:23

leetcode 4. 移除有序数组中的重复元素 Remove Duplicates from Sorted Array的相关文章

2. 从有序链表和数组中移出重复元素 Remove Duplicates

1. 链表 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 83. 删除排序链表中的重复元素(Remove Duplicates from Sorted List)

目录 题目描述: 示例 1: 示例 2: 解法: 题目描述: 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2 输出: 1->2 示例 2: 输入: 1->1->2->3->3 输出: 1->2->3 解法: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNo

LeetCode 第540题 有序数组中的单一元素

/*给定一个只包含整数的有序数组,每个元素都会出现两次,唯有一个数只会出现一次,找出这个数.[0,1,1,2,2,5,5] 示例 1: 输入: [1,1,2,3,3,4,4,8,8] 输出: 2 示例 2: 输入: [3,3,7,7,10,11,11] 输出: 10 注意: 您的方案应该在 O(log n)时间复杂度和 O(1)空间复杂度中运行.*/ /* 思路: 用二分法.mid 为偶数: mid和哪边元素相同,单个的数就在哪边.mid 为奇数: mid和哪边元素不同,单个的数就在哪边.为了简

LeetCode 26 Remove Duplicates from Sorted Array (移除有序数组中重复数字)

题目链接: https://leetcode.com/problems/remove-duplicates-from-sorted-array/?tab=Description 从有序数组中移除重复数字,并且返回不重复数字的个数 遍历操作: 可以使用新的for循环 for (int n : nums){} 每次进行对比,并且更新第一个遇到不相等的元素的下标为i 对数组进行重新赋值操作 当数组长度大于1时,ans初值为1,当数组长度为0时,返回0 参考代码 : package leetcode_5

[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从零单排】No26.Remove Duplicates from Sorted Array

题目 题目要求:去除sort int数组中的重复项. 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 exam

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

[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 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]. 这道题是之前那道Remove Duplicates from Sorted Array 有序数组中