Merge Sorted Array leetcode java(回顾MergeTwoArray和MergeTwoLinkedList)

题目

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 and n respectively.

题解:

这道题是说让B merge到 A 里面。

先复习下原本我们在MergeSort里面怎么利用一个新建的数量来merge two array:

代码如下:

1 public int[] mergeTwoList(int[] A, int[] B) {
 2     int[] C = new int[A.length + B.length];
 3     int k = 0;
 4     int i = 0;
 5     int j = 0;
 6     while(i < A.length && j < B.length) {
 7         if (A[i] < B[j])
 8             C[k++] = A[i++];
 9         else
10             C[k++] = B[j++];
11     }
12     while (i < A.length) 
13         C[k++] = A[i++];
14     while (j < B.length) 
15         C[k++] = B[j++];
16     return C;
17 }

然后我们再顺便复习下,怎么merge two linked list,代码如下:

1     public ListNode mergeTwoLists(ListNode leftlist, ListNode rightlist){
 2         if(rightlist == null)
 3             return leftlist;
 4         if(leftlist == null)
 5             return rightlist;
 6         
 7         ListNode fakehead = new ListNode(-1);
 8         ListNode ptr = fakehead;
 9         while(rightlist!=null&&leftlist!=null){
10             if(rightlist.val<leftlist.val){
11                 ptr.next = rightlist;
12                 ptr = ptr.next;
13                 rightlist = rightlist.next;
14             }else{
15                 ptr.next = leftlist;
16                 ptr = ptr.next;
17                 leftlist = leftlist.next;
18             }
19         }
20         
21         if(rightlist!=null)
22             ptr.next = rightlist;
23         if(leftlist!=null)
24             ptr.next = leftlist;
25         
26         return fakehead.next;
27     }

可以看出merge的思路都是在从头比较两个list的value,用两个指针分别指向当前要比较的node上面。而且最后都会处理下剩下的元素。

而这道题是不能借助一个新的array的,那么我们就不好从前往后比了(不好插入位置)。方便的方法是从后往前比,然后最后处理剩下的元素。

代码如下:

public void merge(int A[], int m, int B[], int n) {
        while(m > 0 && n > 0){
            if(A[m-1] > B[n-1]){
                A[m+n-1] = A[m-1];
                m--;
            }else{
                A[m+n-1] = B[n-1];
                n--;
            }
        }
 
        while(n > 0){
            A[m+n-1] = B[n-1];
            n--;
        }
    }

Merge Sorted Array leetcode java(回顾MergeTwoArray和MergeTwoLinkedList)

时间: 2024-10-09 10:57:12

Merge Sorted Array leetcode java(回顾MergeTwoArray和MergeTwoLinkedList)的相关文章

Median of Two Sorted Array leetcode java

题目: There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 题解: 首先我们先明确什么是median,即中位数. 引用Wikipedia对中位数的定义: 计算有限个数的数据的中位数的方法是:把所有的同类数据按照大小的顺序排列

Search in Rotated Sorted Array leetcode java

题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no du

Merge Sorted Array &lt;leetcode&gt;

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 

Find Minimum in Rotated Sorted Array leetcode java

题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). Find the minimum element. You may assume no duplicate exists in the array. 解题思路: 首先假设一个sorted没有rotated的数组[1,2,3],假设我们通过一个

Remove Duplicates From Sorted Array leetcode java

算法描述: Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example,Given input array

LeetCode算法题-Merge Sorted Array(Java实现)

这是悦乐书的第161次更新,第163篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第20题(顺位题号是88).给定两个排序的整数数组nums1和nums2,将nums2中的元素合并到nums1中,并且作为一个排序的数组.在nums1和nums2中初始化的元素个数分别为m和n.假设nums1有足够的空间(大于或等于m + n)来保存nums2中的其他元素.例如: 输入:nums1 = [1,2,3,0,0,0],m = 3,nums2 = [2,5,6],n = 3

[lintcode easy]Merge Sorted Array II

Merge Sorted Array II Merge two given sorted integer array A and B into a new sorted integer array. Example A=[1,2,3,4] B=[2,4,5,6] return [1,2,2,3,4,4,5,6] Challenge How can you optimize your algorithm if one array is very large and the other is ver

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

LeetCode --- 88. 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 init