【LeetCode练习题】Merge Sorted Array
Merge Sorted Array
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 andn respectively.
题目意思:
将两个有序的数组合并成一个有序的数组。A和B合并到A中且A中的空间足够。
解题思路:
1,逗比的解法。
int compare(const void *p1,const void *p2){
int a = *(int *)p1;
int b = *(int *)p2;
return a-b;
}
class Solution {
public:
void merge(int A[], int m, int B[], int n) {
for(int i = m,j = ; i < m+n; i++,j++){
A[i] = B[j];
}
qsort(A,m+n,sizeof(int),compare);
}
};
2,题目想要我们这样解。
跟合并两个链表是差不多的。区别在两个数组A和B从后往前来一一的作比较,而链表是从头开始比较的。
class Solution {
public:
void merge(int A[], int m, int B[], int n) {
int pa = m - , pb = n - , pr = m + n - ;
while(pa >= && pb >= ) {
if(A[pa] > B[pb])
A[pr--] = A[pa--];
else
A[pr--] = B[pb--];
}
while(pb >= )
A[pr--] = B[pb--];
}
};
╭(╯^╰)╮……Anyway
反正两种方法都AC了,所以,就这样吧。这题简单。
【LeetCode练习题】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. ...
- Leetcode 88. Merge Sorted Array(easy)
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...
随机推荐
- cf471A MUH and Sticks
A. MUH and Sticks time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- hdu 5495 LCS
Problem Description You are given two sequence {a1,a2,...,an} and {b1,b2,...,bn}. Both sequences are ...
- java各公司笔试题集1
IBM笔试题 注:IBM笔试题一小时之内完成,题目全部用英文描述,这里用中文表述 一.名词解释 1.Eclipse 2.J2EE 3.EJB 4.Ajax 5.Web service 二.找出以下代码 ...
- ubuntu 文件readonly的问题: W10: Warning: Changing a readonly file 解决办法
日前,笔者遇到一个奇怪且让人蛋疼的问题,借用别人的话"大家在linux上编辑文件的时候,明明是使用的root登录的,可是这种至高无上的权限在按下i的时候被那串红色错误亵渎了W10: Warn ...
- vi/vim经常使用命令
工作模式 插入命令 a 在光标后附加文本 A 在本行行尾附加文本 i 在光标前插入 I 在本行行首插入文本 o 在光标以下插入新的一行 O 在光标上面插入新的一行 定位命令 h 左移一个字符/ 向左的 ...
- Qt开始学习的一些问题
1.需要将qmake.moc和qvfb的路径加入到系统的环境变量: qmake.moc:export PATH=$PATH:/usr/local/Trolltech/QtEmbedded-4.6.1- ...
- Oracle DBlink的创建-查看与删除
DBlink常用于在两个Oracle数据库之间相互连接,如手工同步数据时,DBLink是最方便快捷的手段之一. 1.创建DBLink语法:create public database link < ...
- [KMP求最小循环节][HDU3746][Cyclic Nacklace]
题意 给你个字符串,问在字符串末尾还要添加几个字符,使得字符串循环2次以上. 解法 无论这个串是不是循环串 i-next[i] 都能求出它的最小循环节 代码: /* 思路:kmp+字符串的最小循环节问 ...
- idea maven jetty插件热部署
maven tomcat插件好像无法进行热部署,jetty可以如下配置实现热部署,但是idea无法进行自动编译,所以需要如下快捷键 Ctrl+Shift+F9,编译 Ctrl+F9,生成项目 < ...
- vs2010 中检测到有潜在危险的 Request.Form 值
解决方法 : 一般在网上搜只有以下两种处理方式: 1.在报错的页面前吧<%Page%>标签中增加validateRequest="false"的属性为false 如下所 ...