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 initialized in A and B are m andn respectively.
思路:
归并排序的收尾工作 + 正着需要移动 那我们就反着来嘛
我的代码:
public class Solution { public void merge(int A[], int m, int B[], int n) { if(A == null || B == null || m + n == 0) return; int last = m + n - 1; int i = m - 1, j = n - 1; while(i >= 0 && j >= 0) { if(A[i] > B[j]) { A[last--] = A[i--]; } else { A[last--] = B[j--]; } } while(i >= 0) A[last--] = A[i--]; while(j >= 0) A[last--] = B[j--]; return; } }
posted on 2015-03-08 21:00 zhouzhou0615 阅读(122) 评论(0) 编辑 收藏 举报