What simple codes in Java, can you make it more simple?


  • 0
    F
    public void merge(int A[], int m, int B[], int n) {//from A[m + n - 1] ,copy the bigger from a[i] and b[k] in sequence
    		int j = m - 1, k = n - 1 ,i = m < 1 ? n - 1 : m + n - 1;
    		for(; i >= 0 && j >= 0 && k >= 0; i--)
    			A[i] = A[j] > B[k] ? A[j--] : B[k--];
    		while(k >= 0) A[i--] = B[k--];
        }

  • 0
    T

    public class Solution {
    public static void merge(int A[], int m, int B[], int n) {
    int A_index = m-1,B_index = n-1 ;
    while(A_index >= 0 && B_index >=0)
    A[A_index+B_index+1] = A[A_index] > B[B_index] ? A[A_index--] : B[B_index--];
    while(B_index >= 0)
    A[(A_index+B_index+1)] = B[B_index--];
    }
    }


Log in to reply
 

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