【数组OJ题】

删除排序数组中的重复项
题目来源

给你一个 升序排列 的数组 nums ,请你 原地 删除重复出现的元素,使每个元素 只出现一次 ,返回删除后数组的新长度。元素的 相对顺序 应该保持 一致 。然后返回 nums 中唯一元素的个数。

c 复制代码
int removeDuplicates(int* nums, int numsSize) {
    //使用三个指针
    // [0,0,1,1,1,2,2,3,3,4]
    //  0 1 2 3 4 5 6 7 8 9
    // 5
    int cur = 0;
    int ret = 1;
    int begin = 0;
    int n = numsSize;

    while (ret < n) {
        if (nums[cur] == nums[ret]) {
            ret++;
        }
        else {
            nums[begin] = nums[cur];
            begin++;
            cur = ret;
            ret++;
        }
    }
    nums[begin] = nums[cur];
    begin++;
    return begin;
}
c 复制代码
int removeDuplicates(int* nums, int numsSize) {
    //三指针
    int cur = 0;
    int tail = 1;
    int temp = 0;

    // [0,0,1,1,1,2,2,3,3,4]
    while (tail < numsSize)
    {
        if (nums[cur] == nums[tail]) {
            tail++;
        }
        else {
            nums[temp] = nums[cur];
            temp++;
            //temp每次存储完成之后都会加一
            //0之后变为1、1之后变为2...
            //所以说temp就是数据元素个数
            cur = tail;
            tail++;
        }
    }

    //还需要把最后一个数据加上
    nums[temp] = nums[cur];
    //
    temp++;

    return temp;
}

合并两个有序数组
题目来源

给你两个按 非递减顺序 排列的整数数组 nums1 和 nums2,另有两个整数 m 和 n ,分别表示 nums1 和 nums2 中的元素数目。

c 复制代码
void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n){
    //使用m和n
    // nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
    // [1,2,2,3,5,6]
    int end1 = m-1;
    int end2 = n-1;
    int ends = m+n-1;
    while((end1>=0)&&(end2>=0)){
        if(nums1[end1]>=nums2[end2]){
            nums1[ends] = nums1[end1];
            end1--;
            ends--;
        }else{
            nums1[ends] = nums2[end2];
            end2--;
            ends--;
        }
    }
    //end1 和 end2
    //if end2先走完了 啥也不用变
    //if end1先走完了 需要将end2剩余元素放到nums1中
    while(end2>=0){
        nums1[ends] = nums2[end2];
        end2--;
        ends--;
    }
}
c 复制代码
void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n) {
    int end1 = m - 1;
    int end2 = n - 1;
    int end = m + n - 1;
    while (end1 >= 0 && end2 >= 0)
    {
        if (nums2[end2] >= nums1[end1]) {
            nums1[end] = nums2[end2];
            end2--;
            end--;
        }
        else {
            nums1[end] = nums1[end1];
            end1--;
            end--;
        }
    }
    while (end2 >= 0)
    {
        nums1[end] = nums2[end2];
        end2--;
        end--;
    }
}
相关推荐
枕星而眠8 分钟前
【数据结构】树与二叉树基础知识点总结
数据结构·c++·后端·算法·运维开发
fei_sun1 小时前
【SystemVerilog验证】数据类型(待补充)
数据结构·systemverilog
无忧.芙桃1 小时前
数据结构之单链表
c语言·开发语言·数据结构
HZ·湘怡1 小时前
二叉树 1
数据结构·算法·二叉树·
雨落在了我的手上1 小时前
Java数据结构(二):时间和空间复杂度
数据结构
悠仁さん1 小时前
list 链式表基本功能模拟实现(双向有头指针循环链表)
c语言·数据结构·链表·list
xxwxx__2 小时前
栈(Stack)详解:概念、实现与避坑指南
c语言·数据结构
少司府2 小时前
C++进阶:map和set的使用
开发语言·数据结构·c++·容器·stl·set·map
cpp_25012 小时前
P11375 [GESP202412 六级] 树上游走
数据结构·c++·算法·题解·洛谷·树形结构·gesp六级
川冰ICE2 小时前
JavaScript进阶③|Map_Set_WeakMap_WeakSet,新型数据结构
开发语言·javascript·数据结构