Merge Sorted Array 解答
Question
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 nums1and nums2 are m and n respectively.
Solution 1 Naive Way
Time complexity O(n), space cost O(n)
public class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int[] originNums1 = Arrays.copyOf(nums1, m);
int pointer1 = 0, pointer2 = 0, i = 0;
while (pointer1 < m && pointer2 < n) {
if (originNums1[pointer1] <= nums2[pointer2]) {
nums1[i] = originNums1[pointer1];
pointer1++;
} else {
nums1[i] = nums2[pointer2];
pointer2++;
}
i++;
}
while (pointer1 < m) {
nums1[i] = originNums1[pointer1];
pointer1++;
i++;
}
while (pointer2 < n) {
nums1[i] = nums2[pointer2];
pointer2++;
i++;
}
}
}
Solution 2
The key to solve this problem is moving element of A and B backwards. If B has some elements left after A is done, also need to handle that case.
Time complexity O(n), space cost O(1)
public class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
while (m > 0 && n > 0) {
if (nums1[m - 1] >= nums2[n - 1]) {
nums1[m + n - 1] = nums1[m - 1];
m--;
} else {
nums1[m + n - 1] = nums2[n - 1];
n--;
}
}
while (n > 0) {
nums1[m + n - 1] = nums2[n - 1];
n--;
}
}
}
Merge Sorted Array 解答的更多相关文章
- LeetCode练题——88. Merge Sorted Array
1.题目 88. Merge Sorted Array——Easy Given two sorted integer arrays nums1 and nums2, merge nums2 into ...
- [LeetCode] Merge Sorted Array 混合插入有序数组
Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...
- 43. Merge Sorted Array && LRU Cache
Merge Sorted Array OJ: https://oj.leetcode.com/problems/merge-sorted-array/ Given two sorted integer ...
- 【LeetCode练习题】Merge Sorted Array
Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note ...
- Leetcode#88. Merge Sorted Array(合并两个有序数组)
题目描述 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分别为 m ...
- 【LeetCode】88. Merge Sorted Array (2 solutions)
Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note ...
- 88. Merge Sorted Array【easy】
88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...
- [LeetCode] 88. Merge Sorted Array 混合插入有序数组
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...
- LeetCode_88. Merge Sorted Array
88. Merge Sorted Array Easy Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...
随机推荐
- bzoj1047-理想的正方形(二维单调队列)
题意: 给一个矩阵,给出行列和每个数,再给出一个N,求出所有N*N的子矩阵中最大值最小值之差的最小值解析: 暴力枚举肯定不行,这题可以用二维单调队列做,把同一行的连续N个点缩成一个点保存最大最小值预处 ...
- <php>上传文件的程序
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- NUnit - 使用感受
Nunit使用 最近项目开始大量使用Nunit, 发现Nunit还是有很多好处的. 1. 测试驱动逻辑,这样可以尽最大可能减少“修改”引入的Bug. 如果你修改了一些东西,导致Case跑不过.请检查你 ...
- (转)iOS Wow体验 - 第六章 - 交互模型与创新的产品概念(2)
本文是<iOS Wow Factor:Apps and UX Design Techniques for iPhone and iPad>第六章译文精选的第二部分,其余章节将陆续放出.上一 ...
- THINKPHP之控制器
如何新增控制器? 在Lib/Action中新增一个控制器:ProductAction.class.php: <?php class ProductAction extends Action{ ...
- Composer 基本指令操作使用
Composer 基本指令操作使用 註: 若 composer.phar 改名為 composer, 請自行將 "php composer.phar" 替換成 "comp ...
- [Angular 2] Injecting a Service
Using Services in Angular 2 is very simple. This lesson covers how to create a simple class as a Ser ...
- FineUI按钮控件
按钮的状态与大小 按钮有启用/禁用,按下/正常几种状态,对应的属性分别为Enabled.EnablePress.Pressed三个属性. 按钮有大中小三个尺寸,对应的属性为Size. 按钮上的图标 按 ...
- XML配置silverlight ,wcf 解析xml
XML 代码: <?xml version="1.0" encoding="utf-8" ?><ChartSet xmlns:xsi=&qu ...
- 1、java编程的建议,面试相关
http://www.cnblogs.com/selene/p/5829605.html 面试相关:http://www.cnblogs.com/anrainie/p/5640208.html lin ...