问题描述:
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 andnums2 are m and n respectively.
原问题链接:https://leetcode.com/problems/merge-sorted-array/
问题分析
这个问题相对比较简单,也比较容易找到思路。对于这两个数组来说,一个数组的实际长度可能远超过它们两个数组里有效元素个数的和。假设数组1里元素个数为m,数组2里元素个数为n,可以从数组1里的m + n -1这个位置开始,从后往前去处理。
我们取3个变量,分别为i, l, r。其中i表示最终在数组里当前位置所放置元素的索引,l表示数组1中间最后一个元素的位置,r表示数组2中间最后一个元素的位置。每次比较l, r的值,将大的那个放入到i的位置。在详细的实现中还需要考虑如果一个数组遍历完的情况。
详细的代码实现如下:
public class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { for(int i = m + n - 1, l = m - 1, r = n - 1; i >= 0; i--) { if(l >= 0 && r >= 0) { if(nums1[l] < nums2[r]) nums1[i] = nums2[r--]; else nums1[i] = nums1[l--]; } else if(r < 0) return; else if(l < 0) nums1[i] = nums2[r--]; } } }
相关推荐
Merge Sorted Array 合并 排序 数组 leetcode
java lru leetcode :ice_cream: LeetCode Kindem 的个人 LeetCode 题解仓库,欢迎交流学习。...Array 48 Rotate Image 53 Maximum Subarray 55 Jump Game 56 Merge Intervals 64 Minimum Path Sum 73
leetcode写题闪退 #*的多少代表此题的有意思程度 有几题第一次写的时候思绪比较混乱: *****Regular Expression Matching 2014.10.29 对于Find Minimum in Rotated Sorted Array II 和 Find Minimum in Rotated ...
Merge Two Sorted Lists Easy #26 Remove Duplicates from Sorted Array Easy #27 Remove Element Easy #35 Search Insert Position Easy #38 Count and Say Easy #53 Maximum Subarray Easy #66 Plus One Easy #70 ...
lru缓存leetcode leetcode ...Merge Two Sorted Lists 22. Generate Parentheses 25. Reverse Nodes in k-Group 26. Remove Duplicates from Sorted Array 27. Remove Element 28. Implement strStr() 3
leetcode 跳跃 leetcode 动态规划,背包问题 01背包问题:416. Partition Equal Subset Sum ...Array 链表 有序链表合并:21. Merge Two Sorted Lists 回文 双指针判断回文:680. 验证回文字符串 Ⅱ
merge 2 sorted array into a 3rd empty array. ##2019-03-28 斐波那契数列(Fibonacci sequence), 又称黄金分割数列、因数学家列昂纳多·斐波那契(Leonardoda Fibonacci) 以兔子繁殖为例子而引入,故又称为...
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 ...
leetcode中文版 LeetCode # Title Chinese Tag Solution 1 Two Sum 两数之和 array,hash 2 Add Two Numbers 两数相加 math ...Sorted ...pointers,array ...Merge Two Sorted Lists 合并两个有序链表 lin
leetcode 跳跃 leetcode 按题型标签,记录...合并K个排序链表](./Heap/merge-k-sorted-lists.md) String 字符串 [0006 Z字形变换](./String/zigzag-conversion.md) [0030 串联所有单词的子串](./String/substring
Array(数组) ID Difficulty Title Java Python 1 Easy 两数之和 26 Easy 删除排序数组中的重复项 27 Easy 移除元素 35 Easy 搜索插入位置 Linked List(链表) ID Difficulty Title Java Python 21 Easy Merge Two ...
Merge_Sorted_Array 简单的 大批 125 有效回文 简单的 大批 167 两个 Sum II Input 数组排序 简单的 大批 209 最小尺寸子阵列总和 中等的 大批 215 数组中的第 K 个最大元素 中等的 大批 217 包含_重复 简单的 大批 ...
Merge Sorted Array vi. Sum vii. Find Minimum in Rotated Sorted Array viii. Largest Rectangle in Histogram ix. Maximal Rectangle x. Palindrome Number xi. Search a 2D Matrix xii. Search for a Range ...
26 | [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/)| [C++](./C++/remove-duplicates-from-sorted-array.cpp) [Python](./Python/remove-duplicates...
leetcode 2 和 ...Array(c++) 00 94.Binary Tree Inorder Traversal(c++:tree traversal inorder) 100.Same Tree(c++) 101.对称树(c++) 104.二叉树的最大深度(c++) 108.将排序数组转换为二叉搜索树
88.合并两个有序数组 (Merge Sorted Array) 100.相同的树 (Same Tree) 104.二叉树的最大深度 (Maximum Depth of Binary Tree) 118.杨辉三角 (Pascal's Triangle) 119.杨辉三角 II (Pascal's Triangle)
20 Merge Sorted Array 61 ... ... 231 Counting Bits 561 232 Maximum Product of Word Lengths 563 233 Gray Code 565 234 Permutations 567 235 Permutations II 571 236 Permutation Sequence 573 237 Generate ...
总览 ... 例如, merge-sorted-array.py的解决方案位于https://leetcode.com/problems/merge-sorted-array/ 。 Leetcode类似问题 我发现一起解决类似的问题很有意义,这样当我们遇到新问题时,我们可以
liwangStudy note for leetcode.Easy001 Two SumUsing hash map.020 Valid ParenthesesUsing stack can achieve O(n) space and O(n) time.Using Regular match, the complexity depends on the regular algorithm....
Merge Two Sorted Lists] [53. Maximum Subarray] [70. Climbing Stairs] [101. Symmetric Tree] [104. Maximum Depth of Binary Tree] [121. Best Time to Buy and Sell Stock] [167. Two Sum II - Input array is ...