这道题的无聊之处在于题目其实给了一些很奇怪的测试用例。比如他会给一些空的数组来,但是这个是不科学的,因为在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. 关于 android 环信无法正确获取昵称的问题

    本案例中 username 记录成 userId了, nick 始终为空...,这是 getNick() 取得的就是 username..... 如果想自己取得自己系统的nickname则 做以下调整 ...

  2. <转--大话session>

    大话Session 原文地址:http://www.cnblogs.com/shoru/archive/2010/02/19/1669395.html 结语 到这里,读者应该对session有了更多的 ...

  3. Java separatorChar 如何在Java里面添加 \

    Java手册 separatorChar public static final char separatorChar 与系统有关的默认名称分隔符.此字段被初始化为包含系统属性 file.separa ...

  4. 修改eclipse 代码字体大小以及文档字体大小

    1..点击[window]在弹出的窗口中选择[preferences] 2.在弹出窗口中找到依次点击General(常规)——Apprearance(外观)——Colors and Fonts(颜色和 ...

  5. 原生态Vim使用快捷键

    我的第一篇博客,凌晨2点加班不想睡,随便写点.本人菜鸟一个,努力学习,争取成为大神.. 第一篇写点什么东西呢,我目前是搞运维的,俗话说"工欲善其事必先利其器",Vim作为最基本的工 ...

  6. 学习了django对于sqlite3进行了了解,谈谈看法

    学习了django对于sqlite3进行了了解,谈谈看法 由于django默认使用的是sqlite3,写了几个建表语句, 然后数据做下迁移,其实就是建表语句的执行. 一直对sqlite3没有一个直观的 ...

  7. leetcode654

    class Solution { public: TreeNode* constructMaximumBinaryTree(vector<int>& nums) { ) retur ...

  8. leetcode204

    public class Solution { public int CountPrimes(int n) { ) { ; } ]; ]; ; ; i < n; i++) { mark[i] = ...

  9. Yii中利用filters来控制访问

    filters()方法定义在CController里,用Gii生成Controller时里面就有filters方法,代码如下: public function filters() { // retur ...

  10. ASP.NET中高级程序员 面试题

    1. 简要说一下.Net的编译过程. 2.ASP.NET与ASP的区别 3.谈一下ASP.NET页面生命周期 4.ASP.NET程序的运行机制.可以从一个页面的请求到返回的角度谈 5.Javascri ...