• leetcode--Merge Sorted Array


    Given two sorted integer arrays A and B, merge B into A as one sorted array.

    Note:
    You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B are mand n respectively.

    public class Solution {
        /**Start merging process from tail of arrays to front of array.<br>
    	 * 
    	 * @param A --one array with has larger size
    	 * @param m --number of elements in array A
    	 * @param B --the other array with smaller size
    	 * @param n --number of elements in array B
    	 * @author Averill Zheng
    	 * @version 2014-06-03
    	 * @since JDK 1.7
    	 */
        public void merge(int A[], int m, int B[], int n) {
            int i = 0, j = 0;
    		    while(i < m && j < n){
            	    if(A[m - 1 - i] > B[n - 1 - j]){
            		    A[m + n - 1 -(i + j)] = A[m - 1 - i];
            		    ++i;
            	    }
            	    else{
            		    A[m + n - 1 -(i + j)] = B[n - 1 - j];
            		    ++j;
            	    }
                }
    		
    		while(j < n){
    			A[n - 1 - j] = B[n - 1 - j];
        		++j;
    		}    
        }
    }
    

      

  • 相关阅读:
    2.17-2.23第一周总结
    10号总结
    9日总结
    8号总结
    7号寒假总结
    6号
    读后感《程序员的修炼之道:从小工到专家》1
    java第二次动手动脑
    回文判断
    二进制的原码,反码以及补码介绍
  • 原文地址:https://www.cnblogs.com/averillzheng/p/3769681.html
Copyright © 2020-2023  润新知