有序数组结合问题-merge two sorted array

本文介绍如何将两个已排序的整数数组A和B合并成一个新的有序数组C。重点在于理解合并过程中的细节,如需声明额外数组C并维护A和B的当前索引。文中提到初始化索引的重要性,未初始化的局部变量可能会导致错误。同时,讨论了全局变量和局部变量的区别,全局变量默认初始化为0,而局部变量未初始化时值不确定。
摘要由CSDN通过智能技术生成

Problem 3:

Merge two given sorted integer array A and B into a new sorted integer array.

解释:

   两个有序数组A和B结合,要先自己声明一个数组C,C中的元素依次由A和B中元素比较得来,每个数组都要有记录当前位置的index。

(自己写的时候半天AC不了,用IDE调试发现是我初始化的问题,index 没有初始化为0,int类型编译器应该给我随便赋了一个值)

代码:

class Solution {
public:
    /*
     * @param A: sorted integer array A
     * @param B: sorted integer array B
     * @return: A new sorted integer array
     */
    vector<int> mergeSortedArray(vector<int> &A, vector<int> &B) {
        // write your code here
        int x = A.size();
        int y = B.size();
        vector<int> C(x+y);
        int i = 0, j = 0,k = 0;
        while(i < x && j < y){
            if(A[i]<B[j]){
                C[k++] = A[i++];
            }
            else{
                 C[k++] = B[j++];
            }
        }
        while(i<x){
             C[k++] = A[i++];
        }
         while(j<y){
             C[k++] = B[j++];
        }
        return C;
    }
};


后续:

关于int类型未初始化的问题:

若编码时int 类型的变量未初始化,全局变量、静态变量默认初始化为0,局部变量为随机值。

再捂脸复习一下哪些是全局变量,哪些是局部变量:

在函数内定义的变量都是局部变量,作用域仅在函数内部。形参也是局部变量,实参给形参传值的过程也是给局部变量赋值的过程;如

int main(){   
 int m,n;  //m,n仅在函数main()内有效    
 return 0;
}

m,n都是局部变量。

在函数外声明的为全局变量,作用域为整个程序, 也就是所有的.c和.h文件,可以通过extern 链接操作。


至于各种类型变量在编译运行期间究竟是如何处理的,之后会统一研究整理一下发上来。
To merge k sorted linked lists, one approach is to repeatedly merge two of the linked lists until all k lists have been merged into one. We can use a priority queue to keep track of the minimum element across all k linked lists at any given time. Here's the code to implement this idea: ``` struct ListNode { int val; ListNode* next; ListNode(int x) : val(x), next(NULL) {} }; // Custom comparator for the priority queue struct CompareNode { bool operator()(const ListNode* node1, const ListNode* node2) const { return node1->val > node2->val; } }; ListNode* mergeKLists(vector<ListNode*>& lists) { priority_queue<ListNode*, vector<ListNode*>, CompareNode> pq; for (ListNode* list : lists) { if (list) { pq.push(list); } } ListNode* dummy = new ListNode(-1); ListNode* curr = dummy; while (!pq.empty()) { ListNode* node = pq.top(); pq.pop(); curr->next = node; curr = curr->next; if (node->next) { pq.push(node->next); } } return dummy->next; } ``` We start by initializing a priority queue with all the head nodes of the k linked lists. We use a custom comparator that compares the values of two nodes and returns true if the first node's value is less than the second node's value. We then create a dummy node to serve as the head of the merged linked list, and a current node to keep track of the last node in the merged linked list. We repeatedly pop the minimum node from the priority queue and append it to the merged linked list. If the popped node has a next node, we push it onto the priority queue. Once the priority queue is empty, we return the head of the merged linked list. Note that this implementation has a time complexity of O(n log k), where n is the total number of nodes across all k linked lists, and a space complexity of O(k).
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值