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的更多相关文章

  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. ...

  10. Leetcode 88. Merge Sorted Array(easy)

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

随机推荐

  1. html 知识

    <pre name="code" class="python"><pre name="code" class=" ...

  2. 在Linux CentOS 6.5 (Final)上安装git-1.9.0

    CentOS 6.5 (Final)默认安装的git版本为1.7.1.3,而我们希望安装1.9.0版本.由于rpm安装库里没有1.9.0版本,因此我们需要找其它方法来安装. 网上有很多文章介绍了如何从 ...

  3. JS Message 网页消息提醒

    JS message是一个非常小的(用gzip压缩之后才3kb)JavaScript library 用于轻松在网页上展示通知提醒.除了通知,它还支持创建带风格的对话框和确认对话框.不需要任何JS框架 ...

  4. Gas Station 解答

    Problem There are N gas stations along a circular route, where the amount of gas at station i is gas ...

  5. Types of Binary Tree

    Complete Binary Tree According to wiki, A complete binary tree is a binary tree in which every level ...

  6. MongoDBAuth

    1,mogoDB 认证登陆

  7. IBatis——(一)

    IBatis是持久层的框架,也就是我们说的Dao层框架,关注数据库操作以及和Java对象之间的关联,我们将这样的框架也称之为ORM(Object/Relaction Mapping)框架.而这里映射的 ...

  8. HDU 4521 间隔》=1的LIS 线段树+dp

    九野的博客,转载请注明出处:http://blog.csdn.net/acmmmm/article/details/11991119 题意: n个数 d个距离 下面n个数的序列,求序列中的最长单调递增 ...

  9. 多线程中的lua同步问题

    最近写paintsnow::start时出现了一个非常麻烦的BUG,程序的Release版本大约每运行十几次就会有一次启动时崩溃(Debug版本还没崩溃过),崩溃点也不固定.经过简单分析之后,确定是线 ...

  10. Oracle SQL函数之日期函数

    sysdate [功能]:返回当前日期. [参数]:没有参数,没有括号 [返回]:日期 SQL> SELECT SYSDATE FROM DUAL; SYSDATE ----------- // ...