合并有序数组的问题

面试题:两个有序数组,怎么把他们合并为一个数组,里面没有重复的数字;

比如两个数组:int [] arr1={1,2,3,4}; int [] arr2={4,5,6,7,8};


方式一:也是比较常用的一种,两个指针;

步骤:

          1.两个指针分别指向数组的起始位置;

          2.比较他们两个指向的值,如果小的放进另外一个数组,进行比较;

          3.再判断两个数组是否都遍历完;

package leetcode;

import org.junit.Test;
import java.util.Arrays;

/**
 * @author zhangyu
 * @version V1.0
 * @ClassName: MergeArray
 * @Description: TOTO
 * @date 2018/11/21 20:08
 **/

public class MergeArray {
    @Test
    public void fun() {
        int[] arr1 = {1, 3, 5, 7};
        int[] arr2 = {2, 4, 6, 8};
        int[] newArr = merge(arr1, arr2);
        for (int number : newArr) {
            System.out.print(number + " ");
        }
    }

    private int[] merge(int[] arr1, int[] arr2) {
        // 考虑临界值,当时题目不为空数组,所以不需要考虑这种情况
        if (arr1 == null || arr1.length == 0) {
            return arr2;
        }
        if (arr2 == null || arr2.length == 0) {
            return arr1;
        }
        int i = 0;
        int j = 0;
        int[] newArr = new int[arr1.length + arr2.length];
        int count = 0;
        while (i < arr1.length && j < arr2.length) {
            if (arr1[i] < arr2[j]) {
                newArr[count] = arr1[i];
                if(i+1!=arr1.length){}
                i++;
                count++;
            }else if (arr1[i] == arr2[j]) {
                newArr[count] = arr1[i];
                i++;
                j++;
                count++;
            }else{
                newArr[count] = arr2[j];
                j++;
                count++;
            }
            // 当arr1的指针已经到了末尾,直接把arr2后面的数组添加到数组中
            if (i  == arr1.length) {
                for (int index = j; index < arr2.length; index++) {
                    newArr[count] = arr2[index];
                    count++;
                }
            }

            // 当arr2的指针已经为到末尾,直接将arr1中的数组添加到数组当中
            if (j == arr2.length) {
                for (int index = i; index < arr1.length; index++) {
                    newArr[count] = arr1[index];
                    count++;
                }
            }
        }
        return Arrays.copyOfRange(newArr, 0, count);
    }
}

 时间复杂度:O(n+m)

空间复杂度:O(n+m)


方式二:利用TreeSet的形式实现;(步骤比较容易,就不写了)

import org.junit.Test;
import java.util.Set;
import java.util.TreeSet;

/**
 * @author zhangyu
 * @version V1.0
 * @ClassName: MergeArray2
 * @Description: TOTO
 * @date 2018/11/21 20:41
 **/

public class MergeArray2 {
    @Test
    public void fun() {
        int[] arr1 = {1, 2, 3};
        int[] arr2 = {3, 4, 5, 6, 7};
        int[] newArr = merge(arr1, arr2);
        for (int number : newArr) {
            System.out.print(number + " ");
        }
    }
    private int[] merge(int[] arr1, int[] arr2) {
        if (arr1 == null || arr1.length == 0) {
            return arr2;
        }
        if (arr2 == null || arr2.length == 0) {
            return arr1;
        }
        Set<Integer> set = new TreeSet();
        for (int arrNnum:arr1) {
            set.add(arrNnum);
        }
        for (int arr2Num:arr2) {
            set.add(arr2Num);
        }
        int[] newArr = new int[set.size()];
        int count = 0;
        for (int number : set) {
            newArr[count] = number;
            count++;
        }
        return newArr;
    }
}

时间复杂度:O(n+m)

空间复杂度:O((n+m)*2)

猜你喜欢

转载自blog.csdn.net/zy345293721/article/details/84330072