您的位置:首页 > 其它

Flexpaper 参数配置说明

2013-03-10 03:12 211 查看
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 inserted = 0;
int i=0;
if(B==null||B.length==0){
return;
}
while(i<m){
if(A[i]>=B[0]){
int temp = A[i];
A[i] = B[0];
B[0] = temp;
int j=0;
while(j+1<n&&B[j]>B[j+1]){
temp = B[j];
B[j] = B[j+1];
B[j+1] = temp;
j++;
}
}
i++;
}
int j=0;
while(j<n){
A[i++]=B[j++];
}
}
}

中心思想:
有一个迭代器遍历A里所有的元素,然后和B里面的第一个元素比较,如果A的元素大于B的元素,那么交换,
始终保证B的第一个元素大于A当前遍历到的元素,并且保证B是有序的。最后把B衔接到A的末尾就行。
算法的复杂度目测是O(n*m)的

本文出自 “在云端” 博客,请务必保留此出处http://kcy1860.blog.51cto.com/2488842/1334514
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: