## 一.题目描述
![](https://box.kancloud.cn/2016-01-05_568bb5e9c64f5.jpg)
## 二.解题技巧
这道题不存在复杂的分析过程和边界条件。如果单纯得考虑从小到大地将两个数组进行合并的话,每次在num1中插入一个数的话,需要将后面的元素都向后移动一位,这样,整个处理过程的时间复杂度为O(m*n)。
由于两个数组的元素的个数是知道的,同时,合并后的数组也是递增排序的,也就是说,排序之后的数组的最大值是放在最后面的,因此,我们可以从后往前遍历,也就是将最大值放在第一个数组的m+n-1位置,然后将次最大值放在m+n-2位置,依次类推,这样在将元素放置到合适位置的时候,就不需要移动元素,这个方法的时间复杂度为O(m+n)。
## 三.示例代码
~~~
// 时间复杂度O(m+n),空间复杂度O(1)
class Solution {
public:
void merge(int A[], int m, int B[], int n) {
int ia = m - 1, ib = n - 1, icur = m + n - 1;
while (ia >= 0 && ib >= 0) {
A[icur--] = A[ia] >= B[ib] ? A[ia--] : B[ib--];
}
while (ib >= 0) {
A[icur--] = B[ib--];
}
}
};
~~~
~~~
// 使用STL
#include <iostream>
#include <vector>
using std::vector;
class Solution
{
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n)
{
int ResultIndex = m + n - 1;
m--;
n--;
while (m >= 0 || n >= 0)
{
if (m < 0)
{
nums1[ResultIndex--] = nums2[n--];
continue;
}
if (n < 0)
{
nums1[ResultIndex--] = nums1[m--];
continue;
}
if (m >= 0 && n >= 0)
{
if (nums1[m] > nums2[n])
{
nums1[ResultIndex--] = nums1[m--];
continue;
}
else
{
nums1[ResultIndex--] = nums2[n--];
continue;
}
}
}
}
};
~~~
- 前言
- 2Sum
- 3Sum
- 4Sum
- 3Sum Closest
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array II
- Search in Rotated Sorted Array
- Remove Element
- Merge Sorted Array
- Add Binary
- Valid Palindrome
- Permutation Sequence
- Single Number
- Single Number II
- Gray Code(2016腾讯软件开发笔试题)
- Valid Sudoku
- Rotate Image
- Power of two
- Plus One
- Gas Station
- Set Matrix Zeroes
- Count and Say
- Climbing Stairs(斐波那契数列问题)
- Remove Nth Node From End of List
- Linked List Cycle
- Linked List Cycle 2
- Integer to Roman
- Roman to Integer
- Valid Parentheses
- Reorder List
- Path Sum
- Simplify Path
- Trapping Rain Water
- Path Sum II
- Factorial Trailing Zeroes
- Sudoku Solver
- Isomorphic Strings
- String to Integer (atoi)
- Largest Rectangle in Histogram
- Binary Tree Preorder Traversal
- Evaluate Reverse Polish Notation(逆波兰式的计算)
- Maximum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Longest Common Prefix
- Recover Binary Search Tree
- Binary Tree Level Order Traversal
- Binary Tree Level Order Traversal II
- Binary Tree Zigzag Level Order Traversal
- Sum Root to Leaf Numbers
- Anagrams
- Unique Paths
- Unique Paths II
- Triangle
- Maximum Subarray(最大子串和问题)
- House Robber
- House Robber II
- Happy Number
- Interlaving String
- Minimum Path Sum
- Edit Distance
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock II
- Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock IV
- Decode Ways
- N-Queens
- N-Queens II
- Restore IP Addresses
- Combination Sum
- Combination Sum II
- Combination Sum III
- Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- Longest Consecutive Sequence
- Word Search
- Word Search II
- Word Ladder
- Spiral Matrix
- Jump Game
- Jump Game II
- Longest Substring Without Repeating Characters
- First Missing Positive
- Sort Colors
- Search for a Range
- First Bad Version
- Search Insert Position
- Wildcard Matching