[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. ...
随机推荐
- 峰Spring4学习(8)spring对事务的支持
一.事务简介: 二.编程式事务管理: 例子 1.需求:模拟转账,张三向李四转账50元: 数据库中存在t_count表: 代码实现: BankDao.java: package com.cy.dao; ...
- Spring cloud 之Feign基本使用
首先导入feign的依赖: <!-- 添加feign声明式webservice client --> <dependence> <groupId>org.sprin ...
- Java-Web 用html和css写一个EasyMall注册界面
要求: html代码: <!DOCTYPE html> <html> <head> <title>EasyMall注册页面</title> ...
- 页面白屏并且报错PHP Parse error: syntax error, unexpected end of file in 试了很久总算解决了
页面白屏并且报错PHP Parse error: syntax error, unexpected end of file in 试了很久 啥短标记,打开,都试了 最简单的办法 是重新建立一个文件, ...
- 【CentOS 6.5】【转】新版本linux生成xorg.conf
新版本的linux如何生成xorg.conf 较新版本的linux系统都已经没有xorg.conf文件,但是有时候为了对显示做微调或为了支持多屏显示等原因,还需要手工生成一个xorg.conf,然后根 ...
- Apache Flume 安装文档、日志收集
简介: 官网 http://flume.apache.org 文档 https://flume.apache.org/FlumeUserGuide.html hadoop 生态系统中,flume 的职 ...
- Eclipse开启或取消快速导航栏(toggle breadcrumb)
在Eclipse中快速调出导航栏 关闭导航栏: 在视图的设置中,去掉breadcrum的勾选状态
- Notepad++中的高级查找
准备以下字符串用来演示 abcdeab cdeabcde abcd eabcde 基于扩展的查找 基于扩展的查找不能算是真正的正则表达式搜索,因此这种查找方式仅是提供了支持转义字符.主要常用的 ...
- orchard cms 项目迁移
删除Orchard.Web 下的 App_Data 目录,重新安装项目
- python's output redirect
[python's output redirect] fin = open("xx.txt", 'r'); print >>fin, "hello world ...