My Java solution2


  • 0
    C
     public class Solution {
     public void merge(int[] nums1, int m, int[] nums2, int n) {
    	int p0=0,p1=0,p2=0;
    	while((p1<m)&&(p2<n))
    	{
    		if(nums1[p1]<=nums2[p2])
    		{
    			nums1[p0++]=nums1[p1++];
    		}
    		else
    		{	movebackward(nums1,p1,m);
    			nums1[p0++]=nums2[p2++];
    			m++;
    			p1++;
    		}
    	}
    	if(p2<n)
    	{	while(p2<n)
    		{
    			nums1[p0++]=nums2[p2++];
    		}
    	}
    }
         private void movebackward(int[] nums1, int p1,int m) {
    	// TODO Auto-generated method stub
    	for(int i=m;i>p1;i--)
    	{
    		nums1[i]=nums1[i-1];
    	}
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.