88. Merge Sorted Array

这道题目很简单,但是意思要看懂,给的参数和条件。
描述:
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
Note:
You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initialized in nums1 and nums2 are m and n respectively.

意思是:给出两个排序过的int型数组nums1和nums2,合并nums2到nums1中。这个合并假设nums1有足够的空间。
给出了4个参数:void merge(vector& nums1, int m, vector& nums2, int n)
其中的m和n分别是nums1和nums2中的有效字符数。

思路一:

1.要将nums2合并到nums1中,有效字符数是m+n,申请一个新的大小为m+n的数组res来存放排序后的结果。
2.从头开始比较nums1和nums2,将较为小的那一个存入res中,res下表++。这时候必然会有一个数组先存完,即i或者j达到上限,跳出循环。
3.将剩余下的那一部分元素拷贝到res中,再swap掉nums1.

c++,这次两个代码的思路不一样,见下

class Solution {
public:
    void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
        if(n == 0){return;}
        if(m == 0 && n != 0 ){
            nums1.swap(nums2);
            return;
        }
        vector<int> res;
        res.resize(m+n);
        int i = 0;
        int j = 0;
        int temp = 0;
        while(i < m && j < n){
            if(nums1[i] <= nums2[j]){
                res[temp] = nums1[i++];
            }
            else{
                res[temp] = nums2[j++];
            }
            temp++;
        }    
        if(i < m){
            for(int a = temp;a < (m+n);a++){
                res[a] = nums1[i++];
            }
        }
        if(j < n){
            for(int a = temp;a < (m+n);a++){
                res[a] = nums2[j++];
            }
        }   
        nums1.swap(res);     
    }
};

python:

在将以上代码转换成python代码的时候遇到了一些问题,貌似这里使用python的话只能允许在nums1原list上进行修改(我猜是底层c的引用的问题,不知道在哪儿限制了),使得不能新建一个list,对新建的list操作再重新赋值到nums1上去。

思路二

由于这里给出的条件是nums1有足够大的空间,查了一些博文,引用了别人的想法,从nums1的最后开始操作,将比较得到较大的那些值赋到nums1尾部,最后哪个赋完了,就跳出循环。
如果nums2有剩余,将其拷贝到nums1余下的空间中,没有,就结束了

class Solution(object):
    def merge(self, nums1, m, nums2, n):
        a = m - 1
        b = n - 1 
        while a >= 0 and b >= 0:
            if nums1[a] >= nums2[b]:
                nums1[a+b+1] = nums1[a]
                a -= 1
            else:
                nums1[a+b+1] = nums2[b]
                b -= 1
        #1.如果是由于b = -1退出while,那么a中余下的元素就全部是小于b的起始元素的了,下面的b+1==0,相当于没操作 

        #2.这里处理b还有剩余的情况,将nums2从开始到b(b+1表示0~b)的所有数赋值到nums1中
        nums1[:b+1] = nums2[:b+1]     
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、付费专栏及课程。

余额充值