华为OD机试 2024E卷题库疯狂收录中,刷题 <a rel="nofollow" href="https://blog.csdn.net/qq_40374604/category_12822407.html" style="color: red;">点这里</a>。
实战项目访问:http://javapub.net.cn/
专栏导读
本专栏收录于 《华为OD机试(JAVA)真题(E卷+D卷+A卷+B卷+C卷)》 。
刷的越多,抽中的概率越大,私信javapub,备注华为OD,加入华为OD刷题交流群,每一题都有详细的答题思路、详细的代码注释、3个测试用例、为什么这道题采用XX算法、XX算法的适用场景,发现新题目,随时更新,全天CSDN在线答疑。
整型数组合并
描述
题目标题:将两个整型数组按照升序合并,并且过滤掉重复数组元素。
输出时相邻两数之间没有空格。
输入描述:
输入说明,按下列顺序输入:
1 输入第一个数组的个数 2 输入第一个数组的数值 3 输入第二个数组的个数 4 输入第二个数组的数值
输出描述:
输出合并之后的数组
示例1
输入:
3
1 2 5
4
-1 0 3 2
输出:
-101235
Java 编程
package cn.net.javapub.demo2.demo;
/**
* @author: shiyuwang
* @url: http://javapub.net.cn
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = br.readLine()) != null && line.length() > 0) {
String[] strs = br.readLine().split(" ");
int[] array1 = new int[strs.length];
for (int i = 0; i < array1.length; ++i)
array1[i] = Integer.parseInt(strs[i]);
line = br.readLine();
strs = br.readLine().split(" ");
int[] array2 = new int[strs.length];
for (int i = 0; i < array2.length; ++i)
array2[i] = Integer.parseInt(strs[i]);
System.out.println(combineBySort(array1, array2));
}
}
static String combineBySort(int[] array1, int[] array2) {
int[] outPut = new int[array1.length + array2.length];
Arrays.sort(array1);
Arrays.sort(array2);
int M = array1.length, R = array2.length;
int idx = 0, i = 0, j = 0;
if (array1[i] > array2[j]) {
outPut[idx++] = array2[j++];
} else if (array1[i] < array2[j]) {
outPut[idx++] = array1[i++];
} else {
outPut[idx++] = array1[i++];
j++;
}
while (i < M && j < R) {
if (array1[i] > array2[j]) {
if (outPut[idx - 1] != array2[j])
outPut[idx++] = array2[j];
++j;
} else if (array1[i] < array2[j]) {
if (outPut[idx - 1] != array1[i])
outPut[idx++] = array1[i];
++i;
} else {
if (outPut[idx - 1] != array1[i])
outPut[idx++] = array1[i];
++i;
++j;
}
}
if (i == M) {
while (j < R) {
if (outPut[idx - 1] != array2[j])//去重
outPut[idx++] = array2[j];
j++;
}
} else {
for (; i < M; ++i)
if (outPut[idx - 1] != array1[i])
outPut[idx++] = array1[i];
}
StringBuilder sb = new StringBuilder();
for (i = 0; i < idx; ++i)
sb.append(outPut[i]);
return sb.toString();
}
}
展示效果:

🏆下一篇: 华为OD机试 - 火车进站 (Java 2024 E卷 100分) 🏆本文收录于, 搬砖工逆袭Java架构师
刷的越多,抽中的概率越大,私信javapub,备注华为OD,加入华为OD刷题交流群,每一题都有详细的答题思路、详细的代码注释、3个测试用例、为什么这道题采用XX算法、XX算法的适用场景,发现新题目,随时更新,全天CSDN在线答疑。
优质合集推荐
《突击面试》Java面试题合集
《面试1v1》
《编程工作总结》

















