-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution88.java
More file actions
44 lines (41 loc) · 1.07 KB
/
Copy pathSolution88.java
File metadata and controls
44 lines (41 loc) · 1.07 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
/**
* @Title: Solution88.java——
* @Package EasyCode
* @Description: TODO
* @author msdumin@gmail.com
* @date 2019年3月15日 上午10:23:47
* @version V1.0
*/
package EasyCode;
/**
* @ClassName: Solution88——Sorted Array给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组。
* @Description: TODO
* @author msdumin@gmail.com
* @date 2019年3月15日 上午10:23:47
*/
public class Solution88 {
public static void merge(int[] nums1, int m, int[] nums2, int n) {
//归并排序的merge过程
int i = m - 1,j = n - 1;
int index = m+n-1;
while(index >= 0){
if(i < 0){
nums1[index--] = nums2[j--];
}else if(j < 0){
nums1[index--] = nums1[i--];
}else if(nums1[i] > nums2[j] && i>=0)
nums1[index--] = nums1[i--];
else if(nums1[i] <= nums2[j] && j>=0)
nums1[index--] = nums2[j--];
}
}
public static void main(String[] args) {
int[] nums1 = {1,2,3,0,0,0};
int m = nums1.length;
int[] nums2 = {2,5,6};
int n = nums2.length;
merge(nums1, m, nums2, n);
for(int i : nums1)
System.out.println(i);
}
}