[Leetcode] Merge Sorted Array (Java)

来源:互联网 发布:怎样安装photoshop软件 编辑:程序博客网 时间:2024/05/21 14:50

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 from B. The number of elements initialized in A and B are m and n respectively.

简单的有序数组合并,不多说

public class Solution {    public void merge(int A[], int m, int B[], int n) {    int index=m+n-1;int curA = m-1;int curB = n-1;while(index>=0){if(curA>=0&&curB>=0){if(A[curA]>=B[curB]){A[index--]=A[curA--];}else {A[index--]=B[curB--];}}else if(curA>=0){A[index--]=A[curA--];}else {A[index--]=B[curB--];}}            }}



0 0
原创粉丝点击