《leetCode》:Remove Duplicates from Sorted Array

2024-05-14 04:58

本文主要是介绍《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 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.

题目大意为:不开辟额外的空间,将一个已经排序过的数组中的重复数字去除掉。


#include<stdio.h>
#include<stdlib.h>
#include<string.h>/*
思路:这个不开辟额外的空间。
将数组中含有1次以上的数字全部去除掉 
*/
int removeDuplicates(int* nums, int numsSize) {if(nums==NULL||numsSize<1){return 0;}if(numsSize==1){return numsSize;}int index1=0;int index2=1;int count=1;for(int i=1;i<numsSize;i++){if(nums[index1]==nums[i])continue;//不相等就要进行拷贝if(i-index2!=0){//中间有Duplicate需要覆盖 nums[index2]=nums[i];index1=i;index2++;                                       }else{//不需要拷贝,但是需要更新,其实这里的代码可以与上面的代码提取出来并合并,但是这样写逻辑更容易理解一点。index1=i;index2++; }}
//  for(int i=0;i<index2;i++){
//      printf("%d   ",nums[i]);
//  } return index2; }int main(void){int k;while(scanf("%d",&k)!=EOF&&k>0){int *arr=(int *)malloc(k*sizeof(int));if(arr==NULL){exit(EXIT_FAILURE);}for(int i=0;i<k;i++){scanf("%d",arr+i);}int len=removeDuplicates(arr,k);//printf("%d\n",len);}
}

此题比较简单,AC结果如下

从结果可以看出,上面这种方法的效率并不高,因此需要寻找更高效的方法。

出于好奇,将如下代码的公共代码进行了提取

    if(i-index2!=0){//中间有Duplicate需要覆盖 nums[index2]=nums[i];index1=i;index2++;                                       }else{//不需要拷贝,但是需要更新,其实这里的代码可以与上面的代码提取出来并合并,但是这样写逻辑更容易理解一点。index1=i;index2++; }

代码如下

        if(i-index2!=0){//中间有Duplicate需要覆盖 nums[index2]=nums[i];}index1=i;index2++; 

结果吓我一跳,时间居然少了很多(大概4ms),分析了下原因,虽然在执行代码上面并没有多大的改变,原因可能在于在编译阶段花了一定的时间。AC结果如下

网上的代码

看了下网上的代码,发现自己是多么的渣,别人的代码是多么的简单,代码如下:


int removeDuplicates(int* nums, int numsSize) {if(nums==NULL||numsSize<1){return 0;}int index=1;//用于指向即将要覆盖的位置。 for(int i=1;i<numsSize;i++){if(nums[i]!=nums[i-1]){nums[index]=nums[i];index++;}}return index; }

上面的代码在时间上没有改进,Runtime: 12 ms

这篇关于《leetCode》:Remove Duplicates from Sorted Array的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/987807

相关文章

一文详解Java Stream的sorted自定义排序

《一文详解JavaStream的sorted自定义排序》Javastream中的sorted方法是用于对流中的元素进行排序的方法,它可以接受一个comparator参数,用于指定排序规则,sorte... 目录一、sorted 操作的基础原理二、自定义排序的实现方式1. Comparator 接口的 Lam

使用nohup和--remove-source-files在后台运行rsync并记录日志方式

《使用nohup和--remove-source-files在后台运行rsync并记录日志方式》:本文主要介绍使用nohup和--remove-source-files在后台运行rsync并记录日... 目录一、什么是 --remove-source-files?二、示例命令三、命令详解1. nohup2.

JavaScript Array.from及其相关用法详解(示例演示)

《JavaScriptArray.from及其相关用法详解(示例演示)》Array.from方法是ES6引入的一个静态方法,用于从类数组对象或可迭代对象创建一个新的数组实例,本文将详细介绍Array... 目录一、Array.from 方法概述1. 方法介绍2. 示例演示二、结合实际场景的使用1. 初始化二

哈希leetcode-1

目录 1前言 2.例题  2.1两数之和 2.2判断是否互为字符重排 2.3存在重复元素1 2.4存在重复元素2 2.5字母异位词分组 1前言 哈希表主要是适合于快速查找某个元素(O(1)) 当我们要频繁的查找某个元素,第一哈希表O(1),第二,二分O(log n) 一般可以分为语言自带的容器哈希和用数组模拟的简易哈希。 最简单的比如数组模拟字符存储,只要开26个c

leetcode-24Swap Nodes in Pairs

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode swapPairs(L

leetcode-23Merge k Sorted Lists

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode mergeKLists

C++ | Leetcode C++题解之第393题UTF-8编码验证

题目: 题解: class Solution {public:static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num &

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟)

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟) 题目描述 给定一个链表,链表中的每个节点代表一个整数。链表中的整数由 0 分隔开,表示不同的区间。链表的开始和结束节点的值都为 0。任务是将每两个相邻的 0 之间的所有节点合并成一个节点,新节点的值为原区间内所有节点值的和。合并后,需要移除所有的 0,并返回修改后的链表头节点。 思路分析 初始化:创建一个虚拟头节点

C语言 | Leetcode C语言题解之第393题UTF-8编码验证

题目: 题解: static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num & MASK1) == 0) {return

【JavaScript】LeetCode:16-20

文章目录 16 无重复字符的最长字串17 找到字符串中所有字母异位词18 和为K的子数组19 滑动窗口最大值20 最小覆盖字串 16 无重复字符的最长字串 滑动窗口 + 哈希表这里用哈希集合Set()实现。左指针i,右指针j,从头遍历数组,若j指针指向的元素不在set中,则加入该元素,否则更新结果res,删除集合中i指针指向的元素,进入下一轮循环。 /*** @param