zoukankan      html  css  js  c++  java
  • 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 nums = [1,1,2],

    Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length.

    问题描述:去除有序数组中重复的数据,并返回结果数组的长度。

    只会一种简单的o(n)的方法,这个方法牺牲空间复杂度来换取时间复杂度。

    简述过程:

    1 新构建一个数组,将原数组的数据复制到新数组中。

    2 将原数组当作是一个空数组,将新建数组中的元素逐一插入。

    3 插入时判断相邻插入的两个数据是否相等。若相等,则新建数组下标++,反之,两个数字下标都++

    代码:

     1 int removeDuplicates(int* nums, int numsSize) {
     2     if(numsSize<=1)return numsSize;
     3     int* newNums=(int*)malloc(sizeof(int)*numsSize);
     4     for(int i=0;i<numsSize;i++)newNums[i]=nums[i];
     5     int pre=newNums[0];
     6     int i=1;
     7     int j=1;
     8     while(i<numsSize){
     9         if(newNums[i]!=nums[j-1]){
    10             nums[j]=newNums[i];
    11             j++;
    12         }
    13         i++;
    14     }
    15     free(newNums);
    16     return j;
    17 }
    View Code
  • 相关阅读:
    finereport 文档
    FineRport 工作用
    SQL 排序
    fineReport
    python | Mysql 索引优化分析
    python | 缓存穿透、缓存雪崩和缓存击穿面试题
    python | Jenkins构建Spring Boot 详细步骤
    python | MySQL全面查询的正则匹配
    python | TCP 三次握手和四次挥手图解(有限状态机)
    python | 3款工具实现快速K8S开发
  • 原文地址:https://www.cnblogs.com/lichao-normal/p/6104272.html
Copyright © 2011-2022 走看看