LeetCode(2) 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].

  • C++代码如下

    时间复杂度o(n),空间复杂度o(1):

/*
* Remove Duplicates from Sorted Array
* For example Given input array A= [1,1,2]
* Your function should return length = 2; and A is now [1,2]
*/

#include <iostream>
using namespace std;

class Solution{

public:
    //返回值,函数参数,实现功能
    //指向整形数组的指针,数组和数组个数,实现对数组元素去重
    int * RemoveDuplicates(int *a,int num){

        //思路:按照判断相邻两个元素是否相同,若不相同,后一个元素覆盖前一个元素
        //      同时index 记录新的数组的个数
        int index =0;

        // 从第二个元素开始比较
        for(int i=1;i<num;i++){
            //如果相邻的两个元素不同
            if(a[index]!=a[i]){
                index++;
                a[index]=a[i];
            }

        }

        for(int j=0;j<=index;j++){
            cout<<a[j]<<" ";
        }
        cout<<endl;

        return a;
    }

};

int main(){

    int a[]={1,1,2,2,3};

    Solution s1;
    s1.RemoveDuplicates(a,5);

    getchar();
    getchar();
    return 0;
}
  • Result
1 2 3

概念理解

const 定义理解

  • const 修饰

    关于const修饰指针的情况,一般分为如下四种情况:

int b =500;
const int* a = &b; //情况1
int const *a = &b;//情况2
int * const a=&b; //情况3
const int* const a= &b; //情况4

如何区别呢?

(1)情况1

如果const 位于星号的左侧,则const就是用来修饰指针所指向的变量的,即指针指向为常量。如果const位于星号右侧,const就是修饰指针本身,即指针本身是常量。因此,情况1和情况2相同,都是指针所指向的内容为常量(const放在变量声明符的位置无关),这种情况下不允许对内容进行更改操作。

举一个通俗的例子,如果a是一名仓库管理员的话,他所进入的仓库,里面的货物(*a)是他没有权限允许动的。

const 与#define相比有什么不同?

  • 解答
  • c++语言可以用const定义常量,也可以用#define定义常量,但是前者比后者有更多的优点:

1、const 常量有数据类型,而宏常量没有数据类型。编译器可以对前者进行类型安全检查,而后者只进行字符替换,没有类型安全检查,并且在字符串替换过程中可能会产生意想不到的错误,也就是边际效应。

2、在有些集成化的调试工具可以对const常量进行调试,但是不能对宏常量进行调试。在c++程序中只是用const常量而不使用宏常量,即const常量完全取代宏定义。

常量的引进是在早期的c++版本中,当时标准C规范正在定制。那时,常量被看做一个好的思想而被包含在C中。但是,C中的const的意思是“一个不能被改变的普通变量”。在C中,它总是占用内存,而且它的名字是全局符。C编译器不能把const看出一个编译期间的常量。

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-29 04:29:00

LeetCode(2) Remove Duplicates from Sorted Array的相关文章

LeetCode(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 memory. For example, Given input array n

LeetCode(80)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. 分析 删除链表中重复元素结点. 该题目本质很简单,只需一次遍历.需要注意的是,要释放删除的结点空间. AC代码

LeetCode记录之26——Remove Duplicates from Sorted Array

国外的表达思维跟咱们有很大差别,做这道题的时候很明显.简单说本题就是让你把有序数组中的重复项给换成正常有序的.比如 1 2 2 3换成 1 2 3 3,根本不需要考虑重复的怎么办,怎么删除重复项等等.拿起键盘干就行了.然后返回有序项的下标就可以. Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not

leetcode第26题--Remove Duplicates from Sorted Array

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 ar

LeetCode(33)Search in Rotated Sorted Array

题目 Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no dup

【leetcode 移除有序序列重复数字】Remove Duplicates from Sorted Array(List) I(II)

leetcode上有四道关于移除有序序列中重复数字的题目,其中两道为数组结构,两道为链表结构,分别为: (1)Remove Duplicates from sorted array I:移除一个有序数组中的重复数字,并且返回新数组的大小. (2)Remove Duplicates from sorted array II:移除一个有序数组中的重复数字,并且返回新数组的大小,和上道题目不同的是每个数字可以最多重复两次出现. (3)Remove Duplicates from sorted list

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]. 说明: 1)设个标志可实现 实现: 1 class Solution { 2 public

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(两个指针)

Remove Duplicates from Sorted Array II Total Accepted: 38480 Total Submissions: 125235My Submissions Question Solution Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1,1,2,2,