这道题的无聊之处在于题目其实给了一些很奇怪的测试用例。比如他会给一些空的数组来,但是这个是不科学的,因为在C++中不允许定义一个空的列表,我们用的又不是那种糙又快的python,所以在这里我遇到了一些问题,后来还是解决了。这道题题目如下:

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 initialized in A and B are m and n respectively.

然后这个地方给A的长度又非常的蛋疼,又是m指A的长度,又是A的长度无限大,这让一个严格静态的C++使用者非常蛋疼。当然也有可能是我水平不行【逃!

我一开始是想到我先把B做成有序,然后每次给A做个插入就行了,时间消耗那里在B有序后只用遍历A一次了,(但是每次移位置还是要有消耗,这个我没算出来),这个在遇到题目上的那些奇怪测试与奇怪的A的长度上出了各种问题。所以我干脆直接定义了一个vector,然后把B排序,然后将两个数组合并,这个算法也是算导的第一章内容。代码如下:

void Qsort(int a[], int low, int high)
{
if (low >= high)
{
return;
}
int first = low;
int last = high;
int key = a[first];/*用字表的第一个记录作为枢轴*/
while (first<last)
{
while (first<last&&a[last] >= key)
--last;
a[first] = a[last];/*将比第一个小的移到低端*/
while (first<last&&a[first] <= key)
++first;
a[last] = a[first];/*将比第一个大的移到高端*/
}
a[first] = key;/*枢轴记录到位*/
Qsort(a, low, first - 1);
Qsort(a, first + 1, high);
} void main()
{ int A[] = { 1, 2, 3, 4, 6, 7, 14, 15, 20, 35, 45, 55, 56, 57, 58 };
int B[] = { 9, 5, 38 };
int m = sizeof(A) / sizeof(A[0]);
int n = sizeof(B) / sizeof(B[0]);
int i = 0;
int j = 0;
vector<int> tmp; Qsort(B, 0, n - 1);
i = 0;
j = 0;
while (i < m && j < n)
{
if (A[i] > B[j])
{
tmp.push_back(B[j]);
j++;
}
else
{
tmp.push_back(A[i]);
i++;
}
} while (i < m)
{
tmp.push_back(A[i]);
i++;
}
while (j < n)
{
tmp.push_back(B[j]);
j++;
} for (i = 0; i < m+n; i++)
{
cout << tmp.at(i) << ' ';
} system("PAUSE");
}

然后直接通过了。

[leetcode] 12. Merge Sorted Array的更多相关文章

  1. [LeetCode] 88. Merge Sorted Array 混合插入有序数组

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...

  2. Leetcode#88. Merge Sorted Array(合并两个有序数组)

    题目描述 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分别为 m ...

  3. LeetCode 88. Merge Sorted Array(合并有序数组)

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...

  4. [LeetCode] 88. Merge Sorted Array 合并有序数组

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...

  5. 【leetcode】Merge Sorted Array

    题目描述 Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assu ...

  6. LeetCode 88 Merge Sorted Array

    Problem: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array ...

  7. 【题解】【数组】【Leetcode】Merge Sorted Array

    Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume th ...

  8. 【leetcode】Merge Sorted Array(合并两个有序数组到其中一个数组中)

    题目: Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assum ...

  9. leetcode[89] Merge Sorted Array

    合并两个有序数组,放在A中,A中的空间足够. Given two sorted integer arrays A and B, merge B into A as one sorted array. ...

随机推荐

  1. Hive 体系结构

    1.Hive架构与基本组成     下面是Hive的架构图. 图1.1 Hive体系结构     Hive的体系结构可以分为以下几部分:     (1)用户接口主要有三个:CLI,Client 和 W ...

  2. CFGym 101158B(巨坑题)

    前言:无话可说,看懂题意就没什么难度了. 题意:对于[0, 9999]区间内的每一个数b,通过输入的转换表,得到一个e值,把这个值添加到b的后面,得到一个五位数c.对于c的每一位,从0枚举到9,得到5 ...

  3. javascript精髓篇之原型链维护和继承.

    一.两个原型 很多人都知道javascript是原型继承,每个构造函数都有一个prototype成员,通过它就可以把javascript的继承演义的美轮美奂了. 其实啊,光靠这一个属性是无法完成jav ...

  4. 32.使用来MethodFilterInterceptor灵活拦截

    转自:https://wenku.baidu.com/view/84fa86ae360cba1aa911da02.html 步骤一.建立MethodAction,代码如下: package com.a ...

  5. MySQL 常用命令行

    增加新用户 格式:grant 权限 on 数据库.* to 用户名@登录主机 identified by "密码" 如,增加一个用户user1密码为password1,让其可以在本 ...

  6. jQuery load() 判断 iframe 是否加载完毕

    判断 iframe 是否加载完毕  方法.jQuery load() var frm = document.getElementById('myiframe'); $(frm).load(functi ...

  7. Maven(三)理解Maven核心概念

    转载自: http://www.cnblogs.com/holbrook/archive/2012/12/24/2830519.html 本文以类图的方式,介绍maven核心的12个概念以及相互之间的 ...

  8. C#委托(匿名函数)的各种变形写法

      static void TestDelegate() { //类C++11风格:指定初始化容量20,使用初始化列表给部分成员赋值 ) { , , , , -, , }; ; i < lst. ...

  9. [ShaderStaff] Vignette Effect

    操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:GLSL | C 最近在看Cardboard实现,其中关于畸变的着色器代码中有加入 晕影Vignette 效果的实现,固在 ...

  10. x64位windows 2003 server中“Server 对象 错误 'ASP 0177 : 800700c1' Server.CreateObject 失败”问题

    给朋友看一个老asp网站图片不能上传问题,试过网上各种办法都提示: Server 对象 错误 'ASP 0177 : 800700c1' Server.CreateObject 失败 最终问题出在x6 ...