原创转载请注明出处:http://agilestyle.iteye.com/blog/2361133
Question
Merge Two Sorted Arrays into the larger array, given the large array has extra storage. Merge without additional memory
Requirements
Long sorted array {1,3,5,0,0,0} (0 means blank spaces), so used length is 3
The short sorted array {2,4}
The expected merged array will be {1,2,3,4,5,0}
Additionally, no additional memory is used
- e.g. declaring a new array is not allowed
Solution
1. Loop from end to beginning.
2. Keep track of two tail index values of two merged arrays.
3. Final-merged-tail position is equal to longTail+ShortTail+1 (both index start from 0!!)
4. If the shortTail is larger than or equal to 0 after merging, adding the remaining values to the merged array
package org.fool.java.test; public class MergeTwoSortedArrays { public static void main(String[] args) { int[] longArray = {1, 3, 5, 7, 0, 0, 0, 0}; int used = 4; int[] shortArray = {2, 4, 6}; for (int i : longArray) { System.out.print(i + " "); } System.out.println(); merge(longArray, shortArray, used); for (int i : longArray) { System.out.print(i + " "); } } // we passed 3 arguments, the first 2 are the 2 arrays // the longUsed is to present how many items are used in the longArray // This method returns a int value representing how many valid items to be counted in the merged long array private static int merge(int[] longArray, int[] shortArray, int longUsed) { int longTail = longUsed - 1; int shortTail = shortArray.length - 1; // the order is to merge from end to beginning while (longTail >= 0 && shortTail >= 0) { // check which one in the two arrays should be put at this current tail position if (longArray[longTail] > shortArray[shortTail]) { // which means at this position, we'd better to put the value int the longArray for the merged array longArray[longTail + shortTail + 1] = longArray[longTail]; // notice the key here for the final index in the merged array // longTail + shortTail + 1 will be the final index, thinking that both tail indexes start from 0 longTail--; // after we put one value in longArray, we need to shift our focus to the left a little } else { longArray[longTail + shortTail + 1] = shortArray[shortTail]; shortTail--; } } // case 1, longTail not equal to 0? No problem, they are in position already // case 2, shortTail not equal to 0? Need to add element one by one to the final array while (shortTail >= 0) { longArray[shortTail] = shortArray[shortTail]; // notice the final merging is same as copying shortTail--; // do not forget updating the index } // return how many used elements in the new merged array return shortArray.length + longUsed; } }
Console Output
Reference
https://www.youtube.com/watch?v=zYrlYlwkbLo&list=PLlhDxqlV_-vkak9feCSrnjlrnzzzcopSG&index=40
相关推荐
4. Median of Two Sorted Arrays 7. Reverse Integer 9. Palindrome Number 11. Container With Most Water 13. Roman to Integer 15. 3Sum 16. 3Sum Closest 17. Letter Combinations of a Phone Number 18. 4Sum ...
LeetCode Merge 2 Sorted Lists解决方案详解 Merge 2 Sorted Lists是LeetCode上的一个经典算法题目,旨在考察程序员对链表的操作和排序算法的掌握程度。下面对该题目的解决方案进行详细的分析和解释。 问题描述 ...
Merge k Sorted Lists 给定k个有序数组并将其合并成1个有序数组, 代码采用C#编写
主要是两步,先把数组分成两半(merge),再把这两半合并成有序的数组(mergeTwoSortedArrays)。 temp参数的作用是接收排序后的数组,然后再把值一一付给原数组,如果不加这个参数,我们就需要重复开辟空间,或者...
python python_leetcode题解之088_Merge_Sorted_Array
java入门 java_leetcode题解之088_Merge_Sorted_Array
javascript js_leetcode题解之88-merge-sorted-array.js
c语言基础 c语言_leetcode题解之0088_merge_sorted_array.zip
c c语言_leetcode 0021_merge_two_sorted_lists.zip
js js_leetcode题解之21-merge-two-sorted-lists.js
c语言入门 C语言_leetcode题解之21-merge-two-sorted-lists.c
Merge two lists of ordered numbers
Merge Sorted Array 合并 排序 数组 leetcode
Without Repeating Characters 4. Median of Two Sorted Arrays 5. Longest Palindromic Substring 7. Reverse Integer 9. Palindrome Number 11. Container With Most Water 12. Integer to Roman 13. Roman to ...
Without Repeating Characters 中等 字串 重要 Median of Two Sorted Arrays 困难 数学 Longest Palindromic Substring 中等 回文 ZigZag Conversion 中等 矩阵 重要 Reverse Integer 简单 字串 String to Integer ...
标题中的"two-phase-merge_sort-.rar_2phase merge sort_merge_sort_two merge"指的是一个采用两阶段归并排序算法的程序或文档集合。这个算法是针对大数据量、无法一次性加载到内存中的情况设计的,常见于外部排序...
7 Median of Two Sorted Arrays 33 8 Kth Largest Element in an Array 35 9 Wildcard Matching 37 10 Regular Expression Matching in Java 39 11 Merge Intervals 43 12 Insert Interval 45 13 Two Sum 47 14 Two ...
Without Repeating Characters 无重复字符的最长子串 string 4 LMedian of Two Sorted Arrays 两个排序数组的中位数 ary,binary search,dive and conquer 5 Longest Palindromic Substring 最长回文子串 string,dp 8...
Without Repeating Characters 4.Median of Two Sorted Arrays 7.Reverse Integer 8.String to Integer (atoi) 9.Palindrome Number 11.Container With Most Water 14.Longest Common Prefix 15.3Sum 16.3Sum ...