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

    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.

     1 void merge(int* nums1, int m, int* nums2, int n) 
     2 {
     3      int i=0;
     4     if(m==0&&n!=0)
     5     {
     6         while(i<n)
     7         {
     8             nums1[i]=nums2[i];
     9             i++;
    10         }
    11         return ;
    12     }
    13     if(n==0)
    14         return ;
    15     
    16     int *TempArray;
    17 
    18     TempArray=(int*)malloc(sizeof(int)*(m+n));
    19 
    20     int L=0;
    21     int k1=0,k2=0;
    22     while(k1<m&&k2<n)
    23     {
    24         if(nums1[k1]>=nums2[k2])
    25         {
    26             TempArray[L++]=nums2[k2++];
    27         }
    28         else
    29         {
    30             TempArray[L++]=nums1[k1++];
    31         }
    32     }
    33 
    34 
    35     if(k1==m)
    36     {
    37         while(k2<n)
    38         {
    39             TempArray[L++]=nums2[k2++];
    40         }
    41     }
    42 
    43     if(k2==n)
    44     {
    45         while(k1<m)
    46         {
    47             TempArray[L++]=nums1[k1++];
    48         }
    49     }
    50 
    51 
    52 
    53     for(i=0;i<m+n;i++)
    54     {
    55         nums1[i]=TempArray[i];
    56     }
    57 
    58     free(TempArray);
    59 
    60 }
  • 相关阅读:
    线程池
    多线程随笔
    注解随笔
    反射机制
    iO流
    FastDFS+docker建立分布式文件系统
    Java之Exception
    Java之String
    手写SpringMvc
    spring中一些常用注解的含义
  • 原文地址:https://www.cnblogs.com/vpoet/p/4660537.html
Copyright © 2011-2022 走看看