LintCode 6. 合并排序数组 II

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

样例

给出A=[1,2,3,4],B=[2,4,5,6],返回 [1,2,2,3,4,4,5,6]

挑战

你能否优化你的算法,如果其中一个数组很大而另一个数组很小?

解:简单的归并排序。

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
        vector<int> result;
        int i=0,j=0;
        int szA=A.size();
        int szB=B.size();
        while(true)
        {
            if(i>=szA&&j<szB)
            {
                result.push_back(B[j++]);
            }
            else if(i<szA&&j>=szB)
            {
                result.push_back(A[i++]);
            }
            else if(i<szA&&j<szB)
            {
                if(A[i]<B[j])
                {
                    result.push_back(A[i++]);

                }
                else
                {
                    result.push_back(B[j++]);
                }
            }
            else
            break;

        }
        return result;
    }
};

原文地址:https://www.cnblogs.com/zslhg903/p/8361862.html

时间: 2024-08-30 03:07:33

LintCode 6. 合并排序数组 II的相关文章

lintcode 容易题:Merge Sorted Array II 合并排序数组 II

题目: 合并排序数组 II 合并两个排序的整数数组A和B变成一个新的数组. 样例 给出A = [1, 2, 3, empty, empty] B = [4,5] 合并之后A将变成[1,2,3,4,5] 注意 你可以假设A具有足够的空间(A数组的大小大于或等于m+n)去添加B中的元素. 解题: 这里给的是两个数组,上题给的是ArrayList格式,比较好处理,重新定义一个长度m+n的数组,太无节操,其他方法一时想不起来 官方解题,方法很技巧,可以倒着排序,这样就很简单了 Java程序: class

[LintCode] 合并排序数组II

1 class Solution { 2 public: 3 /** 4 * @param A: sorted integer array A which has m elements, 5 * but size of A is m+n 6 * @param B: sorted integer array B which has n elements 7 * @return: void 8 */ 9 void mergeSortedArray(int A[], int m, int B[], i

lintcode_64.合并排序数组 II

合并两个排序的整数数组A和B变成一个新的数组. 你可以假设A具有足够的空间(A数组的大小大于或等于m+n)去添加B中的元素. 样例 给出 A = [1, 2, 3, empty, empty], B = [4, 5] 合并之后 A 将变成 [1,2,3,4,5] class Solution: """ @param: A: sorted integer array A which has m elements, but size of A is m+n @param: m:

[容易]合并排序数组 II

题目来源:http://www.lintcode.com/zh-cn/problem/merge-sorted-array/ C++版 VS2012测试通过 1 //#include <iostream> 2 //#include <vector> 3 //using namespace std; 4 5 class Solution { 6 public: 7 /** 8 * @param A: sorted integer array A which has m element

【LeetCode-面试算法经典-Java实现】【088-Merge Sorted Array(合并排序数组)】

[088-Merge Sorted Array(合并排序数组)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 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 +

lintcode:寻找旋转排序数组中的最小值 II

寻找旋转排序数组中的最小值 II 假设一个旋转排序的数组其起始位置是未知的(比如0 1 2 4 5 6 7 可能变成是4 5 6 7 0 1 2). 你需要找到其中最小的元素. 数组中可能存在重复的元素. 解题 暴力直接线性查找 或者,线性找到第一个开始降序的位置对应的数 应该考虑二分法 递归 + 二分 public class Solution { /** * @param num: a rotated sorted array * @return: the minimum number in

[LeetCode]6. Merge Sorted Arrays合并排序数组

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 initi

LeetCode 88 Merge Sorted Array(合并排序数组)(*)

翻译 给定两个排序的整型数组nums1和nums2,将nums2合并到nums1成一个排序数组. 批注: 你可以假设nums1中有足够的空间(空间大于或等于m+n)来存放来自nums2的额外元素. nums1和nums2的初始空间分别是m和n. 原文 Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: You may assume that nums1

63. 搜索旋转排序数组 II

跟进"搜索旋转排序数组",假如有重复元素又将如何? 是否会影响运行时间复杂度? 如何影响? 为何会影响? 写出一个函数判断给定的目标值是否出现在数组中. 样例 给出[3,4,4,5,7,0,1,2]和target=4,返回 true 发现lintcode有一点不好就是这种O(n)的解法也能给过 1 bool search(vector<int> &A, int target) { 2 // write your code here 3 vector<int&g