leetcode-19-merge

88. Merge Sorted Array

解题思路:

需要注意,两个数组是排好序的,且nums1够大。所以从两个数组的尾端开始比较,大的那个放在nums1的尾部,并且放了之后就可以前进。

例如nums1[i]<nums2[j],那么nums1尾部放nums2[j],然后j--。需要注意的是i可能小于0,这时候,需要用nums2[j]的值填充。

void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
        int i = m - 1;
        int j = n - 1;
        int index = m + n - 1;
        while (j >= 0) {
            if (i < 0)
            nums1[index--] = nums2[j--];
            else
            nums1[index--] = nums1[i] > nums2[j] ? nums1[i--]:nums2[j--];
        }
    }


21. Merge Two Sorted Lists

时间: 2024-10-03 21:54:13

leetcode-19-merge的相关文章

LeetCode --- 56. Merge Intervals

题目链接:Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18]. 这道题的要求是将给定的一组间隔中有重叠的进行合并. 将间隔合并,首先要找到相邻的间隔,然后看其是否有重叠,如果有,就进行合并. 因此,首先考虑对数组排序.排序的时候,只需要按

LeetCode OJ - Merge Sorted Array

原地归并. 下面是AC代码: 1 public void merge(int A[], int m, int B[], int n) { 2 3 int len = A.length; 4 //first copy m elements of A to the end of A 5 for(int i=m-1,j = len-1;i>=0;i--){ 6 A[j--] = A[i]; 7 } 8 //merge the A and B 9 int startA = len - m; 10 int

LeetCode --- 88. Merge Sorted Array

题目链接:Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements init

LeetCode:Merge Two Sorted Lists - 拼接两个有序链表

1.题目名称 Merge Two Sorted Lists(按升序拼接两个有序链表) 2.题目地址 https://leetcode.com/problems/merge-two-sorted-lists/ 3.题目内容 英文: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 l

[LeetCode 题解]: Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题意:对k个有序的链表进行归并排序.并分析其复杂度. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(N

Java for LeetCode 023 Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解题思路一: 之前我们有mergeTwoLists(ListNode l1, ListNode l2)方法,直接调用的话,需要k-1次调用,每次调用都需要产生一个ListNode[],空间开销很大.如果采用分治的思想,对相邻的两个ListNode进行mergeTwoLists,每次将规模减少一半,直到

[LeetCode] 023. Merge k Sorted Lists (Hard) (C++/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 023. Merge k Sorted Lists (Hard) 链接: 题目:https://oj.leetcode.com/problems/merge-k-sorted-lists/ 代码(github):https://github.com/illuz/leetcode 题意: 和 021. Merge T

[LeetCode] 23. Merge k Sorted Lists 合并k个有序链表

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 与21. Merge Two Sorted Lists的拓展,这道题要合并k个有序链表,还是可以两两合并. 类似题目: [LeetCode] 21. Merge Two Sorted Lists 合并有序链表 原文地址:https://www.cnblogs.com/lightwindy/p/8512

[LeetCode]21 Merge Two Sorted Lists 合并两个有序链表

---恢复内容开始--- [LeetCode]21 Merge Two Sorted Lists 合并两个有序链表 Description 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. Example Example: Input: 1->2->4, 1-&g

19.1.29 [LeetCode 21] Merge Two Sorted Lists

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. Example: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4 1 class Solution { 2 public: