LeetCode 88 — Merge Sorted Array(C++ Java Python)

原创 2014年03月31日 17:05:55
题目:http://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 additional elements from B. The number of elements initialized in A and B are m and nrespectively.

题目翻译:

给定两个有序整数数组A和B,把B并入A成为一个有序数组。
注意:
可以假设A有足够的空间(大小大于等于m + n)来保存来自B的额外元素。A和B的初始元素个数分别为m和n。

分析:
        从后向前考虑。如果A中的先遍历完,应把B中剩下的元素复制到A中。
C++实现:
class Solution {
public:
    void merge(int A[], int m, int B[], int n) {
    	if(n == 0)
    	{
    		return;
    	}

    	int idx = m + n - 1;
    	int i = m - 1;
    	int j = n - 1;

    	while(i >= 0 && j >= 0)
    	{
    		if(A[i] >= B[j])
    		{
    			A[idx--] = A[i--];
    		}
    		else
    		{
    			A[idx--] = B[j--];
    		}
    	}

    	if(i == -1)
    	{
    		while(j >= 0)
    		{
    			A[j] = B[j];
    			j--;
    		}
    	}
    }
};
Java实现:
public class Solution {
    public void merge(int A[], int m, int B[], int n) {
		int idx = m + n - 1;
		int i = m - 1;
		int j = n - 1;

		while (i >= 0 && j >= 0) {
			if (A[i] >= B[j]) {
				A[idx--] = A[i--];
			} else {
				A[idx--] = B[j--];
			}
		}

		if (i == -1) {
			while (j >= 0) {
				A[j] = B[j];
				j--;
			}
		}
    }
}
Python实现:
class Solution:
    # @param A  a list of integers
    # @param m  an integer, length of A
    # @param B  a list of integers
    # @param n  an integer, length of B
    # @return nothing
    def merge(self, A, m, B, n):
        idx = m + n - 1
        i = m - 1
        j = n - 1
        
        while i >= 0 and j >= 0:
            if A[i] >= B[j]:
                A[idx] = A[i]
                i -= 1
            else:
                A[idx] = B[j]
                j -= 1
            
            idx -= 1
        
        if i == -1:
            while j >= 0:
                A[j] = B[j]
                j -= 1
        感谢阅读,欢迎评论!

相关文章推荐

88. Merge Sorted Array [easy] (Python)

题目链接https://leetcode.com/problems/merge-sorted-array/题目原文 Given two sorted integer arrays nums1 an...

leetCode 88.Merge Sorted Array (合并排序数组) 解题思路和方法

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

leetcode 88 Merge Sorted Array

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

Leetcode_88_Merge Sorted Array

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41631609 通过本文你可能学到的知识为: (1)当我们...

LeetCode(88)Merge Sorted Array

题目Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note: ...
  • fly_yr
  • fly_yr
  • 2015年08月20日 17:33
  • 1811

Leetcode:88. Merge Sorted Array(JAVA)

【问题描述】 Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted ar...

LeetCode88——Merge Sorted Array

题目 Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may ...

【leetcode c++】88 Merge Sorted Array

Merge Sorted Array Given two sorted integer arrays nums1 andnums2, merge nums2 into nums1 as one sor...
  • hqq39
  • hqq39
  • 2015年07月27日 14:27
  • 791

[leetcode]88. Merge Sorted Array (java)

题目描述: Merge two sorted linked lists and return it as a new list. The new list should be made by sp...

LeetCode 33 — Search in Rotated Sorted Array(C++ Java Python)

题目:http://oj.leetcode.com/problems/search-in-rotated-sorted-array/ Suppose a sorted array is rotate...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode 88 — Merge Sorted Array(C++ Java Python)
举报原因:
原因补充:

(最多只允许输入30个字)