zoukankan      html  css  js  c++  java
  • LeetCode 88

    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 /*************************************************************************
     2     > File Name: LeetCode088.c
     3     > Author: Juntaran
     4     > Mail: JuntaranMail@gmail.com
     5     > Created Time: Tue 17 May 2016 15:28:11 PM CST
     6  ************************************************************************/
     7 
     8 /*************************************************************************
     9     
    10     Merge Sorted Array
    11     
    12     Given two sorted integer arrays nums1 and nums2, 
    13     merge nums2 into nums1 as one sorted array.
    14 
    15     Note:
    16     You may assume that nums1 has enough space 
    17     (size that is greater or equal to m + n) 
    18     to hold additional elements from nums2. 
    19     The number of elements initialized in nums1 and nums2 
    20     are m and n respectively.
    21 
    22  ************************************************************************/
    23  
    24  #include <stdio.h>
    25 
    26 void merge(int* nums1, int m, int* nums2, int n)
    27 {
    28     int i = m - 1;
    29     int j = n - 1;
    30 
    31     while( i>=0 && j>=0 )
    32     {
    33         if( nums1[i] > nums2[j] )
    34         {
    35             nums1[i+j+1] = nums1[i];
    36             i --;
    37         }
    38         else
    39         {
    40             nums1[i+j+1] = nums2[j];
    41             j --;
    42         }
    43     }
    44     while( j >= 0 )
    45     {
    46         nums1[i+j+1] = nums2[j];
    47         j --;
    48     }
    49 }
  • 相关阅读:
    Codeforces Round #249 (Div. 2) D. Special Grid 枚举
    图论二
    C语言中的atan和atan2(转)
    BestCoder Round #79 (div.2)
    数学
    LCA
    二分图
    动态规划
    线段树
    树状数组
  • 原文地址:https://www.cnblogs.com/Juntaran/p/5511677.html
Copyright © 2011-2022 走看看