当前位置:   article > 正文

LeetCode@Array_88_MergeSortedArray

LeetCode@Array_88_MergeSortedArray

Problem Discription

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.

Java Programming

  1. public class Solution
  2. {
  3. public void merge(int[] nums1, int m, int[] nums2, int n)
  4. {
  5. int p1 = m - 1;
  6. int p2 = n - 1;
  7. int curr = m + n - 1;
  8. while (p1 > -1 && p2 > -1)
  9. {
  10. nums1[curr--] = nums1[p1] > nums2[p2] ? nums1[p1--] : nums2[p2--];
  11. }
  12. while (p2 > -1)
  13. {
  14. nums1[curr--] = nums2[p2--];
  15. }
  16. }
  17. }


Similar Link: 



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/code123/article/detail/62924
推荐阅读
相关标签
  

闽ICP备14008679号