[LeetCode]合并两个有序数组

  •  个人主页:北·海
  •  🎐CSDN新晋作者
  •  🎉欢迎 👍点赞✍评论⭐收藏
  • ✨收录专栏:C/C++
  • 🤝希望作者的文章能对你有所帮助,有不足的地方请在评论区留言指正,大家一起学习交流!🤗

 

题目

 


解法 1 : 直接合并加排序

  • 需要用到头文件 algorithm里面的sort函数进行排序
  • 合并:
    • 利用循环或者双指针
  • 由于是非降序序列,则可以设计为升序序列,sort函数默认为升序序列
  • nums1中有m个非零整数,m+n为nums1的大小, nums2中有n个整数,大小也为n,在循环中对nums1从第一个非零整数开始,也就是下标为m,终止条件为循环n次,这样才能将nums2中的所有元素都copy到nums1的非零整数区域,在利用sort进行排序,默认为升序
//省略头文件
class Solution {
public:
    void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {

        //利用循环合并
        int k;
        for(int i = m,k = 0 ;i<m+n && k <n;i++,k++){
            nums1[i] = nums2[k];
        }

        //默认为升序
        sort(nums1.begin(),nums1.end());

    }
};

sort函数的用法(以数组为例)

  • 使用的头文件 #include <aligorithm>
  • 三个参数 sort(begin,end,cmp)
    • begin : 要排序数组的起始地址(第一个数据的地址)
    • end    : 最后一个数据的下一个地址
    • cmp   : 若这个参数不写,则默认为升序
    • cmp函数示例
    • bool cmp (int a,int b){
            return a <b;
       }
    • 若return a < b; 则为升序

#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
bool cmp(int a,int b){
    return a>b;
}
int main(){
  int c[] = {3,45,2,34,45,56,32,23};
    sort(c,c+(sizeof(c)/sizeof(c[0])),cmp);
    for(int i = 0 ;i<sizeof(c)/sizeof(c[0]);i++){
    cout<<c[i]<<" ";
    }

输出结果 : 56 45 45 34 32 23 3 2  

  • 若return a> b;  则为降序

#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
bool cmp(int a,int b){
    return a<b;
}
int main(){
  int c[] = {3,45,2,34,45,56,32,23};
    sort(c,c+(sizeof(c)/sizeof(c[0])),cmp);
    for(int i = 0 ;i<sizeof(c)/sizeof(c[0]);i++){
    cout<<c[i]<<" ";
    }

输出结果 : 2 3 23 32 34 45 45 56 

  • 其实排序完全可以不需要自己写cmp函数就可以实现
    • 升序 : sort (begin,end,less<数据类型>());
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    int c[] = {3,45,2,34,45,56,32,23};
    sort(c,c+(sizeof(c)/sizeof(c[0])),less<int>());
    for(int i = 0 ;i<sizeof(c)/sizeof(c[0]);i++){
        cout<<c[i]<<" ";
    }
}

 输出结果 : 2 3 23 32 34 45 45 56 

  • 降序 : sort(begin,end,greater<数据类型>());
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    int c[] = {3,45,2,34,45,56,32,23};
    sort(c,c+(sizeof(c)/sizeof(c[0])),greater<int>());
    for(int i = 0 ;i<sizeof(c)/sizeof(c[0]);i++){
        cout<<c[i]<<" ";
    }
}

输出结果 : 56 45 45 34 32 23 3 2  

  • sort也可以完成字符的排序(利用迭代器)
    • 和数组排序一样,也可也使用cmp函数和less<char>()与greater<char>(),在使用cmp函数时传入的a,b类型为char类型
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
  string a = "dskfjeaec";
    sort(a.begin(),a.end());
    cout<<a<<endl;
}
  • sort函数也可以完成字符串的比较(使用vector的迭代器)
    • sort(a.begin(),a.end)//也可也使用cmp函数和less<char>()与greater<char>(),在使用cmp函数时传入的a,b类型为string类型
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{

    vector<string> a = {
        "qwer",
        "fgtr",
        "kfkg"
    };

    sort(a.begin(),a.end());

    for (int i = 0; i < 3; i++) {
    cout<<a[i]<<endl;
    }

}
  • 解法 2 :双指针
    • 因为最终的数据要存入nums1,所以先将nums1复制一份为nums1_copy,定义三个变量p,p1,p2分别指向三个数组的首元素,利用三目运算符,将比较小的那个存放在nums1中,然后将其下标加一,在进行循环,直到某个数组的元素循环玩,在判断,将未循环完的那个数组剩下的元素添加至nums1的末尾
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
//双指针 - 快慢指针
    vector<int> nums1{3,4,56,2,0,0,0},nums2{9,5,7};
    int m=4,n=3;
    //先将nums1复制一份
    vector<int> nums1_copy(nums1.begin(),nums1.end());
    //将p1指向nums1_copy的首元素,将p2指向nums2的首元素,p指向nums1
    int p1 = 0 ,p2 =  0,p=0;
    while((p1<m) && (p2 <n)){
        nums1[p++] = (nums1_copy[p1]<nums2[p2]) ?nums1_copy[p1++]:nums2[p2++];
    }
//将剩下的元素添加至末尾
    while (p1 < m) {
        nums1[p++] = nums1_copy[p1++];
    }
    while (p2 < n) {
        nums1[p++] = nums2[p2++];
    }
//输出
    for(int i = 0 ;i<n+m;i++){
        cout<<nums1[i]<<endl;
    }

}

  • 1.复制vector容器内的元素,vector<int> nums1_copy(nums1.begin(),nums1.end());
  • nums1_copy为新的容器,nums1为要复制的容器
  • 2.若复制数组,则利用algorithm头文件里的copy函数
int main(){

    int a[] = {1,2,3};
    int b[3];
    copy(a,a+3,b);
    for(int i = 0 ;i<3;i++){
        cout<<b[i]<<" ";
    }
}

输出结果 : 1 2 3

换其他数据类型也可以的

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北·海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值