Copyright notice: reproduced, please indicate the source of downstream https://blog.csdn.net/Hollake/article/details/90902367
Found a lot of articles and found that the code is wrong, it does not achieve the purpose of de-duplication, but also the people thumbs up, look at the code you do not look for their own measure what is wrong with you? Here is the code I wrote the code looks long, it may be a bit redundant, but it is easy to understand, if there is wrong or can be optimized, could you point it out, you can learn together, thank you.
Topics requirements: the array A, B orderly integrating, A, B, and removing duplicate elements.
The following code implementation complexity, the length of the array A and B provided for the M and N that time complexity of O (M + N), if implemented with the array, the space complexity is also O (M + N)
public class Solution {
public static void main(String[] args) {
int[] A = {1,2,3,4,5};
int[] B = {1,2,2,3,4,5,5,6,7,7};
int[] result = Solution.multiply(A, B);
System.out.println(Arrays.toString(result));
}
public static int[] multiply(int[] A,int[] B) {
if (A == null && B == null || A.length < 1 && B.length < 1) {
return null;
}
// 创建辅助集合,其实数组创建新的数组也行,new int[A.length+B.length]
List<Integer> list = new ArrayList<>();
int index = 0, p1 = 0, p2 = 0;
// 先将A或者B中的首个元素添加到list中
list.add(A[p1++]);
// 当没有遍历完A和B中任意一个数组时执行
while ( p1 < A.length && p2 < B.length ) {
// 拿到当前指针的最小值
int tmp = A[p1] < B[p2] ? A[p1++] : B[p2++];
// 判断list中是否已经添加了此元素
if (tmp > list.get(index)) {
list.add(tmp);
// 每次添加元素后指针后移
index++;
}
}
// 当B中元素添加完,只剩A中元素时
while ( p1 < A.length ) {
int tmp = A[p1++];
if (tmp > list.get(index)) {
list.add(tmp);
index++;
}
}
// 当A中元素添加完,只剩B中元素时
while ( p2 < B.length ) {
int tmp = B[p2++];
if (tmp > list.get(index)) {
list.add(tmp);
index++;
}
}
// 将list中元素重新移回到数组,如果刚开始创建的是数组,那么就不用这一步了
int[] result = new int[list.size()];
for (int i = 0; i < list.size(); i++) {
result[i] = list.get(i);
}
return result;
}
}