-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
58 lines (43 loc) · 1.29 KB
/
Solution.java
File metadata and controls
58 lines (43 loc) · 1.29 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package leetcode.mergeSortedArray;
import java.util.Arrays;
public class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int l = 0;
int j = 0;
int[] newArray = new int[m+n];
for(int i = 0; i < m+n; i++){
if(l < m && j < n){
if(nums1[l] < nums2[j]){
newArray[i] = nums1[l];
l++;
} else {
newArray[i] = nums2[j];
j++;
}
} else if ( l < m){
newArray[i] = nums1[l];
l++;
} else {
newArray[i] = nums2[j];
j++;
}
}
for(int i = 0; i < m+n; i++){
nums1[i] = newArray[i];
}
}
public static void main(String[] args) {
Solution s = new Solution();
int[] nums1 = {1};
int[] nums2 = {};
int[] resultExpected = {1};
s.merge(nums1,1, nums2, 0);
System.out.println("RESULT EXPECTED: " + resultExpected);
System.out.println("RESULT: " + nums1 );
if(Arrays.equals(nums1, resultExpected)){
System.out.println("Correct");
} else {
System.out.println("Incorrect");
}
}
}