zoukankan      html  css  js  c++  java
  • Merge Sorted Array

    package cn.edu.xidian.sselab.array;

    /**
     *
     * @author zhiyong wang
     * title: Merge Sorted Array
     * content:
     *            Given two sorted integer arrays nums1 and nums2,
     *            merge nums2 into nums1 as one sorted array.
     *    Note:
     *        You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2.
     *        The number of elements initialized in nums1 and nums2 are m and n respectively.
     *
     */
    public class MergeSortedArray {

        //从后面往前面来进行比较,这样就不会产生数据不断移动的困扰
        //因为是往nums1来插入数据,如果nums2比较完了,nums1还有剩余,就不用管了,不用再进行一遍赋值操作
        //这个题就是数据结构的第一个例题
        public void merge(int[] nums1, int m, int[] nums2, int n){
            int aIndex = m-1;
            int bIndex = n-1;
            int index = m + n - 1;
            while(aIndex >= 0 && bIndex >= 0){
                if(nums1[aIndex] > nums2[bIndex]){
                    nums1[index--] = nums1[aIndex--];
                }else{
                    nums1[index--] = nums2[bIndex--];
                }
            }
            while(bIndex >= 0){
                nums1[index--] = nums2[bIndex--];
            }        
        }
    }

  • 相关阅读:
    Unity热更新06-XLua热补丁-05
    Unity热更新06-XLua热补丁-04
    Unity热更新06-XLua热补丁-03
    Unity热更新06-XLua热补丁-02
    Unity热更新06-XLua热补丁-01
    [SQLite][database disk image is malformed]数据库恢复
    VMware 安装MAC
    c#编写的服务中访问网络位置的共享文件夹
    Android Socket发送信息时闪退
    一个简单的Log类
  • 原文地址:https://www.cnblogs.com/wzyxidian/p/5064953.html
Copyright © 2011-2022 走看看