-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d92a700
commit 6a84d4a
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package TwoPointers; | ||
/** | ||
* | ||
* @author chenqun | ||
* Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. | ||
* | ||
*/ | ||
//´ÓºóÍùÇ°¿ªÊ¼ºÏ²¢ | ||
public class MergeSortedArray { | ||
|
||
public void merge(int[] nums1, int m, int[] nums2, int n){ | ||
int end1 = m-1; | ||
int end = m + n -1; | ||
for(int end2=n-1; end2>=0; end2--){ | ||
if(end1 < 0 || nums2[end2] > nums1[end1]){ | ||
nums1[end--] = nums2[end2]; | ||
}else{ | ||
nums1[end--] = nums1[end1]; | ||
end1 --; | ||
end2 ++; | ||
} | ||
} | ||
} | ||
public static void main(String[] args) { | ||
// TODO Auto-generated method stub | ||
int[] nums1 = new int[2]; | ||
nums1[0] = 2; | ||
nums1[1] = 0; | ||
int[] nums2 = {1}; | ||
MergeSortedArray test = new MergeSortedArray(); | ||
test.merge(nums1, 1, nums2, 1); | ||
} | ||
|
||
} |