Lintcode6 Merge Two Sorted Arrays solution 题解

【题目描述】

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

合并两个排序的整数数组A和B变成一个新的数组。

【题目链接】

http://www.lintcode.com/en/problem/merge-two-sorted-arrays/

【题目解析】

A和B都已经是排好序的数组,我们只需要从后往前比较就可以了。

因为A有足够的空间容纳A + B,我们使用游标i指向m + n - 1,也就是最大数值存放的地方,从后往前遍历A,B,谁大就放到i这里,同时递减i。

【题目答案】

http://www.jiuzhang.com/solutions/merge-sorted-array/

时间: 2024-10-04 12:07:28

Lintcode6 Merge Two Sorted Arrays solution 题解的相关文章

Merge k Sorted Arrays

Given k sorted integer arrays, merge them into one sorted array. Given 3 sorted arrays: [ [1, 3, 5, 7], [2, 4, 6], [0, 8, 9, 10, 11] ] return [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]. 这题和merge k sorted lists 一脉相承,但是处理起来要复杂些,即linked list可以非常容易的从当前结点通过.n

Merge Two Sorted Arrays

class Solution { /** * @param A and B: sorted integer array A and B. * @return: A new sorted integer array */ public int[] mergeSortedArray(int[] A, int[] B) { // Write your code here int n = A.length + B.length; int[] rst = new int[n]; int a = 0; in

EPI (Heap) Merge Multiple sorted arrays.

#include <vector> #include <iostream> #include <queue> using namespace std; /* Write a program that takes an input a set of sorted sequences and compute the union of these sequences as a sorted sequence. For example: [3, 5, 7], [0, 6, 8]

LeetCode-21 Merge Two Sorted Lists Solution (with Java)

1. Description: 2. Examples: 3.Solutions: 1 /** 2 * Created by sheepcore on 2019-05-07 3 * Definition for singly-linked list. 4 * public class ListNode { 5 * int val; 6 * ListNode next; 7 * ListNode(int x) { val = x; } 8 * } 9 */ 10 class Solution {

23. Merge k Sorted Lists - LeetCode

Question 23.?Merge k Sorted Lists Solution 题目大意:合并链表数组(每个链表中的元素是有序的),要求合并后的链表也是有序的 思路:遍历链表数组,每次取最小节点 Java实现: public ListNode mergeKLists(ListNode[] lists) { ListNode preHead = new ListNode(0); ListNode minNode = getMinNode(lists); ListNode tmpNode =

leetcode题解||Median of Two Sorted Arrays问题

problem: There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). thinking: (1)求中位数,就是求已序数列的中间位置对应的数字,分奇偶.两个已序数组求中位数,就是求合并后的中位数. (2)采用merge sor

[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

【题解】【数组】【Leetcode】Median of Two Sorted Arrays

Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

leecode 题解 || Merge k Sorted Lists 问题

problem: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. Tags Divide and Conquer Linked List Heap 合并K个已序单链表 thinking: (1)题目没有要求不能够新开ListNode,所以暴力破解法:提取K个list的keyword.排序.新建结点插入.这样的情况对原list是否排好序没有要求. 排