简体   繁体   English

合并c中的两个排序数组

[英]Merging two sorted arrays in c

Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. 给定两个已排序的整数数组nums1和nums2,将nums2合并为nums1作为一个已排序的数组。 my solution is not working can anyone help 我的解决方案无法正常工作,任何人都可以帮忙

enter code here 
void merge(int* nums1, int m, int* nums2, int n) {
int i=0,j=0,k=0;
while(i<m&&j<n)
{
    if(nums1[i]<nums2[j])
    {
        nums1[k]=nums1[i];
        i++;
        k++;
    }
    else if (nums1[i]>nums2[j])
    {
        nums1[k]=nums2[j];
        k++;
        j++;
    }
    else
    {nums1[k]=nums1[i];
        k++;i++;j++;}
}

while(i!=m)
{  
    nums1[k]=nums1[i];
      k++,i++;


}
while(j!=n)
{
    nums1[k]=nums2[j];
      k++,j++;

}

return nums1;

} }

compiler is saying output is wrong.. its a question on leetcode 编译器说输出是错误的..关于leetcode的问题

The question is that number in nums2 will replace nums1's number when nums2' number is larger, and this will modify numbers in nums1. 问题是,当nums2的数字较大时,nums2中的数字将替换nums1的数字,这将修改nums1中的数字。 You can use another buffer to store the result instead of using nums1, or merge them from the end of both. 您可以使用另一个缓冲区来存储结果,而不是使用nums1,或从两者的末尾合并它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM