[leetcode] 12. Merge Sorted Array
这道题的无聊之处在于题目其实给了一些很奇怪的测试用例。比如他会给一些空的数组来,但是这个是不科学的,因为在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的更多相关文章
- [LeetCode] 88. Merge Sorted Array 混合插入有序数组
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...
- Leetcode#88. Merge Sorted Array(合并两个有序数组)
题目描述 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分别为 m ...
- LeetCode 88. Merge Sorted Array(合并有序数组)
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...
- [LeetCode] 88. Merge Sorted Array 合并有序数组
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...
- 【leetcode】Merge Sorted Array
题目描述 Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assu ...
- LeetCode 88 Merge Sorted Array
Problem: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array ...
- 【题解】【数组】【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 ...
- 【leetcode】Merge Sorted Array(合并两个有序数组到其中一个数组中)
题目: Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assum ...
- leetcode[89] Merge Sorted Array
合并两个有序数组,放在A中,A中的空间足够. Given two sorted integer arrays A and B, merge B into A as one sorted array. ...
随机推荐
- 八、jdk工具之JvisualVM、JvisualVM之二--Java程序性能分析工具Java VisualVM
目录 一.jdk工具之jps(JVM Process Status Tools)命令使用 二.jdk命令之javah命令(C Header and Stub File Generator) 三.jdk ...
- [Android] 开发第八天
View 类是所有 UI组件的基类,它包含的 XML 属性和方法是所有组件都可使用的. ViewGroup 继承了 View 类,主要当作容器类使用,它是一个抽象类,实际使用中会使用它的子类作为容器. ...
- 简单神经网络TensorFlow实现
学习TensorFlow笔记 import tensorflow as tf #定义变量 #Variable 定义张量及shape w1= tf.Variable(tf.random_normal([ ...
- grunt 不是内部或外部命令,也不是可运行的程序或批处理文件
问题1 grunt 不是内部或外部命令,也不是可运行的程序或批处理文件 解决方法: Grunt和 Grunt 插件是通过 npm 安装并管理的,npm是 Node.js 的包管理器. 安装CLI 在继 ...
- php使用curl库进行ssl双向认证
官方文档: http://www.php.net/manual/zh/function.curl-setopt.php#10692 官方举例: <?phpcurl_setopt($ch, CUR ...
- LDAP属性对照表
AD属性对照表 姓 Sn 名 Givename 英文缩写 Initials 显示名称 displayName 描述 Description 办公室 physicalDeliveryOfficeName ...
- Mysql 性能分析 Explain
Mysql Query Optmize: 查询优化器, SQL语句会给Query Optimize他会执行他认为最优的方式.. Mysql 常见问题 CPU饱和,IO磁盘发生在装入数据大于内存时. E ...
- 使用 phpStorm 开发
苦恼蛋疼的曾哥工作室,让人痛不欲生,缓慢的同步速度,另人恼火的插件配置,让人疯狂的卡.简直是让人用了几天之后就不行了. 废话不多说,一款很好的php IDE. 1. phpStorm 下载 here ...
- 在Ubuntu16.04中安装Docker CE
apt-get install apt-transport-https ca-certificates curl software-properties-common curl -fsSL https ...
- IP地址工具类
/// <summary> /// 获取客户端IP地址 /// </summary> /// <returns></returns> public st ...