My java solution - Not use new array && Not use new variable


  • 1
    S
    public class Solution {
        public void merge(int A[], int m, int B[], int n) {
            // try not use new variable
            //try include exception in normal control
            // try not minus m, n
            while(m+n>0){
                if(m<1){
                    A[m + n-1] = B[(n--)-1];
                }else if(n<1){
                    return;
                }else if( A[m-1]<= B[n-1]){
                    A[m + n-1] = B[(n--)-1];
                }else{
                    A[m + n-1] = A[(m--)-1];
                }
            }
        }
    }

  • 4
    S
    public class Solution {
        public void merge(int A[], int m, int B[], int n) {
    
            while(m+n>0 && n != 0){
                A[m+n -1] = (m<1 || A[m-1]<= B[n-1])?B[(n--) -1]:A[(m--) -1];
            }
        }
    }

  • 0
    T

    This idea is right , but the expression has someting wrong. eg. when the expression"B[(n--)-1]" is executed over, the value of "m+n-1" in the left side of equation will be changed as the value of n's change.....


Log in to reply
 

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