[lintcode easy]Merge Sorted Array

Merge Sorted Array

Given two sorted integer arrays A and B, merge B into A as one sorted array.

Have you met this question in a real interview?

Yes

Which company asked you this question? Airbnb Alibaba Amazon Apple Baidu Bloomberg Cisco Dropbox Ebay Facebook Google Hulu Intel Linkedin Microsoft NetEase Nvidia Oracle Pinterest Snapchat Tencent Twitter Uber Xiaomi Yahoo Yelp Zenefits

Thanks for your feedback.

Example

A = [1, 2, 3, empty, empty], B = [4, 5]

After merge, A will be filled as [1, 2, 3, 4, 5]

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.

///Because it is sorted array, we can compare them from the end to the start.

///which one is greater, put it in the last array A

///keep decreasing m and n

///until m or n equal to 0

///if m=0, copy all the numbers in array B to A;

class Solution {
    /**
     * @param A: sorted integer array A which has m elements,
     *           but size of A is m+n
     * @param B: sorted integer array B which has n elements
     * @return: void
     */
    public void mergeSortedArray(int[] A, int m, int[] B, int n) {
        // write your code here
        while(m>0&&n>0)
        {
           if(A[m-1]>B[n-1])
           {
               A[m+n-1]=A[m-1];
               m--;
           }
           else
           {
               A[m+n-1]=B[n-1];
               n--;
           }
        }

        while(n>0)
        {
            A[m+n-1]=B[n-1];
            n--;
        }

    }
}
时间: 2024-10-26 16:11:12

[lintcode easy]Merge Sorted Array的相关文章

[lintcode easy]Merge Sorted Array II

Merge Sorted Array II Merge two given sorted integer array A and B into a new sorted integer array. Example A=[1,2,3,4] B=[2,4,5,6] return [1,2,2,3,4,4,5,6] Challenge How can you optimize your algorithm if one array is very large and the other is ver

[lintcode easy]Convert Sorted Array to Binary Search Tree With Minimal Height

Given a sorted (increasing order) array, Convert it to create a binary tree with minimal height. Example Given [1,2,3,4,5,6,7], return 4 / 2 6 / \ / 1 3 5 7 Note There may exist multiple valid solutions, return any of them. ////////////////////// 二叉查

<LeetCode OJ> Merge Sorted Array【88】

88. Merge Sorted Array My Submissions Question Total Accepted: 80830 Total Submissions: 274120 Difficulty: Easy Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: You may assume that nums1 has enough sp

LeetCode --- 88. 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 init

[LeetCode] Merge Sorted Array [22]

题目 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 ar

【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 initialize

43. Merge Sorted Array && LRU Cache

Merge Sorted Array OJ: https://oj.leetcode.com/problems/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 add

LeetCode: Merge Sorted Array [088]

[题目] 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

算法5--排序--Merge Sorted Array

之前几天在忙其他的事情,没有时间更新,今天更新了几个,虽然有几个SMR的博客暂时没有开放,已经写好了,以后会慢慢开放的 今天再更新一个有关排序的算法题 1  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 to hold additional elements fr