[Leetcode] Merge Sorted Array (C++)
我在Github上新建了一个解答Leetcode问题的Project, 大家可以参考, 目前是Java 为主,里面有leetcode上的题目,解答,还有一些基本的单元测试,方便大家起步。
题目:
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.
Tag:
Array; Two Pointers
体会:
这个小题,初看很简单,但是实现起来还是有些小地方需要特别注意。首先一个特别的地方就是在A里面merge B。如果是像原始merge算法从头开始merge的话,就可能会出现需要插入操作的地方。(比如需要把B[1]插入到A[2]后)。这时就需要把A[3]到A[last]全部往后移动一位。因为不是链表,明显不合适。但是题目又要求merge到A, 那怎么办呢,解决办法就是从A的屁股上开始merge,就是从大数到小数开始merge。
具体执行的时候,我们的目标就是把B merge 完,即int j = n - 1, while (j >= 0)就继续执行。
那什么时候选择来自B的元素呢,这里面要注意A的边界情况,即A的元素已经都用完了。所以会有两种情况选择来自B的元素:
一是A的元素用完了 (i.e. i < 0),无条件选择B的;二是当A没完时,有B[j] > A[i]。
class Solution {
public:
void merge(int A[], int m, int B[], int n) {
int i = m - ;
int j = n - ;
int k = m + n - ;
// when B is all merged into A, job done
while (j >= ) {
// merge
if (i < || B[j] > A[i]) {
// when A is done or A is less than B, choose B
A[k] = B[j];
j--;
} else {
A[k] = A[i];
i--;
}
k--;
}
}
};
[Leetcode] Merge Sorted Array (C++)的更多相关文章
- [LeetCode] Merge Sorted Array 混合插入有序数组
Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...
- [leetcode]Merge Sorted Array @ Python
原题地址:https://oj.leetcode.com/problems/merge-sorted-array/ 题意:Given two sorted integer arrays A and B ...
- [LeetCode] Merge Sorted Array
Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...
- [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 合并已排序的数组
void merge(int A[], int m, int B[], int n) { int *a=A,*b=B; ,j=; ||m==){ //针对特殊情况,比如A或B中无元素的情况 & ...
- leetcode - Merge Sorted Array (run time beats 100.00% of cpp submissions.)
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...
- [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
Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note ...
- Leetcode#88. Merge Sorted Array(合并两个有序数组)
题目描述 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分别为 m ...
随机推荐
- HTML元素遮挡Flash之梦
wmode参数: transparent模式:可用z-index控制层级 opaque模式:可用z-index控制层级 window模式:flash层级在浏览器核心显示窗口之上,flash会盖住与他重 ...
- 神奇的match和replace
源自跟奈落大叔的讨论,PHP和JavaScript的比较. 正则: 先说几个正则写法: () 选择匹配一组, (?:) 降低 () 的优先级, .*? 和 .+? ,阻止 . 和 + 的贪婪. 还有一 ...
- http head
Accept HTTP_ACCEPT Accept-Charset HTTP_ACCEPT-CHARSET Accept-Encoding HTTP_ACCEPT-ENCODING Accept-La ...
- [HDU] 2063 过山车(二分图最大匹配)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=2063 女生为X集合,男生为Y集合,求二分图最大匹配数即可. #include<cstdio> ...
- udp之nat穿透的困惑
nat穿透实现:[A]内网地址[内A]192.168.1.176:25789通过stun服务器查询映射到的外网地址为外网地址[外A]212.10.55.124:26559UDPsocketA绑定到[内 ...
- Activiti 使用自己的身份认证服务
Activiti 中内置了用户和组管理的服务,由identityService 提供调用接口,默认在spring配置中如下: <bean id="identityService&quo ...
- LeetCode_Minimum Path Sum
iven a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- 利用linux信号机制调试段错误(Segment fault)
在实际开发过程中,大家可能会遇到段错误的问题,虽然是个老问题,但是其带来的隐患是极大的,只要出现一次,程序立即崩溃中止.如果程序运行在PC中,segment fault的调试相对比较方便,因为可以通过 ...
- Oracle优化笔记
2016-11-22 子查询:标量子查询 内联视图(in-line view) 半连接/反连接 标量子查询 select 后跟子查询 类似自定义函数 可用开窗函数之类的改写 内联视图(in ...
- Ext.window的close的问题
以前每次都是用的hide,关闭后隐藏窗体,下一次点击再打开,这种方法在我的随笔里面有,可是现在遇到一个问题,我的窗体里面有个formpanel,formpanel每一项都有一个默认值,意思就是修改的时 ...