LintCode 6.合并排序数组 ||
生活随笔
收集整理的這篇文章主要介紹了
LintCode 6.合并排序数组 ||
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
import org.junit.Test;import java.util.Arrays;public class MergeSort {/*** @param A: sorted integer array A* @param B: sorted integer array B* @return: A new sorted integer array* <p>* 合并排序數(shù)組 II* 合并兩個排序的整數(shù)數(shù)組A和B變成一個新的數(shù)組。* <p>* 樣例* 給出A=[1,2,3,4],B=[2,4,5,6],返回 [1,2,2,3,4,4,5,6]* <p>* 挑戰(zhàn)* 你能否優(yōu)化你的算法,如果其中一個數(shù)組很大而另一個數(shù)組很小?*/public int[] mergeSortedArray(int[] A, int[] B) {// write your code hereint i = A.length;int j = B.length;int x = 0;int y = 0;int[] C = new int[i + j];int k = 0;while (x < i && y < j) {if (A[x] <= B[y]) {C[k++] = A[x++];} else {C[k++] = B[y++];}}while (x < i) {C[k++] = A[x++];}while (y < j) {C[k++] = B[y++];}return C;}@Testpublic void testMergeSortedArray() {int[] A = {23, 4, 2, 34, 2, 34, 32};int[] B = {6, 245, 3, 234, 2, 46, 23, 45, 23423, 3, 4, 23};System.out.println(Arrays.toString(A));System.out.println(Arrays.toString(B));System.out.println(Arrays.toString(mergeSortedArray(A, B)));}
}
轉(zhuǎn)載于:https://www.cnblogs.com/wei1/p/9582062.html
總結(jié)
以上是生活随笔為你收集整理的LintCode 6.合并排序数组 ||的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 何小鹏:超快充+高续航+自营充电站将淘汰
- 下一篇: Jquery中post与get之间的区别