将两个排好序的序列合并成一个(指针和数组分别实现)

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
12         ListNode cur(0);
13         ListNode* temp = &cur;
14         while(l1&&l2){
15             if((l1->val)<=(l2->val)){
16                 temp->next = l1;
17                 l1 = l1->next;
18                 temp = temp->next;
19             }else if((l1->val)>(l2->val)){
20                 temp->next = l2;
21                 l2 = l2->next;
22                 temp = temp->next;
23             }
24         }
25         if(l1){
26             temp->next = l1;
27         }else{
28             temp->next = l2;
29         }
30         return cur.next;
31     }
32 };

2、数组实现,可以和指针采用类似的方法。要申请一个中间数组。若题目要求将合并的数组保存到num1中,我们可以最后将中间数组的值拷贝到num1中。

 1 class Solution {
 2 public:
 3     void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
 4         if(!nums2.size()){
 5             return;
 6         }
 7         vector<int> temp(m+n,0);
 8         int i = 0,j = 0;
 9         int k = 0;
10         while(i<m&&j<n){
11             if(nums1[i]<=nums2[j]){
12                 temp[k] = nums1[i];
13                 i++;
14                 k++;
15             }else if(nums1[i]>nums2[j]){
16                 temp[k] = nums2[j];
17                 j++;
18                 k++;
19             }
20         }
21         if(i<m){
22             while(i<m){
23                 temp[k] = nums1[i];
24                 k++;
25                 i++;
26             }
27         }else{
28             while(j<n){
29                 temp[k] = nums2[j];
30                 k++;
31                 j++;
32             }
33         }
34         nums1.assign(temp.begin(),temp.begin()+m+n);
35
36     }
37 };

若要求不让使用中间变量,可以使用以下算法

 1 void merge(vector<int>& nums1, int m, vector<int>& nums2, int n)
 2 {
 3     // num1r, num2r read iterators on nums1 and num2r, wi - write iterator
 4     for (int num1r = m - 1, num2r = n - 1, wi = m + n - 1;
 5         num2r >= 0; --wi)
 6     {
 7         if (num1r >= 0 && nums1[num1r] > nums2[num2r])
 8         {
 9             nums1[wi] = nums1[num1r--];
10         }
11         else
12         {
13             nums1[wi] = nums2[num2r--];
14         }
15     }
16 }

两种方法时间复杂度为均为O(n)

时间: 2024-08-25 12:19:28

将两个排好序的序列合并成一个(指针和数组分别实现)的相关文章

将两个排好序的数组,合并到另外一个数组中,并且合并之后的数组也是有序的。

int a[3] = {12, 15, 17}; int b[4] = { 2, 8, 16, 22}; int c[7] = {0}; int i = 0, j = 0, k = 0; while (i < 3 && j < 4 ) { if (a[i] > b[j]) { c[k++] = b[j++]; } else { c[k++] = a[i++]; } } while (i < 3) { c[k++] = a[i++]; } while (j <

【LeetCode-面试算法经典-Java实现】【021-Merge Two Sorted Lists(合并两个排好序的单链表)】

[021-Merge Two Sorted Lists(合并两个排好序的单链表)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 题目大意 合并两个排序链表并返回一个新的列表.新的链表的

通过另类的泛型约束将两个输入输出参数不同的方法合并成一个方法的实现

其实我也不知道如何定义这个标题,词乏,姑且先这样定义吧. 看了本文章的朋友,如果有更好标题,请告诉我,谢谢. 有个项目使用SDK时遇到这样一个情况. 该SDK有个BtPrinterManager类,拥有两个方法:ServerPrint和ClientPrint,这两个方法有一部分参数是一样的,一部分参数不一样. 现在我们要对这个类进行封装,把这两个方法合并成一个方法,并且使其拥有相同的输入参数和输出参数. 比较粗糙的做法是,把这两个方法的输入参数合并成一个输入模型类,把两个方法的输出参数也合并成一

Python 实现把两个排好序的的列表合并成一个排序列表

列表是生序的 # -*- coding: utf-8 -*- # 合并两个排序的数组 def merge_list(a, b): if not a: return b if not b: return a a_index = b_index = 0 ret = [] while a_index < len(a) and b_index < len(b): if a[a_index] <= b[b_index]: ret.append(a[a_index]) a_index += 1 el

实现2个排好序的子序列合并

数组a,b为已排序好的升序序列. 思路:1. 将a,b数组copy到一个新的数组c中(数组c的长度为a,b之和) 2. 在c中以数组a为基准,当b中的数值小于a的时候,a中以后数值向后移1位,然后把当前b的值赋值过来. 具体实现: public static int[] sort(int[] a, int[] b) { // 将a,b数组copy到数组c中 int[] c = new int[a.length + b.length]; System.arraycopy(a, 0, c, 0, a

两条带头结点的升序重复合并成一个无重复的升序链表

#include<stdio.h> #include<stdlib.h> typedef struct node{ int  data; struct node * next; }ElemSN; ElemSN  * Createlink(int a[],int n){ //建立带表头结点的链表 int i; ElemSN * h, * p; h=p=(ElemSN *)malloc(sizeof(ElemSN)); h->next=NULL; for( i=0;i<n;

JavaScript把两个数组对象合并成一个一一对应的数组对象

合并数组或者对象在数组或对象前面加...,是es6的新写法,然后数组的map方法会返回数组. var obj1 = [{ "id": 980550455852, "model": "XQG70-S1208FW", "color": "白", "invStatusName": "正品", "bactualQty": 10947, "brea

剑指Offer对答如流系列 - 二叉搜索树的后序遍历序列

面试题33:二叉搜索树的后序遍历序列 题目描述 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则返回true,否则返回false.假设输入的数组的任意两个数字都互不相同. 例如,输入数组{5.7.6.9.11.10.8},则返回true,因为这个整数序列是下图二叉搜索树的后序遍历结果. 如果输入的数组是{7.4.6.5},则由于没有哪棵二叉搜索树的后序遍历是这个序列,因此返回false. 问题分析 后序遍历 遍历顺序是 :左子树 -> 右子树 -> 根结点(最后遍历根节

二叉搜索树的后序遍历序列-剑指Offer

二叉搜索树的后序遍历序列 题目描述 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. 思路 后序遍历:左-->右-->根节点 二叉搜索树中,左子树的节点小于根节点,右子树的节点大于根节点 递归调用,根据大小区别左右子树,若在右子树中发现小于根节点的节点,就不是后续遍历序列 举一反三:如果要求处理一棵二叉树的遍历序列,我们可以先找到二叉树的根节点,再基于根节点把整棵树的遍历序列拆分成左子树对应的子序列和右子