OJ练习20——T88 Merge sorted array

把有序序列B合并到有序序列A中,A的大小大于m+n。

特别的地方在于,不是返回一个新数组。

【思路】

可以开一个local数组,归并两串,再把结果赋给A。但不是很喜欢这种做法。(因为太简单了)

插入排序是把当前元素插入到前面已经有序的m个元素中,与题目环境很符合。

【other code】

void merge(int A[], int m, int B[], int n) {
        int i,j;
        if(m==0)
            for(i=0; i<n; i++)
                A[i]=B[i];
        for(j=0; j<n; j++)
        {
            for(i=m-1+j; i>=0; i--)
            {
                if(A[i]>B[j])
                    A[i+1]=A[i];
                else break;

            }
            A[i+1]=B[j];
        }
    }

【总结】

插入排序是从后向前遍历,这是我总是不能掌握的地方_(:зゝ∠)_

时间: 2024-10-13 14:19:47

OJ练习20——T88 Merge sorted array的相关文章

LeetCode OJ - Merge Sorted Array

原地归并. 下面是AC代码: 1 public void merge(int A[], int m, int B[], int n) { 2 3 int len = A.length; 4 //first copy m elements of A to the end of A 5 for(int i=m-1,j = len-1;i>=0;i--){ 6 A[j--] = A[i]; 7 } 8 //merge the A and B 9 int startA = len - m; 10 int

&lt;LeetCode OJ&gt; Merge Sorted Array【88】

88. Merge Sorted Array My Submissions Question Total Accepted: 80830 Total Submissions: 274120 Difficulty: Easy Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: You may assume that nums1 has enough sp

【leetcode】Merge Sorted Array

Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialize

43. Merge Sorted Array &amp;&amp; LRU Cache

Merge Sorted Array OJ: https://oj.leetcode.com/problems/merge-sorted-array/ Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume that A has enough space (size that is greater or equal to m + n) to hold add

算法5--排序--Merge Sorted Array

之前几天在忙其他的事情,没有时间更新,今天更新了几个,虽然有几个SMR的博客暂时没有开放,已经写好了,以后会慢慢开放的 今天再更新一个有关排序的算法题 1  Merge Sorted Array描述Given two sorted integer arrays A and B, merge B into A as one sorted array.Note: You may assume that A has enough space to hold additional elements fr

LeetCode——Merge Sorted Array

Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B are m

归并有序数组--Merge Sorted Array

https://leetcode.com/problems/merge-sorted-array/ 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)

88. Merge Sorted Array【leetcode】算法,java将两个有序数组合并到一个数组中

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 n

No.88 Merge Sorted Array

No.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