-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeArray.java
More file actions
57 lines (52 loc) · 1.44 KB
/
Copy pathMergeArray.java
File metadata and controls
57 lines (52 loc) · 1.44 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
package AlgorithmTest;
/*
* 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组。
* 初始化 nums1 和 nums2 的元素数量分别为 m 和 n。
* 你可以假设 nums1 有足够的空间(空间大小大于或等于 m + n)来保存 nums2 中的元素。
*
* */
public class MergeArray {
public static void main(String[] args) {
int nums1[]={
1
};
int nums2[]={
2
};
merge(nums1,1,nums2,1);
}
public static void merge(int[] nums1, int m, int[] nums2, int n){
String result="";
int i=0;
int j=0;
for(;i<m;){
for(;j<n;){
if(nums1[i]<=nums2[j])
{
result+=nums1[i];
i++;
break;
}else{
result+=nums2[j];
j++;
}
}
}
if(i<m){
for(int p=j;p<n;p++){
result+=nums1[p];
}
}
if(j<n){
for(int q=j;q<n;q++){
result+=nums2[q];
}
}
char [] charArray=result.toCharArray();
String resultT="";
for (int w = 0; w < charArray.length; w++) {
resultT+=charArray[w]+",";
}
System.out.println("["+resultT.substring(0,resultT.length()-1)+"]");
}
}