LintCode之6 合并排序数组
2017-11-24 16:11
369 查看
题目来源:合并排序数组
题目描述:
合并两个排序的整数数组A和B变成一个新的数组。
样例:
给出A=[1,2,3,4],B=[2,4,5,6],返回 [1,2,2,3,4,4,5,6]
Java代码:
题目描述:
合并两个排序的整数数组A和B变成一个新的数组。
样例:
给出A=[1,2,3,4],B=[2,4,5,6],返回 [1,2,2,3,4,4,5,6]
Java代码:
public int[] mergeSortedArray(int[] A, int[] B) { // Write your code here int[] result = new int[A.length+B.length]; int acount=0,bcount=0,rcount=0; while (acount<A.length&&bcount<B.length) { if (A[acount]<=B[bcount]) { result[rcount++]=A[acount]; acount++; continue; } if (A[acount]>B[bcount]) { result[rcount++]=B[bcount]; bcount++; continue; } } if (acount>=A.length&&bcount<B.length) { while (bcount<B.length) { result[rcount++] = B[bcount++]; } } if (acount<A.length&&bcount>=B.length) { while (acount<A.length) { result[rcount++] = A[acount++]; } } return result; }
相关文章推荐
- Lintcode:合并排序数组
- LintCode Merge Sorted ArrayII 合并排序数组 II
- lintcode之 合并排序数组 II
- LintCode 6.合并排序数组 II
- lintcode merge-k-sorted-lists 合并k个排序链表
- LintCode:删除排序数组中的重复数字
- LINTCODE——将数组重新排序以构造最小值
- 把排序数组转换为高度最小的二叉搜索树(LintCode)
- LintCode:M-搜索旋转排序数组
- lintcode合并两个排序链表
- LintCode_63_搜索旋转排序数组 II
- LintCode-剑指Offer-(165)合并两个排序链表
- Google/LintCode:M-合并k个排序链表
- lintcode: 把排序数组转换为高度最小的二叉搜索树
- LintCode SortedArrayToBST 把排序数组转换为高度最小的二叉搜索树
- lintcode-搜索旋转排序数组-62
- lintcode 165. 合并两个排序链表
- LintCode 删除排序数组中的重复数字
- lintcode-39-恢复旋转排序数组
- LintCode:搜索旋转排序数组 II