Skip to content

Commit

Permalink
MergeSortedArray.java
Browse files Browse the repository at this point in the history
  • Loading branch information
terminator123 committed Jan 12, 2016
1 parent d92a700 commit 6a84d4a
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/MergeSortedArray.java
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);
}

}

0 comments on commit 6a84d4a

Please sign in to comment.