-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMerge2Arrays.java
More file actions
26 lines (23 loc) · 749 Bytes
/
Copy pathMerge2Arrays.java
File metadata and controls
26 lines (23 loc) · 749 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
Ðåàëèçóéòå ìåòîä, ñëèâàþùèé äâà îòñîðòèðîâàííûõ ïî íåóáûâàíèþ ìàññèâà ÷èñåë â îäèí îòñîðòèðîâàííûé ìàññèâ.
Âîñïîëüçóéòåñü ïðåäîñòàâëåííûì øàáëîíîì. Äåêëàðàöèþ êëàññà, ìåòîä main è îáðàáîòêó ââîäà-âûâîäà äîáàâèò ïðîâåðÿþùàÿ ñèñòåìà.
Sample Input:
0 2
1 3
Sample Output:
0 1 2 3
*/
/**
* Merges two given sorted arrays into one
*
* @param a1 first sorted array
* @param a2 second sorted array
* @return new array containing all elements from a1 and a2, sorted
*/
public static int[] mergeArrays(int[] a1, int[] a2) {
int[] result = new int[a1.length + a2.length];
System.arraycopy(a1, 0, result, 0, a1.length);
System.arraycopy(a2, 0, result, a1.length, a2.length);
Arrays.sort(result);
return result;
}